Best way to get the path to an executable Topic is solved
Best way to get the path to an executable
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.
Re: Best way to get the path to an executable
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.
Re: Best way to get the path to an executable
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?
Re: Best way to get the path to an executable
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.
There are probably similar keys for other pre-installed apps.
Re: Best way to get the path to an executable Topic is solved
3 sources:
- AHK's WinGet command (or WMI)
- HKEY_CLASSES_ROOT\Applications
- HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
- AHK's WinGet command (or WMI)
- HKEY_CLASSES_ROOT\Applications
- HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
Code: Select all
q:: ;window get path/command line via WinGet / WMI object
WinGet, hWnd, ID, A
;WinGet, hWnd, ID, ahk_class Notepad
WinGet, vPName, ProcessName, % "ahk_id " hWnd
WinGet, vPPath, ProcessPath, % "ahk_id " hWnd
WinGet, vPID, PID, % "ahk_id " hWnd
;Win32_Process class - Windows applications | Microsoft Docs
;https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process
oWMI := ComObjGet("winmgmts:")
oQueryEnum := oWMI.ExecQuery("Select * from Win32_Process where ProcessId=" vPID)._NewEnum()
if oQueryEnum[oProcess]
vCmdLn := oProcess.CommandLine
, vPPath2 := oProcess.ExecutablePath
oWMI := oQueryEnum := oProcess := ""
MsgBox, % vPName "`r`n" vPPath "`r`n" vPPath2 "`r`n" vCmdLn
return
;==================================================
w:: ;names to paths via App Paths registry key
;get selected paths (if available):
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
;get all paths:
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:: ;names to command-line strings via Applications registry key
;get selected command-line strings (if available):
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
;get all command-line strings:
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
Last edited by jeeswg on 20 Aug 2019, 20:04, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Best way to get the path to an executable
Great. Awesome info, guys. I do believe I have all I need to do what I want. Thanks.
Re: Best way to get the path to an executable
I've run into a problem. I keep getting an error 122 ( DllCall("GetLastError") ) -- insufficient Buffer. Code:
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?
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)
Re: Best way to get the path to an executable
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?
Re: Best way to get the path to an executable
See here how to use it -> https://github.com/jNizM/DllExport/blob ... t.ahk#L517
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Re: Best way to get the path to an executable
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.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?
Awesome! Thank you.jNizM wrote:See here how to use it -> https://github.com/jNizM/DllExport/blob ... t.ahk#L517
Re: Best way to get the path to an executable
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:
should have been this:
Noob mistake, noob mistake. Good to go now though.
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)
Code: Select all
DllCall("Api-ms-win-core-version-l1-1-0.dll\GetFileVersionInfo", "Str", TEST, "UInt", 0, "UInt", TEST2, "Ptr", verInfo)
Re: Best way to get the path to an executable
I'm getting annoyed. Can someone do me a favor and tell me if this returns NULL for you:
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.
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
It's assuming "Calculator.exe" is in your Program Files directory. W10 for me, I don't know about earlier OS's.
Re: Best way to get the path to an executable
I'm getting an access violation, according to ErrorLevel. Running as administrator does not help.
Re: Best way to get the path to an executable
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):
That returns "0" for the error level and all is good.
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