viewpoint-particle

Author Topic: Get application handle from process name  (Read 45 times)

Jeremy Whilde

  • Newbie
  • *
  • Posts: 46
Get application handle from process name
« on: April 20, 2017, 10:32:05 am »
Is there a way in WB to get a handle from an application process name? or from the PID?

So if you have an application running as app.exe and you do not have its window name but you have the "app.exe" as process name and if required I can also get the PID of this, like this:

#DefineFunction udfProcID(processname)
   retvalue = 0
   objWMIService = ObjectGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

   strQuery = 'select * from win32_process where Name = "%processname%"'
   colProcesses = objWMIService.ExecQuery( strQuery )
   ForEach objProcess In colProcesses
       procid = objProcess.ProcessId     ;Specify required property here
       
       If procid > 0 Then retvalue = procid
       Break
   Next

   objProcess    = 0
   colProcesses  = 0
   objWMIService = 0
   Return retvalue
#EndFunction

processname = 'app.exe'   ; program / process name


ret = udfProcID (processname)


Message("Process ID for %processname%", ret)

So how do you get the handle for the app.exe? (remember I have not got the app.exe main window title as this unfortunately changes)

I had thought that I could use the above function and specify the property as "Handle" but this seems to also return the process ID and not the handle for the app.exe!

Thanks JW

td

  • Tech Support
  • *****
  • Posts: 1983
    • WinBatch
Re: Get application handle from process name
« Reply #1 on: April 20, 2017, 12:58:04 pm »
Check out this Tech Database article.  It illustrates using the OpenProcess kernel32 function to obtain a process handle:

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/Samples~from~Users+Account~Name~Associated~with~a~Process.txt

Here is a link to the win32 function's documentation:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms684320(v=vs.85).aspx

Please note the required SeDebugPrivilege privilege.
"DON'T PANIC”
The Hitchhiker's Guide to the Galaxy, Douglas Adams