Windows如何获取已经运行的进程的进程组? [英] Windows how to get the process group of a process that is already running?

查看:61
本文介绍了Windows如何获取已经运行的进程的进程组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,它使用 Process.Start(ProcessStartInfo info)来启动新流程.

I have a WPF application that starts a new process using Process.Start(ProcessStartInfo info).

如何获取进程的组进程ID,以便可以使用 GenerateConsoleCtrlEvent 发送 Ctrl + C 信号? https://msdn.microsoft.com/en-us/library/windows/desktop/ms683155%28v=vs.85%29.aspx

How do I get the group process ID of the process so that I can send a Ctrl+C signal using GenerateConsoleCtrlEvent? https://msdn.microsoft.com/en-us/library/windows/desktop/ms683155%28v=vs.85%29.aspx

但是,我似乎无法在创建的新进程中找到控制台窗口的组进程ID.它具有当前Windows用户的会话ID和进程ID.

However, I can't seem to find the group process ID of the console window in the new process that is created. It has a session ID for the cur windows user and a process ID.

我终于使我的程序开始工作,但是我仍然找不到真正问题的真正答案.

edit: I finally got my program to work but I still never found a true answer to my real question.

通过使用GenerateConsoleCtrlEvent广播到控制台中的所有进程,我能够将ctrl c发送到进程.

I was able to send ctrl c to a process by using GenerateConsoleCtrlEvent to broadcast to all processes in a console.

但是,我无法弄清楚如何获取正在运行的进程的进程组.当然,如果创建新流程,则可以保存流程组(它应该是使用创建新流程组的创建标志调用createprocess的流程的ID).但是,如果您不是自己创建一个新组,而只是想知道某个进程所属的组,那么我找不到与实际获取此ID相关的任何内容.当然,这些信息存储在某个地方并且可以检索!

However, I was not able to figure out how to get the process group of a process that is running. You can of course save the process group if you create a new process (it should be the ID of the process that called createprocess with the creation flag for create new process group). However, I cannot find anything related to actually grabbing this ID if you are not making a new group yourself and just wish to know the group that a process belongs to. Surely this information is stored somewhere and can be retrieved!

我可以使用以下功能在Windows NT版本中获取父ID:从子流程中获取父流程ID

I can get the parent ID in windows NT versions using this function: Fetching parent process Id from child process

但是,这不能保证相同的进程组.我开始得出一个结论,即Windows没有从进程ID函数获取进程组ID.

However, this does not guarantee the same process group. I am starting to conclude that windows does not have get process group id from process id function.

Linux有一个简单的getpgrp函数,它确实希望我正在寻找.我不明白,如果我无法获取Windows的价值,为什么Windows会有一个进程组

Linux has a simple getpgrp function that does want I am looking for. I don't understand why windows has a process group if I can't get the value of it

推荐答案

GenerateConsoleCtrlEvent 状态的文档(重点是我的):

The documentation for GenerateConsoleCtrlEvent states (emphasis mine):

要接收信号的进程组的标识符.在对CreateProcess函数的调用中指定CREATE_NEW_PROCESS_GROUP标志时,将创建一个进程组.新流程的流程标识符也是新流程组的流程组标识符.

因此,如果您的进程在组中,则WPF应用程序的PID应该是组ID.

So if your processes are in a group, the WPF application's PID should be the group ID.

这篇关于Windows如何获取已经运行的进程的进程组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