得到CreateProcess返回的句柄后,我调用TerminateProcess,为进程导出代码传递42。然后,我使用WaitForSingleObject终止进程,最后调用GetExitCodeProcess。
该函数调用均未报告错误。子进程是一个无限循环,不会自行终止。
问题在于,有时GetExitCodeProcess为退出代码返回42(应该如此),有时返回0。为什么?
#include <string>
#include <sstream>
#include <iostream>
#include <assert.h>
#include <windows.h>
void check_call( bool result, char const * call );
#define CHECK_CALL(call) check_call(call,#call);
int
main( int argc, char const * argv[] )
{
if( argc>1 )
{
assert( !strcmp(argv[1],"inf") );
for(;;)
{
}
}
int err=0;
for( int i=0; i!=200; ++i )
{
STARTUPINFO sinfo;
ZeroMemory(&sinfo,sizeof(STARTUPINFO));
sinfo.cb=sizeof(STARTUPINFO);
PROCESS_INFORMATION pe;
char cmd_line[32768];
strcat(strcpy(cmd_line,argv[0])," inf");
CHECK_CALL((CreateProcess(0,cmd_line,0,0,TRUE,0,0,0,&sinfo,&pe)!=0));
CHECK_CALL((CloseHandle(pe.hThread)!=0));
CHECK_CALL((TerminateProcess(pe.hProcess,42)!=0));
CHECK_CALL((WaitForSingleObject(pe.hProcess,INFINITE)==WAIT_OBJECT_0));
DWORD ec=0;
CHECK_CALL((GetExitCodeProcess(pe.hProcess,&ec)!=0));
CHECK_CALL((CloseHandle(pe.hProcess)!=0));
err += (ec!=42);
}
std::cout << err;
return 0;
}
std::string
get_last_error_str( DWORD err )
{
std::ostringstream s;
s << err;
LPVOID lpMsgBuf=0;
if( FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
0,
err,
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,
0,
0) )
{
assert(lpMsgBuf!=0);
std::string msg;
try
{
std::string((LPCSTR)lpMsgBuf).swap(msg);
}
catch(
... )
{
}
LocalFree(lpMsgBuf);
if( !msg.empty() && msg[msg.size()-1]=='\n' )
msg.resize(msg.size()-1);
if( !msg.empty() && msg[msg.size()-1]=='\r' )
msg.resize(msg.size()-1);
s << ", \"" << msg << '"';
}
return s.str();
}
void
check_call( bool result, char const * call )
{
assert(call && *call);
if( !result )
{
std::cerr << call << " failed.\nGetLastError:" << get_last_error_str(GetLastError()) << std::endl;
exit(2);
}
}
最佳答案
我以为nobugz的评论已被钉住了...
但是,这还有另一个大胆的猜测-也许您调用TerminateProcess()
太早而不是太晚,而Windows并没有完全序列化由TerminateProcess()
指定的设置进程错误代码以及它在进程初始化时可能要执行的操作?我认为这不太可能,但是值得将Seth suggested(甚至是Sleep(1)
)调用的Sleep(0)
调用放入以查看行为是否发生了变化。
另外,您可能要考虑发布一些人们可以尝试的代码。