6

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.

user3840170
  • 26,597
  • 4
  • 30
  • 62
Rob
  • 175
  • 3
  • 3
  • 12
  • When not converting anything: GetLastError() returns: 12003 and InternetGetLastResponseInfo() returns:7771a9a8. – Rob May 12 '12 at 17:16
  • just to note, FTPCreateDirectory api takes LPCTSTR , so you could simply do FTPCreateDirectory(hFtpSession,&id) – johnathan May 12 '12 at 19:11
  • GetUsername also takes a LPTSTR , and there you'd also take the reference of your tchar array. – johnathan May 12 '12 at 19:13
  • Turns out I had FILE (not a directory) on my ftpserver that had the exact name of my username. So when I deleted that file, all worked fine. Thanks for your help nevertheless. – Rob May 12 '12 at 20:52

3 Answers3

4

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
2

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.

Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Interesting, when I just do this: cout << FtpCreateDirectory(hFtpSession,username) << endl; cout << GetLastError() << endl; The boolean turns 0 (indicating the function failed) and the Error message I recieve is: 12003. I can't find what this error means as it is not described on http://msdn.microsoft.com/en-us/library/windows/desktop/ms681384(v=vs.85).aspx. – Rob May 12 '12 at 17:07
  • But if I'd say newstring = "directoryname" and then FtpCreateDirectory(hFtpSession,newstring); it works. Why's that? – Rob May 12 '12 at 17:11
0
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.

johnathan
  • 2,315
  • 13
  • 20
  • Thanks for your replies jonathon, but it still doesn't work. I get the exact same errors. But if I'd say newstring = "directoryname" and then FtpCreateDirectory(hFtpSession,newstring); it works... – Rob May 12 '12 at 20:22