How to call an address from CallbackCreate in another script?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
eugenesv
Posts: 175
Joined: 21 Dec 2015, 10:11

How to call an address from CallbackCreate in another script?

27 Mar 2024, 10:18

Can I call a function created in one AHK script from another AHK script via a simple DllCall(fnAddress) that I've shared between these scripts? Or do I need to translate the address somehow?

(ultimately, this callback would be called from a non-ahk program, so I can't just #include it as I would in scripts)

When executing the simpified scripts below, I get Error: An exception was thrown. Specifically: 0xc0000005

Code: Select all

#Requires AutoHotKey 2.1-alpha
^1::share_cb() ; send callback address to another script

send_cb_to_ahk(addr) {
  MsgNum := DllCall("RegisterWindowMessage", "Str","test123")
  PostMessage(MsgNum, addr,0,, HWND_BROADCAST:=0xFFFF)
}
cbAhkTest() {
  return true
}
global addr_cbAhk := CallbackCreate(cbAhkTest) ;

share_cb() {
  OutputDebug("sent addr_cbAhk=" addr_cbAhk)
  send_cb_to_ahk(addr_cbAhk)
}

Code: Select all

#Requires AutoHotKey 2.1-alpha
^2::call_ahk() ; call the address we received previously from another script

global addr_cb_ahk := 0
MsgNum := DllCall("RegisterWindowMessage", "Str","test123")
OnMessage(MsgNum, SetGlobalCallbackAddr)
Persistent()

SetGlobalCallbackAddr(wParam, lParam, msg, hwnd) {
  global addr_cb_ahk
  addr_cb_ahk := wParam
  OutputDebug("received addr_cb_ahk=" addr_cb_ahk)
}

call_ahk(){
  if addr_cb_ahk = 0 {
    OutputDebug("✗✗✗ callback isn't ready yet!")
  } else {
    OutputDebug("calling callback in another script @" addr_cb_ahk)
    DllCall(addr_cb_ahk,"ptr")
  }
}
Last edited by gregster on 27 Mar 2024, 20:24, edited 1 time in total.
Reason: Moved topic from 'Scripts and Functions (v2)'.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 142 guests