views:

2406

answers:

4

Would anyone hapen to know how to convert a LPTSTR to a char* in c++?

Thanks in advance, workinprogress.

+3  A: 

Here are a lot of ways to do this. MFC or ATL's CString, ATL macros, or Win32 API.

LPTSTR szString = _T("Testing");
char* pBuffer;

You can use ATL macros to convert:

USES_CONVERSION;
pBuffer = T2A(szString);

CString:

CStringA cstrText(szString);

or the Win32 API WideCharToMultiByte if UNICODE is defined.

John Z
+4  A: 

Depends if it is Unicode or not it appears. LPTSTR is char* if not Unicode, or w_char* if so.

Discussed better here (accepted answer worth reading)

KiwiBastard
+1  A: 
char * pCopy = NULL;
if (sizeof(TCHAR) == sizeof(char))
{
    size_t size = strlen(pOriginal);
    pCopy = new char[size + 1];
    strcpy(pCopy, pOriginal);
}
else
{
    size_t size = wcstombs(NULL, pOriginal, 0);
    pCopy = new char[size + 1];
    wcstombs(pCopy, pOriginal, size + 1);
}
Mark Ransom
A: 

Thanks for the help!