23

I'm trying to convert a TCHAR to a string as in:

std::string mypath;
TCHAR path[MAX_PATH];
GetModuleFileName( NULL, path, MAX_PATH );

I need to set mypath to that of path. I did a simple loop and concatenated path[index] to mypath and this works but I don't like this way.

I'm new to C++ but have done plenty of C#. I've seen examples of the GetModuleFileName that passes in a "char" but it doesn't like it. It needs the TCHAR or a LPWSTR.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
David
  • 241
  • 1
  • 2
  • 3

5 Answers5

26

TCHAR is a macro defined as a char or wchar depending on what you have your character set defined to. The default after 2008 is have the character set to unicode. this code works if you change your character set.

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR* bob ="hi";
    string s = bob;    
}

Right click on the project settings and chage the folowing

enter image description here

if You want to use TCHAR as a Unicode character set use wstring

rerun
  • 25,014
  • 6
  • 48
  • 78
  • 11
    Better yet, define a typedef on `TCHAR`: `typedef std::basic_string tstring;` and use `tstring` everywhere. – Pablo May 15 '11 at 04:34
  • Thanks for the help. Setting the charactor set above worked. Now I can use type char in the GetModuleFileName method. – David May 15 '11 at 05:18
  • This gives me the error `a value of type "const char *" cannot be used to initialize an entity of type "TCHAR *"` – ThisClark Sep 20 '15 at 19:01
  • 1
    @ThisClark Is TCHAR defined. This works on VS2013 and 2015. TCHAR is defined in tchar.h if that is not included this of course will not work. – rerun Sep 21 '15 at 16:20
  • You are the ONE the ONE and ONLY. Who really pin pointed the real pain of TCHAR. WOW it was great answer. –  Feb 03 '17 at 09:23
15

When I really need to do it I use the following:

TCHAR  infoBuf[32767];
GetWindowsDirectory(infoBuf, 32767);

And then I convert it to a wstring which can be converted to a standard std::string:

wstring test(&infoBuf[0]); //convert to wstring
string test2(test.begin(), test.end()); //and convert to string.
Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
OhadM
  • 4,687
  • 1
  • 47
  • 57
8

If you want the path in chars, you should call GetModuleFilenameA. That function takes LPSTR instead of LPTSTR.

Note that almost all Win32 functions that take or return strings have two version, one ending in A (ANSI?) and the other ending in W (wide).

avakar
  • 32,009
  • 9
  • 68
  • 103
5

You can also convert from _TCHAR* to char* using wcstombs or wcstombs_s function

http://msdn.microsoft.com/en-us/library/5d7tc9zw%28v=vs.80%29.aspx

nuoritoveri
  • 2,494
  • 1
  • 24
  • 28
1

Hi this is a late answer but I have an idea.

{wstring test = User;
 std::wcout << test << std::endl;
 string test2(test.begin(), test.end());
 std::cout << test2 << std::endl;}

User is in this example the username as a TCHAR. Now I can use the name as a string or wstring. This is the easiest way to convert the TCHAR to a string.

David
  • 11
  • 1