iPhilip wrote: ↑18 Jul 2018, 17:25
I found a way to simplify the
GetPath function and make it run significantly faster (~100 times).
EDIT: Changed the function so that it now works with both Notepad x32 and x64 on Windows 7.
Hello
@iPhilip and
@jeeswg
Does this function work consistently when multiple windows of notepad are open?
I'm trying to understand the code to make it work for Windows 10.
I have played with the code a little to make it give me the file path of the notepad active window (This doesn't work yet as expected).
Also two lines are added to display some variables most important the BaseAddress and Address.
Code: Select all
q::
hWnd := WinActive("ahk_class Notepad") ; get active window's id and store within hWnd
MsgBox, % GetNotepadWinPath(hWnd)
return
GetNotepadWinPath(hwnd) {
static MAX_PATH := 260
WinGetClass, Class, ahk_id %hwnd%
WinGet, PID, PID, ahk_id %hwnd%
hProc := DllCall("OpenProcess", "UInt", 0x410, "Int", 0, "UInt", PID, "Ptr") ; PROCESS_QUERY_INFORMATION (0x400) | PROCESS_VM_READ (0x010)
MsgBox, % "Process ID: " PID "`nProcess Start Path: " vPPath "`nWindow id: " hWnd "`nhProc: " hProc ; display (PID, vPPath, hWnd, hProc) for debugging purposes
if !(Class = "Notepad" && hProc)
Return
DllCall("IsWow64Process", "Ptr", hProc, "IntP", IsWow64Process)
BaseAddress := DllCall(A_PtrSize = 4 ; If DLL call fails, returned value will = 0
? "GetWindowLong"
: "GetWindowLongPtr"
, "Ptr", hWnd, "Int", -6, A_Is64bitOS ? "Int64" : "UInt") ; GWLP_HINSTANCE = -6
Address := !A_Is64bitOS || IsWow64Process ? BaseAddress + 0xCAE0 : BaseAddress + 0x10B40
MsgBox, % "BaseAddress: " Format("{1:#x}",BaseAddress) "`nAddress: " Format("{1:#x}",Address) ; display (BaseAddress, Address) for debugging purposes
VarSetCapacity(Path, MAX_PATH*2, 0)
DllCall("ReadProcessMemory", "Ptr", hProc, "Ptr", Address, "Str", Path, "UPtr", MAX_PATH*2, "UPtr", 0)
DllCall("CloseHandle", "Ptr", hProc)
Return Path
}
I always get the same BaseAddress and Address whatever was the active window.
So I think that this approach will not work well to get file paths of different notepad windows.
I'm still a little confused if I will be able to get some BaseAddress that's related to the active window itself and see some constant offset where the file path is stored in.
All the best
Omar