我无法使用终止进程杀死子进程
I am not able to kill a child process using TerminateProcess
我使用 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引入的,但我可能是错的
相关文章:
- 终止 QProcess 不会终止子进程
- 什么时候最好在子进程中使用 CPU 或 I/O 密集型代码 [ C++ ]
- 子进程更新共享 mmap 内存,但父进程没有更改
- 使用 waitpid 时等待子进程终止
- 使用重定向标准处理子进程中的 kbhit
- 由 JOB 中的进程启动的子进程是否可以将 JOB 属性设置为脱离作业?
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- kill() 总是返回 0(成功),即使在子进程已经结束之后?
- 父进程和子进程之间的 POSIX 信号量
- 检测到由于操作系统内存不足而导致子进程终止
- 使用system()创建独立的子进程
- 从stdin读取时子进程挂起(fork/dup2竞争条件)
- 在 Bash 脚本中处理来自子进程的信号
- Qt C++ - 如何成功将数据传递给子进程?
- C++ 窗口本地系统模拟在子进程中失败
- 将类型化数组写入子进程 stdin 无法正常工作
- 将 nodejs 脚本作为子进程执行(而不是从其他脚本执行)
- 使用信号检测子进程何时终止的最佳方法是什么?
- MPI C 逐行向所有进程子进程发送矩阵 (MPI_COMM_SPAWN)
- 捕获远程进程子进程的标准输出