Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

c# - Windows how to get the process group of a process that is already running?

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

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

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.

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

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!

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

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 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The documentation for GenerateConsoleCtrlEvent states (emphasis mine):

The identifier of the process group to receive the signal. A process group is created when the CREATE_NEW_PROCESS_GROUP flag is specified in a call to the CreateProcess function. The process identifier of the new process is also the process group identifier of a new process group.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...