I am not able to kill a child process using TerminateProcess

我的未来我决定 提交于 2019-12-11 20:20:45

问题


I have a problem to kill a child process using TerminateProcess. I call to this function and the process still there (in the Task Manager). This piece of code is called many times launching the same program.exe many times and these process are there in the task manager which i think is not good. Actually is created two process all the time: the program.exe and conhost.exe.

I will really appreciate any help.

Here is the code:

STARTUPINFO childProcStartupInfo;
memset( &childProcStartupInfo, 0, sizeof(childProcStartupInfo));
childProcStartupInfo.cb = sizeof(childProcStartupInfo);
childProcStartupInfo.hStdInput = hFromParent;   // stdin
childProcStartupInfo.hStdOutput = hToParent;    //  stdout
childProcStartupInfo.hStdError = hToParentDup;  // stderr
childProcStartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
childProcStartupInfo.wShowWindow = SW_HIDE;


PROCESS_INFORMATION childProcInfo;  /* for CreateProcess call */



bOk = CreateProcess(
    NULL,           // filename
    pCmdLine,   // full command line for child
    NULL,           // process security descriptor */
    NULL,           // thread security descriptor */
    TRUE,           // inherit handles? Also use if STARTF_USESTDHANDLES */
    0,              // creation flags */
    NULL,           // inherited environment address */
    NULL,           // startup dir; NULL = start in current */
    &childProcStartupInfo,          // pointer to startup info (input) */
    &childProcInfo);            // pointer to process info (output) */

CloseHandle( hFromParent );
CloseHandle( hToParent );
CloseHandle( hToParentDup );

CloseHandle( childProcInfo.hThread);
CloseHandle( childProcInfo.hProcess);

TerminateProcess( childProcInfo.hProcess ,0);  //this is not working, the process 

回答1:


There are two possible reasons that I know of:

  • you can't kill a process running under a different security context than the one of the process which calls TerminateProcess (see here)
  • the process is doing something in kernel mode (e.g. some unfinished I/O operations by driver, etc) - I believe this was introduced with Vista, but I might be wrong


来源:https://stackoverflow.com/questions/12485379/i-am-not-able-to-kill-a-child-process-using-terminateprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!