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
996 views
in Technique[技术] by (71.8m points)

windows - Win32: How to enumerate child processes?

What's the best way to enumerate the child processes of the currently running process under Win32? I can think of a couple of ways to do it, but they seem overly complicated and slow. Here's the requirements for the solution:

  1. Specifically I need to know if any there are any processes currently running which were started by the current process.
  2. Will be running on WinXP and should not require distributing special DLL's.
  3. Should not require a lot of CPU overhead (it will be running periodically in the background).
  4. I'll eventually be writing this in Delphi, but I can convert from whatever language you have the code in. Mostly I'm looking for the most efficient set of Win32 API's to use.

Thanks!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You could use the toolhelp API

#include <tlhelp32.h>

Process32First() 

And loop using

Process32Next()

http://www.codeproject.com/KB/threads/processes.aspx

EDIT delphi

uses tlhelp32;

procedure FillAppList(Applist: Tstrings); 
var   Snap:THandle; 
        ProcessE:TProcessEntry32; 
begin 
     Applist.Clear; 
     Snap:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
     ProcessE.dwSize:=SizeOf(ProcessE); 
     if Process32First(Snap,ProcessE) then 
     begin 
          Applist.Add(string(ProcessE.szExeFile)); 
          while Process32Next(Snap,ProcessE) do 
                 .. compare parent id
      end 
      CloseHandle(Snap); 
end;

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

...