LPTSTR and LPCSTR C++

Rob picture Rob · May 12, 2012 · Viewed 9.7k times · Source

So the output of the function GetUserName() gives the username as a LPTSTR. I need to convert this to a LPCSTR, as I want the username to be the name of the an ftpdirectory.

TCHAR id [UNLEN+1];
DWORD size = UNLEN+1;
GetUserName(id, &size); // this is an LPTSTR

FtpCreateDirectory(hFtpSession,id) // 2d parameter should be an LPCSTR

The problem is that I need to convert the LPTSTR string to a LPCSTR string. Now I know that:

LPTSTR is a (non-const) TCHAR string and LPCSTR is a const string.

But how do I convert a TCHAR to a const string?

I should note I don't have a rich programming/C++ background, I should also note that I'm compiling in multi-byte, not unicode.

Answer

CB Bailey picture CB Bailey · May 12, 2012

As you are compiling for multi-byte, not unicode you don't have to do anything. LPTSTR will convert implicitly to LPCSTR as it's just a char* to const char* conversion.