实际碰到的一个问题,从MSDN上拷贝了一段代码,是用C写的,编译通过,执行崩溃,
#include <Windows.h>
// test.c 用Unicode方式编译崩溃
void main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( !CreateProcess( NULL, // No module name (use command line)
"calc.exe", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
以上代码用unicode方式c编译可以通过,运行时崩溃,编译器会报个警告,儿非错误
test.c(13) : warning C4133: 'function' : incompatible types - from 'char [9]' to 'LPWSTR'
CreateProcessW 的第二个参数要去是LPWSTR ,这里被强制转换了而c++方式编译的话会报错,直接编译不过
test.cpp(21) : error C2664: 'CreateProcessW' : cannot convert parameter 2 from 'const char [9]' to 'LPWSTR'