C ++:将TCHAR数组转换为std :: string

时间:2014-04-25 13:26:23

标签: c++ string visual-studio-2010 char type-conversion

我想将TCHAR数组转换为std::string。在搜索互联网后,我这样做了:

TCHAR fileName[260];
GetProcessName(
    reinterpret_cast<DWORD>(processId), 
    fileName, 
    MAX_PATH
    );
std::wstring tmpWString( fileName );
std::string result( tmpWString.begin(), tmpWString.end() );

std::cout << "the length of the tmpWString is: " << tmpWString.length() << std::endl;

这为我提供tmpWString长度0。然而,纯粹的巧合,我在转换之前添加了一个睡眠功能,然后一切顺利,我可以正确地获得转换结果:

TCHAR fileName[260];
::Sleep(500);
GetProcessName(
    reinterpret_cast<DWORD>(processId), 
    fileName, 
    MAX_PATH
    );
std::wstring tmpWString( fileName );
std::string result( tmpWString.begin(), tmpWString.end() );

std::cout << "the length of the tmpWString is: " << tmpWString.length() << std::endl;
std::cout << "this is the resulted string: " << result << std::endl;

我没理解。为什么添加睡眠会使其工作而省略它会导致零长度wstring?毕竟我不想在我的代码中包含任何睡眠。有人可以解释一下吗?我的工作环境是使用Windows XP的虚拟机上的Visual Studio 2010。对于VS2010的Character Set Configuration Properties,我没有指定任何设置。提前致谢。

0 个答案:

没有答案
相关问题