Most unusual bug ever - Run command

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
func
Posts: 47
Joined: 20 May 2016, 20:52

Most unusual bug ever - Run command

10 Aug 2017, 02:14

In the following file is a fully working script that will append a new menu item to Notepad's menu bar named "Foo", when selecting items in this "Foo" menu, you will get msgbox alerts. For this to work, you need to download hook.dll from: https://www.autoitscript.com/forum/appl ... hp?id=7166
and place it in same folder as the script

Now try commenting out line 8 (Run Notepad) and then manually run notepad while the script is running - try selecting the menus under "Foo", nothing will happen!
However, the "Foo" menu gets created, so ALL the same code is run as when Run Notepad is not commented out -
This is why it's so strange, literally the only thing different in the two scenarios is one has notepad Run by ahk and one does not, and it completely changes whether the script works or not.. Does anyone know why???

Code: Select all

#SingleInstance Force
#Persistent
#NoEnv

if not A_IsAdmin
Run *RunAs "%A_AhkPath%" "%A_ScriptFullPath%"

Run Notepad
SetTimer, CheckForWindow, 100
Return

CheckForWindow:
WinGet, active_processName, ProcessName, A
if (active_processName = "notepad.exe") 
{
	WinGet, hWnd, ID, A
	WinGet, WinTitle, PID, A
	WinTitle := "ahk_pid " WinTitle
	result := CheckMenu(hWnd)
if (result = 0)
{
	MainMenu := DllCall("GetMenu", "UInt", hWnd)
	MyMenu := AppendMenu(MainMenu, "&Foo")
	AppendMenuItem(MyMenu, 1000, "Apple")
	MySubMenu := AppendMenu(MyMenu, "Open Me")
	AppendMenuItem(MySubMenu, 1001, "Apple1")
	RedrawMenuBar(hWnd)
	HookMsg(WinTitle, WM_COMMAND:=0x111, "_Filter")
}
}
Return

_Filter(wParam, lParam, msg, hwnd) {
	If (wParam=1000 AND msg = 273)
		MsgBox, click on 1000
	If (wParam=1001 AND msg = 273)
		MsgBox, click on 1001
}

AppendMenu(MenuHandle, MenuName) {
	MF_POPUP = 16
	SubMenuHandle := DllCall("CreateMenu")
	DllCall("AppendMenu", "Ptr", MenuHandle, "UInt", MF_POPUP, "UPtr", SubMenuHandle, "Str", MenuName)
	Return SubMenuHandle
}

AppendMenuItem(MenuHandle, ItemID, ItemName) {
	DllCall("AppendMenu", "Uint", MenuHandle, "UInt", 0, "Uint", ItemID, "Str", ItemName)
}

RedrawMenuBar(hWnd) {
	DllCall("DrawMenuBar", "Ptr", hWnd)	
}

CheckMenu(hWnd)
{
	menu_hWnd := DllCall("GetMenu", "int", hWnd)
	if (DllCall("GetMenuItemCount", "ptr", menu_hWnd) > -1) {
		Loop, % DllCall("GetMenuItemCount", "ptr", menu_hWnd)
		{
			VarSetCapacity(string, 256)
			SubMenu_hWnd := DllCall("GetSubMenu", "int", menu_hWnd, "int", A_Index)
			string_result := DllCall("GetMenuString" , "int", menu_hWnd, "int", SubMenu_hWnd, "str", string, "int", 128, "int", 0x0000)
			if (string = "&Foo")
				Return 1
		}
		Return 0
		}
	Return 1
}

HookMsg(WinTitle, MsgNumber, Function) {
	; hook.dll -- https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=7166
	static hModule := DllCall("LoadLibrary", "Str", "hook.dll", "Ptr")
	static _ := { base: {__Delete: "HookMsg"} }
	static iThreadIdTarget, hWndTarget
	If !_ {
		DllCall("hook.dll\UnInstallFilterDLL", "Int", iThreadIdTarget, "Ptr", hWndTarget, "Ptr", A_ScriptHwnd)
		Return DllCall("FreeLibrary", "Ptr", hModule)
	}

	hWndTarget := WinExist(WinTitle)

	static WH_CALLWNDPROC := 4
	     , WH_GETMESSAGE := 3
	     , WM_USER := 1024
	     , UM_ADDMESSAGE := WM_USER + 0x100
	iThreadIdTarget := DllCall("GetWindowThreadProcessId", "Ptr", hWndTarget, "Ptr", 0, "UInt")
	hook := DllCall("hook.dll\InstallFilterDLL", "Int", WH_CALLWNDPROC, "Int", iThreadIdTarget, "Ptr", hWndTarget) ; 0 = Ok
	hookG := DllCall("hook.dll\InstallFilterDLL", "Int", WH_GETMESSAGE, "Int", iThreadIdTarget, "Ptr", hWndTarget) ; 0 = Ok

	DllCall("SendMessage", "Ptr", hWndTarget, "UInt", UM_ADDMESSAGE, "UPtr", MsgNumber, "Ptr", A_ScriptHwnd, "Ptr")
	OnMessage(MsgNumber, Function)
	_hWndTarget := hWndTarget
}
just me
Posts: 9528
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Most unusual bug ever - Run command

10 Aug 2017, 04:15

Just a guess: You force the script to run as Administrator, so Notepad might need to run 'as Administrator', too. It does if run it by the script.
func
Posts: 47
Joined: 20 May 2016, 20:52

Re: Most unusual bug ever - Run command

10 Aug 2017, 11:59

Thanks for the guess, it led me to a partial answer:

First of all, the script doesn't even need to be run as admin, that was just a shotgun approach to try and fix it

Neither does notepad need to be run as admin,

But using everything search by voidtools to search for 'notepad.exe', I discovered there are 3 notepad.exe in different location on my windows 10 system..
One is in C:\Windows, one is in C:\Windows\System32, and one is in C:\Windows\SysWOW64
Only the one run from C:\Windows\SysWOW64 will work with the hook.dll, and I guess that is the one AHK runs, weird as hell!
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
Contact:

Re: Most unusual bug ever - Run command

10 Aug 2017, 12:05

Nothing unusual about this.

It's 2017 and so I'll assume you're running a 64-bit version of Windows, like me. Here on 10, C:\Windows\system32 comes before C:\Windows in %PATH%. As a full path to Notepad.exe is not supplied, this means C:\Windows\system32\Notepad.exe is ran.

hook.dll is a 32-bit DLL. You can't load it into a 64-bit notepad.exe process, which is presumably what gets started when you run Notepad from your start menu etc.

Your script must be run with a 32-bit AutoHotkey, or otherwise the DllCalls to hook.dll would always fail. As such, Run Notepad means Run C:\Windows\system32\Notepad.exe which means Run C:\Windows\SysWOW64\Notepad.exe thanks to the wonders of WOW32 File System Redirection.

You can see if a 64-bit version of hook.dll exists and use that as appropriate, shove notes in that this will only work with 32-bit AutoHotkey, or ditch hook.dll and use AutoHotkey_H.dll with HotKeyIt's InjectAhkDll to run SetWindowsHookEx inside the Notepad.exe process yourself.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: arrondark, Bing [Bot], DRS and 137 guests