在C ++中将TCHAR转换为字符串

时间:2022-02-26 16:38:19

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

我正在尝试将TCHAR转换为字符串,如下所示:

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.

我需要将mypath设置为路径。我做了一个简单的循环和连接路径[索引]到我的路径,这是有效的,但我不喜欢这种方式。

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.

我是C ++的新手,但已经做了很多C#。我已经看到了传入“char”的GetModuleFileName的例子,但它不喜欢它。它需要TCHAR或LPWSTR。

4 个解决方案

#1


21  

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.

TCHAR是一个定义为char或wchar的宏,具体取决于您定义的字符集。 2008之后的默认设置是将字符设置为unicode。如果您更改字符集,此代码将起作用。

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

Right click on the project settings and chage the folowing

右键单击项目设置并查看下面的内容

在C ++中将TCHAR转换为字符串

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

如果要将TCHAR用作Unicode字符集,请使用wstring

#2


8  

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

如果你想要chars中的路径,你应该调用GetModuleFilenameA。该函数采用LPSTR而不是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).

请注意,几乎所有采用或返回字符串的Win32函数都有两个版本,一个以A(ANSI?)结尾,另一个以W(宽)结尾。

#3


6  

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

当我真的需要这样做时,我使用以下内容:

TCHAR  infoBuf[32767]
GetWindowsDirectory(infoBuf, 32767); 
//Let's convert to string...
wstring test(&infoBuf[0]); //convert to wstring
string test2(test.begin(), test.end()); //and convert to string.

Hope that helps.

希望有所帮助。

#4


5  

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

您还可以使用wcstombs或wcstombs_s函数将_TCHAR *转换为char *

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

#1


21  

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.

TCHAR是一个定义为char或wchar的宏,具体取决于您定义的字符集。 2008之后的默认设置是将字符设置为unicode。如果您更改字符集,此代码将起作用。

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

Right click on the project settings and chage the folowing

右键单击项目设置并查看下面的内容

在C ++中将TCHAR转换为字符串

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

如果要将TCHAR用作Unicode字符集,请使用wstring

#2


8  

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

如果你想要chars中的路径,你应该调用GetModuleFilenameA。该函数采用LPSTR而不是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).

请注意,几乎所有采用或返回字符串的Win32函数都有两个版本,一个以A(ANSI?)结尾,另一个以W(宽)结尾。

#3


6  

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

当我真的需要这样做时,我使用以下内容:

TCHAR  infoBuf[32767]
GetWindowsDirectory(infoBuf, 32767); 
//Let's convert to string...
wstring test(&infoBuf[0]); //convert to wstring
string test2(test.begin(), test.end()); //and convert to string.

Hope that helps.

希望有所帮助。

#4


5  

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

您还可以使用wcstombs或wcstombs_s函数将_TCHAR *转换为char *

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