Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding PID of all processes running

Status
Not open for further replies.

mikeprestim

Programmer
Mar 1, 2001
21
GB
Is there any way in perl on Win32 to return a list of all process ID for all processes running and not a child of mine, or even catching one just by its name. Similar to csh ps -e | grep "prog", I do not want to have to shell out to a csh script to do it.

Thanks in advance

Mike McMahon
 
Here is some code I'm playing with that SHOULD work, however... I haven't worked out how to properly pack the ProcessEntry structure. Feel free to play with it... you'll notice 2 different attempts at the pack... 2 of many that I have tried...

<code>
use Win32::API;

$CreateToolhelp32Snapshot=new Win32::API(&quot;kernel32&quot;,&quot;CreateToolhelp32Snapshot&quot;,[N,N],N);
$ProcessFirst=new Win32::API(&quot;kernel32&quot;,&quot;Process32First&quot;,[N,P],N);
$ProcessNext=new Win32::API(&quot;kernel32&quot;,&quot;Process32Next&quot;,[N,P],N);

# typedef struct tagPROCESSENTRY32 {
# DWORD dwSize;
# DWORD cntUsage;
# DWORD th32ProcessID;
# DWORD th32DefaultHeapID;
# DWORD th32ModuleID;
# DWORD cntThreads;
# DWORD th32ParentProcessID;
# LONG pcPriClassBase;
# DWORD dwFlags;
# TCHAR szExeFile[MAX_PATH]; # MAX_PATH=260
# DWORD th32MemoryBase;
# DWORD th32AccessKey;
# } PROCESSENTRY32;

$template=&quot;LLLLLLLLLA260LL&quot;;

$snapshot=$CreateToolhelp32Snapshot->Call(2,0);
#print &quot;Snapshot: $snapshot\n&quot;;
if ($snapshot) {
$ProcessEntry=pack($template,0,0,0,0,0,0,0,0,0,' 'x260,0,0);
$result=$ProcessFirst->Call($snapshot,$ProcessEntry);
#print &quot;ProcessFirst: $result\n&quot;;
while ($result) {
($size,$usage,$pid,$hid,$mid,$threads,$ppid,$base,$flags,$file,$mem,$akey)=
unpack($template,$ProcessEntry);

# foo;

$ProcessEntry=pack($template,());
$result=$ProcessNext->Call($snapshot,$ProcessEntry);
#print &quot;ProcessFirst: $result\n&quot;;
}
}
</code>
 
Jay


thanks for code. I have not had much luck with it as of yet.
I am returning an undefined value on
$CreateToolhelp32Snapshot
$ProcessFirst
$ProcessNext

so I the script fails when it tries the call.
I have not done to much with win32::api yet so it is time to read the help pages on it.

Regards

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top