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
.
So depending on your compilation configuration, you can convert TCHAR* to string or wstring. To use UNICODE character set, click Project->Properties->Configuration Properties->General->Character Set, and then select "Use Unicode Character Set".
For Unicode platforms, TCHAR is defined as synonymous with the WCHAR type. MAPI clients can use the TCHAR data type to represent a string of either the WCHAR or char type. Be sure to define the symbolic constant UNICODE and limit the platform when it is required.
Use _tcslen to find the length of a null-terminated array of TCHAR , if this is really what you wish to do. However, you might be better served by: Not using TCHAR at all an instead use only UTF-16 encoded text, the platform native encoding. Not using C strings and instead use std::wstring .
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
if You want to use TCHAR as a Unicode character set use wstring
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With