Dllcall() uesed variable not works

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cgx5871
Posts: 315
Joined: 26 Jul 2018, 14:02

Dllcall() uesed variable not works

Post by cgx5871 » 05 Feb 2023, 16:42

Code: Select all

if (A_PtrSize = 8)
	throw "Please run the script using 32-bit AutoHotkey"

SetWorkingDir, %A_ScriptDir%
Run, Notepad,,, pid
WinWait, % "ahk_pid " pid
hWnd := WinExist()

AppendMenu(hWnd, 1000, "Click Me")

hook1 := new HookMsg(hWnd, 0x111)
OnMessage(0x111, "WM_COMMAND")
Return

WM_COMMAND(wParam, lParam, msg, hwnd) {
	If (wParam=1000)
		MsgBox, Clicked
}

AppendMenu(hWnd, ItemID, ItemName) {
	hMenu := DllCall("GetMenu", "Ptr", hWnd, "Ptr")
	DllCall("AppendMenu", "Ptr", hMenu, "UInt", 0x0, "UPtr", ItemID, "Str", ItemName)
	DllCall("DrawMenuBar", "Ptr", hWnd)
}

class HookMsg
{
	__New(hWnd, MsgNumber)
	{
		; hook.dll -- https://www.autoitscript.com/forum/applications/core/interface/file/attachment.php?id=7166
		dll:=A_MyDocuments "\AutoHotkey\Lib\Plugins\hook.dll"		
		if !FileExist(dll)
			throw "hook.dll is missing"
		static _ := DllCall("LoadLibrary", "Str",A_MyDocuments "\AutoHotkey\Lib\Plugins\hook.dll", "Ptr")
		static WH_CALLWNDPROC := 4
		     , WH_GETMESSAGE := 3
		     , UM_ADDMESSAGE := (WM_USER := 1024) + 0x100

		this.hWnd := hWnd
		this.iThreadIdTarget := DllCall("GetWindowThreadProcessId", "Ptr", this.hWnd, "Ptr", 0)
		hook := DllCall("hook\InstallFilterDLL", "Int", WH_CALLWNDPROC, "Int", this.iThreadIdTarget, "Ptr", this.hWnd) ; 0 = Ok
		hookG := DllCall("hook\InstallFilterDLL", "Int", WH_GETMESSAGE, "Int", this.iThreadIdTarget, "Ptr", this.hWnd) ; 0 = Ok
		DllCall("SendMessage", "Ptr", this.hWnd, "UInt", UM_ADDMESSAGE, "UPtr", MsgNumber, "Ptr", A_ScriptHwnd, "Ptr")
	}

	__Delete()
	{
		DllCall("hook\UnInstallFilterDLL", "Int", this.iThreadIdTarget, "Ptr", this.hWnd, "Ptr", A_ScriptHwnd)
	}
}
AppendMenu Script
------------------------------------------
dll:=A_MyDocuments "\AutoHotkey\Lib\Plugins\hook.dll"
static _ := DllCall("LoadLibrary", "Str",A_MyDocuments "\AutoHotkey\Lib\Plugins\hook.dll", "Ptr") ;'; works
static _ := DllCall("LoadLibrary", "Str",dll, "Ptr") ;'; not works
static _ := DllCall("LoadLibrary", "Str",%dll%, "Ptr") ;'; not works
------------------------------------------
why?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Dllcall() uesed variable not works

Post by swagfag » 05 Feb 2023, 17:32

u should read again in the docs what static variables are, how they work and how they get initialized. then it will become obvious why a local non-static variable cant be used to initialize a static one

Post Reply

Return to “Ask for Help (v1)”