|
|
Hai all,
Can anyone tell how to convert LPTSTR to char*
Thanks in advance
George K Jolly
|
|
|
|
|
Depends on what you are compiled for. If Ansi just cast it otherwise you have to use a conversion function or one of the macros like CT2A etc.
led mike
|
|
|
|
|
By using a type-cast. What code is giving you trouble?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
If UNICODE is not defined (that is, an ANSI build), LPTSTR is a char*. If UNICODE is defined, you can use the T2A macro from ATL, or use _bstr_t (or various other means) to end up with a char*.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
CGenericList.Clear method should clear the contents of m_Data from the memory heap. This is done because strdup is called to create a duplicate of the string in a linked list so that data is not overwritten.
However, should CGenericList be of type int the Clear method would not compile since it does not take int types as parameters:
GenericList.h(32): error C2664: 'free' : cannot convert parameter 1 from 'int' to 'void *'
SentenceList.cpp
---------------------------------------------------------------------
CSentenceList::CSentenceList(char strFileToRead[MAX_SENTENCE_LENGTH + 1], bool bSorted)
: CGenericList<char*> ()
...
void CSentenceList::readFile(char strFileToRead[MAX_FILENAME_LENGTH])
{
char strLine[MAX_SENTENCE_LENGTH + 1];
fstream fileToRead(strFileToRead,ios::in);
while (!fileToRead.eof() && Size < MAX_LINES)
// Read line of text from file and update Sentences.strSentence.
{ fileToRead.getline(strLine, MAX_SENTENCE_LENGTH);
Sentences.Insert(strdup(strLine));
Size++;
}
fileToRead.close();
}
...
GenericList.h
---------------------------------------------------------------------
template<class T>
class CGenericList
void Clear() // empties the list
{
CGenericNode<T> *pTemp = m_pFirst;
// clear the data
while(pTemp != NULL)
{
CGenericNode<T> *pRemember = pTemp->m_pNextNode;
free (pTemp->m_data);
pTemp = pRemember;
}
// clear the nodes
while(pTemp != NULL)
{
CGenericNode<T> *pRemember = pTemp->m_pNextNode;
delete pTemp;
pTemp = pRemember;
}
m_iCount = 0;
}
Is it wise and possible to say, for example:
if typeof(<T>) == char*
// Clear the data
...
free (pTemp->m_data);
...
Alternatively how can I loop through the methods from the derived class, in this case, CSentenceList?
Jon
|
|
|
|
|
jon_80 wrote: while(pTemp != NULL)
{
CGenericNode<t> *pRemember = pTemp->m_pNextNode;
free (pTemp->m_data);
pTemp = pRemember;
}
First, it doesn't look like you are allocating your data variable on the heap, so there is no need to delete it.
Second, it is generally unwise to use 2 different heaps like this (malloc/free is not the same heap as that of new/delete).
Third, in general, when writing generic classes like this, you accept any type and just hold it. If your link list is holding pointers (e.g. char*), it is up to the module that created those pointers to delete them. This is especially true in this case since if you pass a pointer to a char* that was created with new and try to free it using free instead of delete, you will corrupt memory.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Regarding the third point it makes sense from a design point of view, but, how can I free up memory from the calling module?
The memory heap is allocated as follows:
...
while (!fileToRead.eof() && Size < MAX_LINES)
// Read line of text from file and update Sentences.strSentence.
{ fileToRead.getline(strLine, MAX_SENTENCE_LENGTH);
Sentences.Insert(strdup(strLine));
// string is copied, this is where I think I am allocating memory.
Size++;
}
...
The problem is that protected members and public members cannot be called from derived classes (somehow!!). Can I send u the code somewhere?
Jon
|
|
|
|
|
First of all, this is one of the reasons why a few people recommended you use the standard string class, but I digress.
You're list class (which I believe you called GenericList), should have some accessor (either a get method, or iterators) to the data it holds. Whatever module is using the above code (the one calling strdup) should also have a cleanup method (or simply the destructor) that will iterate through the list and call free on each data element prior to emptying the list (which the list class itself should take care of).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
The weird thing is that I'm getting access related errors when I'm trying to clean-up from the calling class.
...
CSentenceList::~CSentenceList()
{
// Clear the data
CGenericNode<char*> *pTemp = Sentences.m_pFirst;
while(pTemp != NULL)
{
CGenericNode<char*> *pRemember = pTemp->m_pNextNode;
free (pTemp->data);
pTemp = pRemember;
}
}
...
c:\Documents and Settings\Jon\My Documents\Visual Studio Projects\IAD Practice and Assignment\C++ - Assignment\02 & 03 - Sentence\SentenceClass\SentenceList.cpp(19): error C2248: 'CGenericList<T>::m_pFirst' : cannot access private member declared in class 'CGenericList<T>'
with
[
T=char *
]
and
[
T=char *
]
c:\Documents and Settings\Jon\My Documents\Visual Studio Projects\IAD Practice and Assignment\C++ - Assignment\02 & 03 - Sentence\SentenceClass\SentenceList.cpp(22): error C2248: 'CGenericNode<T>::m_pNextNode' : cannot access private member declared in class 'CGenericNode<T>'
with
[
T=char *
]
and
[
T=char *
]
CGenericList.h
#pragma once
#include ".\GenericNode.h"
#include <iostream>
#include <stdlib.h>
#include <tchar.h>
using namespace std;
template<class T>
class CGenericList
{
public:
CGenericList(void)
{
m_pFirst = NULL;
m_pLast = NULL;
m_iCount = 0;
}
virtual ~CGenericList(void)
{
Clear();
}
void Insert(T pData)
{
// create the node to store our data
CGenericNode<T> *pNode = new CGenericNode<T>;
pNode->data = pData; // set the payload
if(m_iCount == 0)
{
m_pFirst = pNode;
m_pLast = pNode;
}
else
{
m_pLast->m_pNextNode = pNode;
m_pLast = pNode;
}
m_iCount++;
}
bool Find(T pData)
{
CGenericNode<T> *pTemp = m_pFirst;
while(pTemp != NULL)
{
if(pTemp->data == pData)
return true;
pTemp = pTemp->m_pNextNode;
}
return false;
}
bool Remove(T pData)
{
CGenericNode<T> *pTemp = m_pFirst;
CGenericNode<T> *pPrevious = NULL; // keep track of previous node
while(pTemp != NULL)
{
if(pTemp->data == pData)
{
// we found the node!
RemoveNode(pPrevious, pTemp); // call helper method
return true; // true indicates that something was removed
}
pPrevious = pTemp; // remember the previous node
pTemp = pTemp->m_pNextNode;
}
return false; // we found nothing here
}
int GetCount()
{
return m_iCount;
}
void Display()
{
CGenericNode<T> *pTemp = m_pFirst;
int iCount = 0;
while(pTemp != NULL)
{
iCount++;
cout << iCount << "|";
cout << pTemp->data << _T(" ");
cout << endl;
pTemp = pTemp->m_pNextNode;
}
}
protected:
int m_iCount;
CGenericNode<T> *m_pFirst;
CGenericNode<T> *m_pLast;
// helper method
void Clear() // empties the list
{
CGenericNode<T> *pTemp = m_pFirst;
// clear the nodes
while(pTemp != NULL)
{
CGenericNode<T> *pRemember = pTemp->m_pNextNode;
delete pTemp;
pTemp = pRemember;
}
m_iCount = 0;
}
void RemoveNode(CGenericNode<T> *pPrevious, CGenericNode<T> *pTemp)
{
if(pPrevious == NULL) // remove the first node
{
m_pFirst = pTemp->m_pNextNode;
if(m_pFirst == NULL) m_pLast = NULL; // this was the only node
delete pTemp; // delete the original node
}
else
{
pPrevious->m_pNextNode = pTemp->m_pNextNode; // skip this node
if(pPrevious->m_pNextNode == NULL) // in case we are deleting the last node
m_pLast = pPrevious;
delete pTemp;
}
m_iCount--;
}
};
CGenericNode.h
#pragma once
#include <stdlib.h>
// forward declaration of CGenericList (it is a template)
// and is a friend of CGenericNode
template<class T> class CGenericList;
// declaration of CGenericNode
template<class T>
class CGenericNode
{
friend CGenericList<T>;
public:
CGenericNode()
{
m_pNextNode = NULL;
}
T data;
//protected:
// T m_data;
private:
CGenericNode<T> *m_pNextNode;
};
Jon
|
|
|
|
|
Hello,
I received an error message when i tried to send serial port the datas with outportb.
%Starting with dos.h library
...
int port=0x378;
outportb(int port, unsigned int);
...
...
for ( i=0 ; i<8 ; i++)
{
outportb(port,member.u8value[i]);
}
...
Error Message ==> [Linker Error] Unresolved external '_outportb' referenced from H:\INTERFACE\INTERFACE.OBJ
(Interface is the file in which all datas are saved)
What could be the problem? Thanks for your tricks in advance.
Regards,
Cahit
|
|
|
|
|
In Windows NT and later, you cannot directly access serial ports.
But check this article[^]
See Part 5 for a link to the source to inpout32.dll, which can be used in NT +
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Hello,
I'm currently programming a basic TCP client-server application, but i've some problems with winsock.
Can you help me?
I have this errors in my program:
error C2079: 'ifr' uses undefined struct 'ifreq'
error C2065: 'buffer' : undeclared identifier
error C2065: 'DEVICE' : undeclared identifier
error C2065: 'ioctl' : undeclared identifier
error C2065: 'SIOCGIFINDEX' : undeclared identifier
error C2039: 'ifr_ifindex' : is not a member of 'ifreq'
see declaration of 'ifreq'
error C2065: 'SIOCGIFHWADDR' : undeclared identifier
error C2039: 'ifr_hwaddr' : is not a member of 'ifreq'
see declaration of 'ifreq'
error C2228: left of '.sa_data' must have class/struct/union type
error C2065: 'PACKET_OTHERHOST' : undeclared identifier
error C2065: 'time' : undeclared identifier
error C2027: use of undefined type 'ethhdr'
see declaration of 'ethhdr'
error C2227: left of '->h_proto' must point to class/struct/union
error C2065: 'ETH_TYPE_NULL' : undeclared identifier
error C2065: 'gettimeofday' : undeclared identifier
error C2027: use of undefined type 'ethhdr'
see declaration of 'ethhdr'
What are the header file for the ifreq struct? and for the sockaddr_ll struct?
Can you help me?
thanks
|
|
|
|
|
try this:
#include <afxsock.h> // MFC socket extensions
|
|
|
|
|
try this:
#include // MFC socket extensions
I didn't understand, where i get the MFC socket extensions.
Is it needed a program to install those extensions??
Can you help me again?
|
|
|
|
|
He didn't type the <> correctly. He was saying to include:
#include <afxsock.h>
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
thank you Zac....i still get the same errors with or without the MFC socket extensions.
What are the header file for ifreq, sockaddr_ll structures??
thanks
|
|
|
|
|
They should be in the winsock2.h header (which is included from the afxsock.h file).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Those structures aren't include in winsock2.h
Another question, Is necessary put this lines before open a socket?
int wsaret=WSAStartup(0x101,&wsaData);
//WSAStartup returns zero on success.
//If it fails we exit.
if(wsaret!=0)
{
return 0;
}
|
|
|
|
|
afpr wrote: Another question, Is necessary put this lines before open a socket?
int wsaret=WSAStartup(0x101,&wsaData);
Only if you want it to work. Otherwise, you'll likely receive a WSANOTINITIALISED error.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
You don't have to call that before you open each socket, but it does need to be called at some point in your application prior to opening a socket.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
To work with sendto() or recvfrom() functions, is it necessary to use WSAstartup function?
|
|
|
|
|
WSAStartup MUST be called once in your program before any socket operations can be performed. This is typically don't in the InitInstance method, but doesn't have to be.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
thanks Zac, but i have some errors, can you help me?
These are de errors:
1-
'DEVICE' : undeclared identifier
'ioctlsocket' : cannot convert parameter 3 from 'struct ifreq *' to 'unsigned long *'
in this lines:
strncpy(ifr.ifr_name, DEVICE, IFNAMSIZE);
if (ioctlsocket(s, SIOCGIFINDEX, &ifr) == -1) {
perror("SIOCGIFINDEX");
exit(1);
}
2-
'function-style cast' : illegal as right side of '.' operator
left of '.sa_data' must have class/struct/union type
in this line:
for (i = 0; i < 6; i++) {
src_mac[i] = ifr.sockaddrifr_hwaddr.sa_data[i]; src_mac is unsigned char src_mac[6]
}
3-
use of undefined type 'ethhdr'
see declaration of 'ethhdr'
left of '->h_proto' must point to class/struct/union
ETHER_TYPE_NULL' : undeclared identifier
in this line:
eh->h_proto = ETHER_TYPE_NULL;
4-
'gettimeofday' : undeclared identifier
in this line:
gettimeofday(&begin,NULL);
thanks
|
|
|
|
|
You need to include header file or files did you include files
|
|
|
|