Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Getting a full executable path from a running process


  • Please log in to reply
7 replies to this topic
halpo
  • Members
  • 5 posts
  • Last active: Apr 14 2011 08:05 PM
  • Joined: 12 Feb 2009
I need to find out the full path of a running process. I could be running any of several versions of a program, all have the same process name as I would get from
WinGet ProcName, ProcessName, WinTitle
The problem is that I need to get back to the original location so that I can run
FileGetVersion Version, FullEXEPath

to get the version number so that the behavior can change dynamically.

Does anyone know how to get the full executable path from a running process?[/code]

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
^F2::

 WinGet, PID, PID, A

 FullEXEPath := GetModuleFileNameEx( PID )

 FileGetVersion, Version, %FullEXEPath%

 MsgBox, 0, %Version%, %FullEXEPath%

Return



GetModuleFileNameEx( p_pid ) ; by shimanov -  www.autohotkey.com/forum/viewtopic.php?t=9000

{

   if A_OSVersion in WIN_95,WIN_98,WIN_ME

   {

      MsgBox, This Windows version (%A_OSVersion%) is not supported.

      return

   }



   h_process := DllCall( "OpenProcess", "uint", 0x10|0x400, "int", false, "uint", p_pid )

   if ( ErrorLevel or h_process = 0 )

      return



   name_size = 255

   VarSetCapacity( name, name_size )



   result := DllCall( "psapi.dll\GetModuleFileNameExA", "uint", h_process, "uint", 0, "str"

   , name, "uint", name_size )



   DllCall( "CloseHandle", h_process )



   return, name

}


halpo
  • Members
  • 5 posts
  • Last active: Apr 14 2011 08:05 PM
  • Joined: 12 Feb 2009
Works wonderfully! thanks a ton. This will help out a lot.

ProgrammerPaul
  • Members
  • 75 posts
  • Last active: Oct 05 2017 01:14 PM
  • Joined: 28 May 2009
I changed the msgbox to MsgBox %FullEXEPath%
Does anyone have any idea why I might get:
---------------------------
AutoHotkey.ahk
---------------------------
屜敤汬尵畁潴潈歴祥停啁屌䍓呉居䍓呉⹅塅E䵞潹
---------------------------
OK
---------------------------

Or this one with Notepad.exe launched from the Run window in the Start Menu:
---------------------------
AutoHotkey.ahk
---------------------------
?ʓ귐;OWNLOADS\UTILITIES\MACRO\AutoHotkey\headshot.txt
---------------------------
OK
---------------------------
That's the name of a file, almost, it's c:\DOWNLOADS. The file is monitored by my AutoHotkey script, but it wasn't loaded in notepad anywhere.
I confirmed the PID is correct.
I tried this on a Windows 7 pro and Windows 2008 R2 server.

Any help would be appreciated. I'm only interested in the path. I'm a bit surprised that this isn't built in. Then again, it seems to have taken me years of heavy use to want to do this :)

Thanks
Paul

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Here is a rectified version:

^F2::
 WinGet, PID, PID, A
 FullEXEPath := GetModuleFileNameEx( PID )
 FileGetVersion, Version, %FullEXEPath%
 MsgBox, 0, %Version%, %FullEXEPath%
Return

GetModuleFileNameEx( p_pid ) ; by shimanov -  www.autohotkey.com/forum/viewtopic.php?t=9000
{
   if A_OSVersion in WIN_95,WIN_98,WIN_ME
   {
      MsgBox, This Windows version (%A_OSVersion%) is not supported.
      return
   }

   h_process := DllCall( "OpenProcess", "uint", 0x10|0x400, "int", false, "uint", p_pid )
   if ( ErrorLevel or h_process = 0 )
      return

   name_size = 255
   VarSetCapacity( name, name_size )

   result := DllCall( "psapi.dll\GetModuleFileNameEx" [color=#FF0000]( A_IsUnicode ? "W" : "A" )[/color]
                 , "uint", h_process, "uint", 0, "str", name, "uint", name_size )

   DllCall( "CloseHandle", h_process )

   return, name
}

kWo4Lk1.png

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
FYI, with AutoHotkey_L you can use:
WinGet Path, Process[color=darkred]Path[/color], WinTitle


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
I did not know.. Thanks :)

ProgrammerPaul
  • Members
  • 75 posts
  • Last active: Oct 05 2017 01:14 PM
  • Joined: 28 May 2009
Lexikos for the win!

Thanks for your help, as always :)