Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[AHK_H(+dll) / AHK_L / v2] AhkDllThread+AhkExported


  • Please log in to reply
274 replies to this topic
HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

These functions will make it easy to use AutoHotkey_H.dll/exe using AHK_L object syntax.
AHK_H v1 (AutoHotkey.dll and AutoHotkey.exe) can be downloaded here and AHK_H v2 here.

AhkExported is used to access main AutoHotkey_H.exe functions
AhkDllThread is used to access AutoHotkey.dll functions, it loads each dll from Memory and makes it possible to load the same dll as many times as you like.

 

Download:
AhkExported.ahk - or - AhkExported.ahk (v2 version)
AhkDllThread.ahk - or - 
AhkDllThread.ahk (v2 version)

 

!! PLEASE NOTE !!

;When compiled, AhkDllThread.ahk assumes AutoHotkey[Mini].dll is installed in executable, e.g.:
If 0
   FileInstall,AutoHotkey.dll,For AhkDllThread() we don't need to extract that file
Example requires and GlobalVarsScript and GlobalVarsScript

Thanks SKAN for FileGetVersionInfo_AW.

Save all files in your [url=http://www.autohotkey.com/docs/Functions.htm#lib]lib folder. (GlobalVarsScript.ahk + AhkExported.ahk + AhkDllThread.ahk)


AHK_H Example.

EXE:=AhkExported()
DLL:=AhkDllObject("X:\AutoHotkey.dll")
MEM:=AhkDllThread("X:\AutoHotkey.dll")

GlobalVarsScript("EXE",0,EXE)
GlobalVarsScript("DLL",0,DLL)
GlobalVarsScript("MEM",0,MEM)

DLL.ahktextdll("#Persistent`n" GlobalVarsScript())
MEM.ahktextdll("#Persistent`n" GlobalVarsScript())

DLL.ahkassign.var:="value"
MEM.ahkassign.var:="anothervalue"
EXE.ahkassign.var:="main"

DLL.addscript("MEM.addscript(""ToolTip % EXE.ahkgetvar.var"",1)`nMsgBox % EXE.ahkfunction.func(1)",1)
MsgBox % "Main and threads (var) values:"
. "`n" EXE.ahkgetvar.var
. "`n" DLL.ahkgetvar.var
. "`n" MEM.ahkgetvar.var


func(var){
Return "returned from main function"
}

AHK_L example. (only AhkDllThread is compatible with AHK_L).

Loop 3
dll%A_Index%:=AhkDllThread(A_ScriptDir "\autohotkey.dll"),dll%A_Index%.ahkdll()

dll1.ahkExec("CriticalObject:=CriticalObject()`nhObj:=CriticalObject(CriticalObject,1)`nhCriticalSection:=CriticalObject(CriticalObject,2)")
dll2.ahkExec("CriticalObject:=CriticalObject(" (dll1.ahkgetvar.hObj+0) "," dll1.ahkgetvar.hCriticalSection ")")
dll3.ahkExec("CriticalObject:=CriticalObject(" (dll1.ahkgetvar.hObj+0) "," dll1.ahkgetvar.hCriticalSection ")")

dll1.addScript("Label:`nLoop 10000`nCriticalObject[A_Index]:=A_Index`nExitApp",1)
dll2.addScript("Label:`nLoop 10000`nToolTip % CriticalObject[A_Index],50`nExitapp")
dll3.addScript("Label:`nLoop 10000`nToolTip % CriticalObject[A_Index],100`nExitapp")

dll1.ahkLabel("Label",1)
Sleep 100
dll2.ahkLabel("Label",1)
dll3.ahkLabel("Label",1)

While % dll2.ahkReady() || dll3.ahkReady()
Sleep 100
ExitApp
Esc:=ExitApp


HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
AhkDllThread() and AhkDllObject() are now compatible with AHK_L as well :!:

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007

AhkDllObject(dll="AutoHotkey.dll",obj=0,p1="",p2="",p3="",p4="",p5="",p6="",p7="",p8="",p9="",p10=""){
;...
   If IsObject(dll) {
      If dll[""]=0
         Return DllCall(dll.f,dll.0) ;ahkReady, ahkReload
      else if dll[""]=1
         Return DllCall(dll.f,dll.1,p1,dll.0) ;ahkTerminate, ahkExec, ahkFindFunk, ahkFindLabel, ahkPause
      else if dll[""]=2
         Return DllCall(dll.f,dll.1,p1,dll.2,p2,dll.0) ;addScript, ahkassign, ahkgetvar, ahkLabel
      else if dll[""]=3
         Return DllCall(dll.f,dll.1,p1,dll.2,p2,dll.3,p3,dll.0) ;ahkdll, ahktextdll, addFile, ahkExecuteLine
      else if dll[""]=11
         Return DllCall(dll.f,dll.1,p1,dll.2,p2,dll.3,p3,dll.4,p4,dll.5,p5,dll.6,p6,dll.7,p7,dll.8,p8,dll.9,p9,dll.10,p10,dll.11,p11,dll.0)

Use variadic functions and variadic calling my friend.

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Thanks fincs, I have not thought about that :oops:
I have updated AhkDllObject above.

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Updated 64-bit support.

aSEioT
  • Members
  • 87 posts
  • Last active:
  • Joined: 31 Oct 2010

AhkDllThread() and AhkDllObject() are now compatible with AHK_L as well :!:

How can I run this in AHK_L(V2 especially), Thank you, :oops:

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
I have fixed AhkDllThread() and AhkExported() for v2 (it is included in Ahk_H v2alpha/lib/AhkDllThread.ahk + AhkExported.ahk)
Works now with AutoHotkey_Hv2alpha and AutoHotkey_Lv2alpha, AutoHotkey.dll v2alpha has to be used ;)

aSEioT
  • Members
  • 87 posts
  • Last active:
  • Joined: 31 Oct 2010
Thank you sir!
ps: Is it possible to get/copy an object direct from ahkdll or reverse? :lol:


And the

ahktextdll() ; default behavior

is not suitable for v2, due to the remove of "#Persistent"

aSEioT
  • Members
  • 87 posts
  • Last active:
  • Joined: 31 Oct 2010
Encounter some problems!
#Include AhkDllThread.ahk
AhkDll:=AhkDllThread()
AhkDll.ahktextdll("
(Join`r`n
#NoTrayIcon
#!@::           ; make the script persistent
Return
)")

Loop{                  ; when use the " while ahkdll.ahkready() sleep 10 " always fail"
  Sleep, 100
}until AhkDll.ahkReady()

p := AhkDll.addscript("              ; a little test
(Join`r`n
test:
   MsgBox here
return
func(){
  return "anything"
  }
)")
AhkDll.ahkExecuteLine(p,1)               ; not work
MsgBox % AhkDll.ahkExecuteLine(p,1)   ; this work
ahkDll.ahkLabel("test")                           ; ok
MsgBox % AhkDll.ahkFindFunc("func")  ; return nothing
test in the laterest ahk_l v2 :cry:

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

And the

ahktextdll() ; default behavior

is not suitable for v2, due to the remove of "#Persistent"

Thanks, did not notice that yet :oops:

#Include AhkDllThread.ahk
AhkDll:=AhkDllThread()
AhkDll.ahktextdll("#NoTrayIcon`n~VKFF::Return") ; [color=red]This will be the default script for next release[/color]
; MsgBox ahkdll.ahkReady() ; uncomment to see that ahktextdll already waits for AutoHotkey.dll to start up.
While [color=red]![/color]ahkdll.ahkReady() ; [color=red]When you use While ahkdll.AhkReady(), it will wait until script exits, which will never happen :)[/color]
  Sleep, 100	

p := AhkDll.addscript("              ; a little test
(Join`r`n
test:
   MsgBox here
return
func(){
  return "anything"
  }
)")
AhkDll.ahkExecuteLine(p,1)               ; [color=red]Works fine for me[/color]
Sleep 2000
MsgBox % AhkDll.ahkExecuteLine(p,1)   ; this work
ahkDll.ahkLabel("test")                           ; ok
Sleep 2000
MsgBox % AhkDll.ahkFindFunc("func")  ; [color=red]Will be fixed in next version[/color]


aSEioT
  • Members
  • 87 posts
  • Last active:
  • Joined: 31 Oct 2010

AhkDll.ahkExecuteLine(p,1) ; Works fine for me

Ok! I just realize it due to the main script was exit before the dll thread execute them!
Thanks you!

btw: Is anyway to put the A_ScriptDir to the space of ahkdll thread, I just use some thing like
ahkdll.ahkassign.ScriptDir := A_ScriptDir
If the ahkdll can default use the main script "build in var" will be great!

  • Guests
  • Last active:
  • Joined: --
If you need access to main script variables you can use AutoHotkey_H.exe and AhkExported so from dll you can access the main script same way as you access dll.
exe:=AhkExported()

MsgBox % exe.ahkgetvar.A_ScriptDir


aSEioT
  • Members
  • 87 posts
  • Last active:
  • Joined: 31 Oct 2010
Another question:
How can define "AStr" in ahkdll's DynaCall (just found "s" stand for "str") :?:

  • Guests
  • Last active:
  • Joined: --
A = AStr

W = WStr


HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Thanks for pointing out, I have updated DynaCall Docs