Get handle of current process

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Okapi
Junior Coder
Posts: 28
Joined: Wed Dec 29, 2010 1:22 am

Get handle of current process

Post by Okapi » Fri Jul 22, 2011 4:16 pm

Hi all,

Trying to get the handle of the current process with OpenProcess function with no success. Maybe the problem is in the DesiredAccess value (PROCESS_ALL_ACCESS) ! Can someone help ? thanks.

Code: Select all

LibLoad>kernel32.dll,hLib
LibFunc>kernel32.dll,GetCurrentProcessId,intPId
LibFunc>kernel32.dll,OpenProcess,hProc,PROCESS_ALL_ACCESS,TRUE,%intPId%
MDL>hProc
LibFunc>kernel32.dll,CloseHandle,rslt,%hProc%
LibFree>hLib

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Jul 22, 2011 9:48 pm

I don't understand what it is you're trying to do. GetCurrentProcessId "Retrieves the process identifier of the calling process." which is the process id of YOUR process. Then you follow up with OpenProcess using the id of your process. Since your process is already open. I'm obviously missing something, what is it?

Okapi
Junior Coder
Posts: 28
Joined: Wed Dec 29, 2010 1:22 am

Post by Okapi » Fri Jul 22, 2011 9:57 pm

OpenProcess returns the handle of the specified process

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Sat Jul 23, 2011 2:01 am

Got it... I think...

Using 1 for True and 16 for PROCESS_VM_READ rather than PROCESS_ALL_ACCESS.

Try this. It seems to work for me.

Code: Select all

LibLoad>kernel32.dll,hLib
LibFunc>kernel32.dll,GetCurrentProcessId,intPId
LibFunc>kernel32.dll,OpenProcess,hProc,16,1,intPId
MDL>hProc
LibFunc>kernel32.dll,CloseHandle,rslt,%hProc%
LibFree>hLib

What can you do with a process handle once you have it?

Edit- Maybe this isn't it. Running this multiple times in the editor produces a different result each time. My understanding of handles is that a handle is fixed until the process stops. Yet my understanding of the process is that a non-zero result is a handle. I don't know what is correct.

Okapi
Junior Coder
Posts: 28
Joined: Wed Dec 29, 2010 1:22 am

Post by Okapi » Sat Jul 23, 2011 5:00 pm

The returned value is indeed the handle of the current process, thanks. I use the handle to check if the process is running inside a job or not with IsProcessInJob function.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts