Best way to get the path to an executable Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Malonn
Posts: 43
Joined: 04 Nov 2016, 09:14

Best way to get the path to an executable

30 Aug 2017, 14:41

I'm wondering what's the best way to get the path to an executable? I'm calling "GetFileVersionInfoSize" in order to call "GetFileVersionInfo" and I need the full path and executable name to various files. Is there a function that makes quick work of this? Maybe built in to Windows? I haven't really looked at AHK functions because I'm thinking Windows can do it more easily? What I'm eventually going to do is get the description from it's file info dialogue.
Elesar
Posts: 69
Joined: 31 Oct 2013, 07:56

Re: Best way to get the path to an executable

30 Aug 2017, 14:45

If you are looking for a installed application, you can read the registry entry related to the program and find it's location. Normally you could look for the uninstall string then cut that down to the root folder of the application and build your paths from there.
User avatar
Malonn
Posts: 43
Joined: 04 Nov 2016, 09:14

Re: Best way to get the path to an executable

30 Aug 2017, 15:34

Ah, interesting. What would you think about files that ship with Windows? Same thing? Like a accessory such as the mail app in Windows 10? Or the calculator? Are the path's to those applications stored in the registry like third-party applications?
Elesar
Posts: 69
Joined: 31 Oct 2013, 07:56

Re: Best way to get the path to an executable

30 Aug 2017, 16:03

Some items that ship with Windows will be on the PATH, making them easy to access. For Calculator, you can simply have a line in AHK that says "run calc" and that will start Calculator. Doesn't work with Mail, but you can locate the directory via Registry in this case: HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows Mail\InstallRoot
There are probably similar keys for other pre-installed apps.
User avatar
jeeswg
Posts: 5942
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Best way to get the path to an executable  Topic is solved

30 Aug 2017, 16:42

3 sources:
- AHK's WinGet command (or WMI)
- HKEY_CLASSES_ROOT\Applications
- HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

Code: Select all

q::
WinGet, hWnd, ID, A
WinGet, vPName, ProcessName, % "ahk_id " hWnd
WinGet, vPPath, ProcessPath, % "ahk_id " hWnd
WinGet, vPID, PID, % "ahk_id " hWnd

oWMI := ComObjGet("winmgmts:")
oQueryEnum := oWMI.ExecQuery("Select * from Win32_Process where ProcessId=" vPID)._NewEnum()
	if oQueryEnum[oProcess]
		vCmdLn := oProcess.CommandLine
oWMI := oQueryEnum := oProcess := ""

MsgBox, % vPName "`r`n" vPPath "`r`n" vCmdLn
return

;==================================================

w::
vOutput := ""
vList := "chrome.exe,firefox.exe,iexplore.exe,wordpad.exe,notepad.exe"
Loop, Parse, vList, `,
{
	vName := A_LoopField
	;RegRead, vPath, % "HKEY_LOCAL_MACHINE", % "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" vName
	RegRead, vPath, % "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" vName
	vOutput .= vName "`t" vPath "`r`n"
}
;Clipboard := vOutput
MsgBox, % vOutput

vOutput := ""
Loop, Reg, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths, K
{
	vName := A_LoopRegName
	;RegRead, vPath, % "HKEY_LOCAL_MACHINE", % "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" vName
	RegRead, vPath, % "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" vName
	vOutput .= vName "`t" vPath "`r`n"
}
;Clipboard := vOutput
MsgBox, % vOutput
return

;==================================================

e::
vOutput := ""
vList := "chrome.exe,firefox.exe,iexplore.exe,wordpad.exe,notepad.exe"
Loop, Parse, vList, `,
{
	vName := A_LoopField
	;RegRead, vPath, % "HKEY_CLASSES_ROOT", % "Applications\" vName "\shell\open\command"
	RegRead, vPath, % "HKEY_CLASSES_ROOT\Applications\" vName "\shell\open\command"
	vOutput .= vName "`t" vPath "`r`n"
}
;Clipboard := vOutput
MsgBox, % vOutput

vOutput := ""
Loop, Reg, HKEY_CLASSES_ROOT\Applications, K
{
	vName := A_LoopRegName
	;RegRead, vPath, % "HKEY_CLASSES_ROOT", % "Applications\" vName "\shell\open\command"
	RegRead, vPath, % "HKEY_CLASSES_ROOT\Applications\" vName "\shell\open\command"
	vOutput .= vName "`t" vPath "`r`n"
}
;Clipboard := vOutput
MsgBox, % vOutput
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Malonn
Posts: 43
Joined: 04 Nov 2016, 09:14

Re: Best way to get the path to an executable

30 Aug 2017, 17:42

Great. Awesome info, guys. I do believe I have all I need to do what I want. Thanks.
User avatar
Malonn
Posts: 43
Joined: 04 Nov 2016, 09:14

Re: Best way to get the path to an executable

30 Aug 2017, 22:02

I've run into a problem. I keep getting an error 122 ( DllCall("GetLastError") ) -- insufficient Buffer. Code:

Code: Select all

Loop, Files, %A_ProgramFiles%\Calculator.exe, DFR
	TEST := A_LoopFileFullPath
