简体   繁体   English

我无法使用TerminateProcess杀死子进程

[英]I am not able to kill a child process using TerminateProcess

I have a problem to kill a child process using TerminateProcess . 我在使用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. 这段代码被多次调用,多次启动同一program.exe,而这些过程在任务管理器中都存在,我认为这不好。 Actually is created two process all the time: the program.exe and conhost.exe. 实际上一直都在创建两个进程:program.exe和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 

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 ) 您不能杀死在与调用TerminateProcess的进程不同的安全上下文下运行的进程( 请参见此处
  • the process is doing something in kernel mode (eg some unfinished I/O operations by driver, etc) - I believe this was introduced with Vista, but I might be wrong 该进程正在内核模式下执行某些操作(例如,驱动程序执行一些未完成的I / O操作等)-我相信这是在Vista中引入的,但是我可能错了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM