我无法使用终止进程杀死子进程

I am not able to kill a child process using TerminateProcess

本文关键字:进程 子进程 终止      更新时间:2023-10-16

我使用 TerminateProcess 杀死子进程时遇到问题。我调用此函数并且进程仍然存在(在任务管理器中)。这段代码被调用了很多次启动同一个程序.exe很多次,这些进程在任务管理器中,我认为这不好。实际上一直创建两个进程:程序.exe和主机.exe。

我将非常感谢任何帮助。

这是代码:

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 
我知道有两个

可能的原因:

  • 您不能终止在与调用 TerminateProcess 的进程不同的安全上下文中运行的进程(请参阅此处)
  • 该进程正在内核模式下执行某些操作(例如驱动程序的一些未完成的I/O操作等) - 我相信这是Vista引入的,但我可能是错的