Jump to content


Photo

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


  • Please log in to reply
175 replies to this topic

#1 HotKeyIt

HotKeyIt
  • Fellows
  • 6129 posts

Posted 23 July 2010 - 09:20 PM

These functions will make it easy to use AutoHotkey_H.dll/exe using AHK_L object syntax.
AHK_H (AutoHotkey.dll and AutoHotkey.exe) can be downloaded 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 AutoHotkey_H.exe and GlobalVarsScript

Thanks SKAN for FileGetVersionInfo_AW.

Save all files in your 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


#2 HotKeyIt

HotKeyIt
  • Fellows
  • 6129 posts

Posted 31 October 2010 - 02:31 PM

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

#3 fincs

fincs
  • Fellows
  • 1529 posts

Posted 31 October 2010 - 02:52 PM

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.

#4 HotKeyIt

HotKeyIt
  • Fellows
  • 6129 posts

Posted 31 October 2010 - 04:19 PM

Thanks fincs, I have not thought about that :oops:
I have updated AhkDllObject above.

#5 HotKeyIt

HotKeyIt
  • Fellows
  • 6129 posts

Posted 01 October 2011 - 08:56 AM

Updated 64-bit support.

#6 aSEioT

aSEioT
  • Members
  • 87 posts

Posted 31 October 2011 - 09:07 AM

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

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

#7 HotKeyIt

HotKeyIt
  • Fellows
  • 6129 posts

Posted 03 November 2011 - 06:50 AM

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 ;)

#8 aSEioT

aSEioT
  • Members
  • 87 posts

Posted 03 November 2011 - 01:43 PM

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"

#9 aSEioT

aSEioT
  • Members
  • 87 posts

Posted 03 November 2011 - 04:12 PM

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:

#10 HotKeyIt

HotKeyIt
  • Fellows
  • 6129 posts

Posted 03 November 2011 - 08:39 PM

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]


#11 aSEioT

aSEioT
  • Members
  • 87 posts

Posted 04 November 2011 - 04:53 AM

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!

#12 Guests

  • Guests

Posted 04 November 2011 - 05:04 AM

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


#13 aSEioT

aSEioT
  • Members
  • 87 posts

Posted 04 November 2011 - 12:25 PM

Another question:
How can define "AStr" in ahkdll's DynaCall (just found "s" stand for "str") :?:

#14 Guests

  • Guests

Posted 04 November 2011 - 02:01 PM

A = AStr

W = WStr


#15 HotKeyIt

HotKeyIt
  • Fellows
  • 6129 posts

Posted 04 November 2011 - 02:23 PM

Thanks for pointing out, I have updated DynaCall Docs