TEST2 := DllCall("Api-ms-win-core-version-l1-1-0.dll\GetFileVersionInfoSize", "Str", TEST, "Ptr*", 0)
TEST3 := DllCall("Api-ms-win-core-version-l1-1-0.dll\GetFileVersionInfo", "Str", TEST, "UInt", 0, "UInt", %TEST2%, "Ptr*", verInfo)
Google turns up nothing for that error code and this function. I'm passing what's returned by the GetFileVersionInfoSize. But, I've troubleshot some, and I put the variable at the max for a 32 bit integer (2147483647) and it still 122'd out. What else could the problem be?
Elesar
Posts: 69
Joined: 31 Oct 2013, 07:56

Re: Best way to get the path to an executable

31 Aug 2017, 09:13

I haven't looked into that DLL specifically, but is there a reason you are calling it instead of AHK's built in commands for file version (FileGetVersion) and file size (FileGetSize)? Does it return more information than AHK's functions?
User avatar
jNizM
Posts: 2369
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Best way to get the path to an executable

31 Aug 2017, 09:28

[AHK] 1.1.30.01 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
Malonn
Posts: 43
Joined: 04 Nov 2016, 09:14

Re: Best way to get the path to an executable

31 Aug 2017, 09:37

Elesar wrote:I haven't looked into that DLL specifically, but is there a reason you are calling it instead of AHK's built in commands for file version (FileGetVersion) and file size (FileGetSize)? Does it return more information than AHK's functions?
Yes. This function gives you access to everything on the "Details" tab of a file's properties dialogue in Windows. I'm looking for the description of a file, and AHK does not have a function for that.
jNizM wrote:See here how to use it -> https://github.com/jNizM/DllExport/blob ... t.ahk#L517
Awesome! Thank you.
User avatar
Malonn
Posts: 43
Joined: 04 Nov 2016, 09:14

Re: Best way to get the path to an executable

31 Aug 2017, 10:25

Alright, I see my problem. It is a noob mistake, but I actually did have it right at first. But "GetFileVersionInfo" kept returning NULL, so I tried adding % around the variable and it returned 0, so I thought I was right. Turns out my real problem was the argument type the whole time. I'll be careful with that the next time.
For anyone interested, this:

Code: Select all

DllCall("Api-ms-win-core-version-l1-1-0.dll\GetFileVersionInfo", "Str", TEST, "UInt", 0, "UInt", %TEST2%, "UPtr", verInfo)
should have been this:

Code: Select all

DllCall("Api-ms-win-core-version-l1-1-0.dll\GetFileVersionInfo", "Str", TEST, "UInt", 0, "UInt", TEST2, "Ptr", verInfo)
Noob mistake, noob mistake. Good to go now though.
User avatar
Malonn
Posts: 43
Joined: 04 Nov 2016, 09:14

Re: Best way to get the path to an executable

31 Aug 2017, 13:51

I'm getting annoyed. Can someone do me a favor and tell me if this returns NULL for you:

Code: Select all

Loop, Files, %A_ProgramFiles%\Calculator.exe, DFR
	TEST := A_LoopFileFullPath
TEST2 := DllCall("Api-ms-win-core-version-l1-1-0.dll\GetFileVersionInfoSize", "Str", TEST, "Ptr*", 0)
TEST3 := DllCall("Api-ms-win-core-version-l1-1-0.dll\GetFileVersionInfo", "Str", TEST, "UInt", 0, "UInt", TEST2, "Ptr", verInfo)
MsgBox % TEST3
the box should be a positive number. 0 means it failed. I'll take either of those, but what I'm getting is NULL. Just a blank message. That's not right. Sorry for being dense on this, but I'm troubleshooting...

It's assuming "Calculator.exe" is in your Program Files directory. W10 for me, I don't know about earlier OS's.
User avatar
Malonn
Posts: 43
Joined: 04 Nov 2016, 09:14

Re: Best way to get the path to an executable

31 Aug 2017, 14:03

I'm getting an access violation, according to ErrorLevel. Running as administrator does not help.
User avatar
Malonn
Posts: 43
Joined: 04 Nov 2016, 09:14

Re: Best way to get the path to an executable

31 Aug 2017, 18:27

Alright. I think I have it figured. I should have just read the code linked to by jNizM more carefully. I was missing the setting of the variable's capacity (which I had tried but didn't paste in the forums), but what I really missed was the address ampersand (&). So, this code works (which is exactly what jNizM tried to show):

Code: Select all

Loop, Files, %A_ProgramFiles%\Calculator.exe, DFR
	TEST := A_LoopFileFullPath
TEST2 := DllCall("Api-ms-win-core-version-l1-1-0.dll\GetFileVersionInfoSize", "Str", TEST, "Ptr*", 0)
VarSetCapacity(verData, TEST2 + A_PtrSize)
TEST3 := DllCall("Api-ms-win-core-version-l1-1-0.dll\GetFileVersionInfo", "Str", TEST, "UInt", 0, "UInt", TEST2, "Ptr", &verData)
MsgBox % ErrorLevel
That returns "0" for the error level and all is good.

Return to “Ask For Help”

Who is online

Users browsing this forum: ah_sis90, AviationGuy, electrone77, Google [Bot], israelss, ktbjx, MannyKSoSo, peperami, scriptor2016, sinkfaze and 157 guests