问题描述
在我得到的CreateProcess返回的句柄,我称之为TerminateProcess,经过42进程退出code。然后,我用了WaitForSingleObject的进程终止,最后我打电话GetExit codeProcess。
函数的调用无报告错误。子进程是一个无限循环,并在自己不会终止。
问题是,有时GetExit codeProcess为出口code返回42(因为它应该),有时则返回0。任何想法,为什么?
的#include<串GT;
#包括LT&;&sstream GT;
#包括LT&;&iostream的GT;
#包括LT&;&ASSERT.H GT;
#包括LT&;&WINDOWS.H GT;无效check_call(布尔结果,字符常量*电话);
#定义CHECK_CALL(呼叫)check_call(调用,调用#);INT
主(INT ARGC,字符常量*的argv [])
{
如果(argc个大于1)
{
断言(STRCMP(的argv [1],INF)!);
对于(;;)
{
}
}
INT ERR = 0;
的for(int i = 0; i = 200;!++我)
{
STARTUPINFO sinfo;
ZeroMemory(安培; sinfo,sizeof的(STARTUPINFO));
sinfo.cb = sizeof的(STARTUPINFO);
PROCESS_INFORMATION PE;
焦炭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((GetExit codeProcess(pe.hProcess,&安培; EC)= 0)!);
CHECK_CALL((CloseHandle的(pe.hProcess)= 0)!);
ERR + =(EC = 42!);
}
性病::法院LT&;<呃;
返回0;
}标准::字符串
get_last_error_str(DWORD ERR)
{
的std :: ostringstream秒;
小号所述&;&下;呃;
LPVOID lpMsgBuf = 0;
如果(FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
0,
呃,
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)
(LPSTR)及lpMsgBuf,
0,
0))
{
断言(lpMsgBuf!= 0);
的std :: string信息;
尝试
{
标准::字符串((LPCSTR)lpMsgBuf).swap(MSG);
}
抓住(
...)
{
}
LocalFree(lpMsgBuf);
如果(!msg.empty()及和放大器;味精[msg.size() - 1] =='\\ n')
msg.resize(msg.size() - 1);
如果(!msg.empty()及&放大器;味精[msg.size() - 1] =='\\ R')
msg.resize(msg.size() - 1);
小号所述&;&下; ,\\<<味精<<'';
}
返回s.str();
}空虚
check_call(布尔结果,字符常量*调用)
{
断言(调用&安培;&放大器; *通话);
如果(!结果)
{
的std :: CERR<<调用<< 失败\\ nGetLastError:<< get_last_error_str(GetLastError函数())≤;&下;的std :: ENDL;
出口(2);
}
}
请阅读的和
基本上,TerminateProcess函数是一个丑陋的烂摊子应该避免的。在你自己的危险使用它。
如果在所有可能的使用应该使用其他机制来关闭您的进程。
After I get a handle returned by CreateProcess, I call TerminateProcess, passing 42 for the process exit code. Then, I use WaitForSingleObject for the process to terminate, and finally I call GetExitCodeProcess.
None of the function calls report errors. The child process is an infinite loop and does not terminate on its own.
The problem is that sometimes GetExitCodeProcess returns 42 for the exit code (as it should) and sometimes it returns 0. Any idea why?
#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);
}
}
Basically, the TerminateProcess function is an ugly mess that should be avoided. Use it at your own peril.
If at all possible use should use another mechanism to shutdown your process.
这篇关于42传给TerminateProcess,有时GetExit codeProcess返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!