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
.
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.
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:
Hope that helps.
If you want the path in chars, you should call
GetModuleFilenameA
. That function takesLPSTR
instead ofLPTSTR
.Note that almost all Win32 functions that take or return strings have two version, one ending in
A
(ANSI?) and the other ending inW
(wide).You can also convert from
_TCHAR*
tochar*
usingwcstombs
orwcstombs_s
functionhttp://msdn.microsoft.com/en-us/library/5d7tc9zw%28v=vs.80%29.aspx