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.

有帮助吗?

解决方案

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.

其他提示

If you are not compiling for Unicode, TCHAR=char, so you don't need to convert anything. On the other hand, when compiling for Unicode you must perform a conversion; there are several alternatives for this, have a look here.

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

FtpCreateDirectory(hFtpSession,&id[0]);

This code should work in unicode or multibyte builds.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top