notepad get/set path (get/set text file path)

Post your working scripts, libraries and tools
iPhilip
Posts: 293
Joined: 02 Oct 2013, 12:21

Re: notepad get/set path (get/set text file path)

Post by iPhilip » 18 Jul 2018, 17:25

Hi jeeswg,

Thank you for the great functions. I found a way to simplify the GetPath function and make it run significantly faster (~100 times). Here it is:

Code: Select all

F3::MsgBox % GetNotepadWinPath(WinExist("A"))

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)
   if !(Class = "Notepad" && hProc)
      Return
   DllCall("IsWow64Process", "Ptr", hProc, "IntP", IsWow64Process)
   BaseAddress := DllCall(A_PtrSize = 4  ? "GetWindowLong" : "GetWindowLongPtr", "Ptr", hwnd, "Int", -6, "Ptr")  ; GWLP_HINSTANCE = -6
   Address := !A_Is64bitOS || IsWow64Process ? BaseAddress + 0xCAE0 : BaseAddress + 0x10B40
   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
}
Cheers!

- iPhilip

EDIT: Changed the function so that it now works with both Notepad x32 and x64 on Windows 7.
Last edited by iPhilip on 18 Jul 2018, 20:59, edited 1 time in total.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
jeeswg
Posts: 5136
Joined: 19 Dec 2016, 01:58
Location: UK

Re: notepad get/set path (get/set text file path)

Post by jeeswg » 18 Jul 2018, 18:05

- This is great, thanks very much for sharing. So GWLP_HINSTANCE points to the application in the address space I suppose. Any background details about what led you to this discovery? And also, which OSes have you tested it on?
- This worked for me on Notepad x64 on Windows 7. Although it didn't work for me on Notepad x32 on Windows 7, or on a copy of Notepad x32 Windows XP version on Windows 7. I suppose though that GWLP_HINSTANCE might be able to speed up my current script anyhow, if it points you to the first address space regions to try. Thanks again.
iPhilip
Posts: 293
Joined: 02 Oct 2013, 12:21

Re: notepad get/set path (get/set text file path)

Post by iPhilip » 18 Jul 2018, 18:46

jeeswg wrote:- This is great, thanks very much for sharing. So GWLP_HINSTANCE points to the application in the address space I suppose. Any background details about what led you to this discovery? And also, which OSes have you tested it on?
I was inspired by looking at the getProcessBaseAddress() method from RHCP's classMemory class. You can find it here.
jeeswg wrote:- This worked for me on Notepad x64 on Windows 7. Although it didn't work for me on Notepad x32 on Windows 7, or on a copy of Notepad x32 Windows XP version on Windows 7. I suppose though that GWLP_HINSTANCE might be able to speed up my current script anyhow, if it points you to the first address space regions to try. Thanks again.
I only tested it on Windows 7 with Notepad x64. If you can make it work with Notepad x32, it would be great. I am less interested in Windows XP as it's disappearing.
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
iPhilip
Posts: 293
Joined: 02 Oct 2013, 12:21

Re: notepad get/set path (get/set text file path)

Post by iPhilip » 18 Jul 2018, 21:02

Hi jeeswg,

I updated the above post with a slightly modified function that now works with both Notepad x32 and x64 on Windows 7.

- iPhilip
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
User avatar
jeeswg
Posts: 5136
Joined: 19 Dec 2016, 01:58
Location: UK

Re: notepad get/set path (get/set text file path)

Post by jeeswg » 18 Jul 2018, 21:45

- Thanks for the insight.
- Your update now works on Notepad Windows 7 x64/x32 for me also. Although not on Notepad Windows XP x32, I will investigate.
- I see that you've made one change:
Before: PROCESS_VM_READ
After: PROCESS_QUERY_INFORMATION | PROCESS_VM_READ
- It seems that on both AHK x64/x32, PROCESS_VM_READ was enough to work on Notepad x64, but PROCESS_QUERY_INFORMATION was also needed to work on Notepad x32.
- And yes, I did some benchmark tests, and the new function is around 75 to 100 times faster. (Although using the old function still seemed almost instant to me, I hope it didn't seem noticeably slow to anyone.)
iPhilip
Posts: 293
Joined: 02 Oct 2013, 12:21

Re: notepad get/set path (get/set text file path)

Post by iPhilip » 18 Jul 2018, 22:10

Hi jeeswg,

All your observations are correct. The old function didn't seem slow to me. I just did the speed tests to see if there was an advantage from that perspective.

Thank you for the work you did on these functions. I learned a lot from studying them as well as messing around with Process Hacker. :P

- iPhilip
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
Post Reply

Return to “Scripts and Functions”