Converting TCHAR to string in C++
Asked Answered
I

5

24

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.

Inflexed answered 15/5, 2011 at 3:34 Comment(0)
P
27

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

Psychogenesis answered 15/5, 2011 at 3:53 Comment(5)
Better yet, define a typedef on TCHAR: typedef std::basic_string<TCHAR> tstring; and use tstring everywhere.Cyperaceous
Thanks for the help. Setting the charactor set above worked. Now I can use type char in the GetModuleFileName method.Inflexed
This gives me the error a value of type "const char *" cannot be used to initialize an entity of type "TCHAR *"Gabbard
@Gabbard 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.Psychogenesis
You are the ONE the ONE and ONLY. Who really pin pointed the real pain of TCHAR. WOW it was great answer.Arleen
N
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.
Northwester answered 27/1, 2015 at 18:16 Comment(0)
G
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).

Grand answered 15/5, 2011 at 7:27 Comment(0)
P
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

Psalmist answered 28/6, 2012 at 12:46 Comment(0)
I
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.

Inflexed answered 7/6, 2020 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.