Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++:将TCHAR数组转换为std::string_C++_String_Visual Studio 2010_Char_Type Conversion - Fatal编程技术网

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

C++:将TCHAR数组转换为std::string,c++,string,visual-studio-2010,char,type-conversion,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()

我想将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;

我不明白。为什么添加睡眠会使其工作,而忽略睡眠会导致长度为零的wstring?毕竟,我不想在代码中包含任何睡眠。有人能解释一下吗?我的工作环境是Visual Studio 2010,在一台装有Windows XP的虚拟机上。对于VS2010的配置属性的字符集,我没有指定任何字符集。提前谢谢

请尝试在两个示例中显示结果,并向我们展示…/在文件名部分添加一些值。如果你把事情搞砸了,你可能会有不确定的行为。顺便说一句,你的应用程序是多线程的吗?我更新了我的问题,是的,这段代码是多线程的。@Ben:没有睡眠,couting给我一个空字符串,但是有睡眠,它正确地给出了代码获得的进程名。@E\U learner:尝试进一步隔离问题。要有系统,并采用积极的试错策略来找出错误的原因。例如,将变量或宏逐个替换为固定值,即文字,查看是否会更改某些内容。在运行多个线程之前,尝试在主线程中运行代码。等等如果睡眠改变了你所描述的行为方式,那么事情就会严重混乱,猜测可能不会有多大帮助:
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;