Can AutoHotkey.dll include a function for external calls? Topic is solved

Ask for help, how to use AHK_H, etc.
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Can AutoHotkey.dll include a function for external calls?

15 Oct 2021, 06:39

I am moving to AHK V2
But there are many LIBs that are based on V1

I want AutoHotkey.dll to include a LIB
and then in V2 DllCall to pass in the parameters and get the return value

For example, a lib file called test.ahk contains something like this

Code: Select all

test(v1, v2)
{
    if v1 + v2 = 3
        Return test2()
    Else
        Return 0
}

test2()
{
    Return 1 + 2
}

Then call it like this in V2
I don't know much about AutoHotkey.dll, is there a way to implement such a function and the correct way to do it?

Code: Select all

hThread := DllCall("LoadLibrary","Str","AutoHotkey.dll") 
MsgBox AHKV2(1, 2)

AHKV2(v1, v2)
{
    DllCall("AutoHotkey.dll\ahktextdll", "Str", "#Persistent", "Str", "", "Str", "")
    DllCall("AutoHotkey.dll\ahktextdll", "Str", "#Include test.ahk", "Str", "", "Str", "")
    DllCall("AutoHotkey.dll\ahkExec", "Str", "result := test(" v1 "," v2 ")")
    return DllCall("AutoHotkey\ahkgetvar", "Str", "result", "UInt", 0, "CDecl Str")
}
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Can AutoHotkey.dll include a function for external calls?  Topic is solved

15 Oct 2021, 18:38

You can call the function directly, no need to use ahkExec.
Also no need to load the script every time you call the function, load once, call as often as you want.
You can also create Bound functions for different amount of parameters to use for all functions:

Code: Select all

script:=
(
"#Persistent
test(v1, v2)
{
    if v1 + v2 = 3
        Return test2()
    Else
        Return 0
}

test2()
{
    Return 1 + 2
}"
)
hDll := DllCall("LoadLibrary","Str",dll:="D:\Scratch\Program Files\AutoHotKey\AutoHotkey 1\x64w\AutoHotkey.dll","Ptr") 
DllCall(dll "\ahktextdll", "Str", script, "Str", "", "Str", "")
ahkFunction:=DllCall.Bind(hahkFunction:=DllCall("GetProcAddress","Ptr",hDll,"AStr","ahkFunction","PTR"),"Str",,"Str","","Str",","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str") ; no params
ahkFunction1:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str") ; 1 param
ahkFunction2:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str") ; 2 params
ahkFunction3:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str",,"Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str") ; 3 params
ahkFunction4:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str",,"Str",,"Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str") ; 4 params
ahkFunction5:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str","","Str","","Str","","Str","","Str","","CDecl Str") ; 5 params
ahkFunction6:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str","","Str","","Str","","Str","","CDecl Str") ; 6 params
ahkFunction7:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str","","Str","","Str","","CDecl Str") ; 7 params
ahkFunction8:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str","","Str","","CDecl Str") ; 8 params
ahkFunction9:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str","","CDecl Str") ; 9 params
ahkFunction10:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"Str",,"CDecl Str") ; 10 params
MsgBox ahkFunction2("test","1","2")
MsgBox ahkFunction2("test","4","5")
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Can AutoHotkey.dll include a function for external calls?

16 Oct 2021, 01:31

@HotKeyIt
Thank you very much!!!

A simple script could do this
But for complex ones I have to include

Are there some features that are needed on an ongoing basis? How should they be implemented?
I've added #Persistent but it doesn't seem to work
For example
Works when MsgBox OSDTIP is displayed, exits when closed
If called like the following it will simply exit

OSDTIP viewtopic.php?f=6&t=76881

Code: Select all

hDll := DllCall("LoadLibrary","Str",dll:="D:\OneDrive\Program\AHK\ahkdll-v1\x64w\AutoHotkey.dll","Ptr")
DllCall(dll "\ahktextdll", "Str", "#Persistent `n #NoTrayIcon `n #include D:\OSDTIP.ahk", "Str", "", "Str", "")
ahkFunction:=DllCall.Bind(hahkFunction:=DllCall("GetProcAddress","Ptr",hDll,"AStr","ahkFunction","PTR"), "Str",,"Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str")  ; no params
OSDTIP:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str") ; 2 params

MsgBox OSDTIP("OSDTIP_Desktop","MainText","SubText")
OSDTIP("OSDTIP_Desktop","MainText","SubText") ;simply exit

This works directly in the V1 script Persistent

Code: Select all

#Include D:\OSDTIP.ahk
OSDTIP_Desktop("MainText", "SubText")
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Can AutoHotkey.dll include a function for external calls?

16 Oct 2021, 01:48

I am very sorry for my mistake
I just remember to add Persistent to DllCall
Forgot to add in the current script
Persistent should be added to the current script

Code: Select all

Persistent
hDll := DllCall("LoadLibrary","Str",dll:="D:\OneDrive\Program\AHK\ahkdll-v1\x64w\AutoHotkey.dll","Ptr")
DllCall(dll "\ahktextdll", "Str", "#NoTrayIcon `n#include D:\OSDTIP.ahk", "Str", "", "Str", "")
hahkFunction := DllCall("GetProcAddress", "Ptr", hDll, "AStr", "ahkFunction", "PTR")
OSDTIP:=DllCall.Bind(hahkFunction,"Str",,"Str",,"Str",,"Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str") ; 2 params
OSDTIP("OSDTIP_Desktop","MainText","SubText")
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: Can AutoHotkey.dll include a function for external calls?

20 Oct 2021, 07:28

Thanks for this!

Still wading my way through _H/V2.

What if the external 'lib' i wanted to work with was a class? Can we still use the ahkdll to work with the class methods?
viv
Posts: 217
Joined: 09 Dec 2020, 17:48

Re: Can AutoHotkey.dll include a function for external calls?

20 Oct 2021, 08:43

@HotKeyIt
I have simply folded it to make it easier to use in V2
viewtopic.php?f=83&t=95656

But there is an error when working with ACC
viewtopic.php?p=425247#p425247

There is also a related script here
viewtopic.php?f=6&t=3702

my code

Code: Select all

Persistent
hDll := DllCall("LoadLibrary","Str",dll:="D:\OneDrive\Program\AHK\ahkdll-v1\x64w\AutoHotkey.dll","Ptr")
DllCall(dll "\ahktextdll", "Str", "#include D:\geturl.ahk", "Str", "", "Str", "")
hahkFunction := DllCall("GetProcAddress", "Ptr", hDll, "AStr", "ahkFunction", "PTR")
ahkFunction:=DllCall.Bind(hahkFunction:=DllCall("GetProcAddress","Ptr",hDll,"AStr","ahkFunction","PTR"),"Str",,"Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","Str","","CDecl Str") ; no params

Sleep 2000 ;sleep for change to chrome
url := ahkFunction("GetActiveBrowserURL")
MsgBox url
it will stuck on chrome( work fine in ie)
In the case of stuck, I use the shortcut keys that come with the script and it works normally
Can I trouble you to see if there is any problem?
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Can AutoHotkey.dll include a function for external calls?

20 Oct 2021, 11:27

ahkFunction uses SendMessage and COM does not support multi-threading over SendMessage, that might be the problem.
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: Can AutoHotkey.dll include a function for external calls?

22 Oct 2021, 13:01

Hey @HotKeyIt -

I'm running into an issue when using autohotkey.dll to load external libs/call their functions.

i'd say about 80% of the time all works great, but pretty reliably something will cause the libraries im loading from V2 namely cJSON and Mysql to fail to load.

ive tried everythiung i can think to make sure the dlls are unloaded/objects freed etc etc, and ive no idea why the script fails sometimes. any ideas would be great appreciated.

Here is the script if it helps...

Code: Select all

dll := A_ScriptDir "\AutoHotKey.dll"
#DllLoad "%A_ScriptDir%\AutoHotKey.dll"
#DllLoad "%A_ScriptDir%\lib\libmysql.dll"

OnExit ExitFunc
#Warn All, Off
#NoTrayIcon
#SingleInstance Force

#Include <JSON>
#Include <ADO>
#Include <V1Loader> ;Contains All V1 Function/Classes and helpers

User := EnvGet("username")
FirstLast := EnvGet("first.last")
UserEmail := EnvGet("testmail")
testphon := EnvGet("testphon")

V1L := IncludeLib("<V1_Libraries>",dll)
dll := A_ScriptDir "\AutoHotKey.dll"

Connection["Query"] := "SELECT * FROM innodb.Salesmen"
Connection["Key"] := "Number"
salesmandata := V1("dbread",Connection)
msgbox(salesmandata["27"]["Territorycode"])

Connection["Query"] := "SELECT * FROM innodb.ChromeUsers WHERE initials = '" user "'"
Connection["Key"] := "Initials"
Ports := V1("dbread",Connection)
msgbox(Ports[User]["chromeport"])


ExitApp(0)
ExitFunc(ExitReason, ExitCode)
{
    msgbox("Exit")
    V1L:=""
}

V1(func,param,ParseToObject:=""){
    rData := V1L.do(func,jxonencode(param))
    if(!ParseToObject)
        return(jxondecode(&rData))
    Else
        return(rData)
}
and the class to load V1 libs...

Code: Select all

Class IncludeLib
{

    __New(libPath, dllPath := "")
    {
		this.dllpath := dllPath
        if !FileExist(dllPath := dllPath ? dllPath : A_ScriptDir "\AutoHotkey.dll")
            MsgBox("Miss AutoHotkey.dll file"), ExitApp()
        This.hDll := DllCall("LoadLibrary", "Str", dllPath, "Ptr")
        DllCall(this.dllpath "\ahktextdll", "Str", "#Persistent `n#NoTrayIcon `n#include " libPath, "Str", "", "Str","","Cdecl Str")
        This.hahkFunction := DllCall("GetProcAddress", "Ptr", This.hDll, "AStr", "ahkFunction", "PTR")
    }

    do(FuncNmae, v*)
    {
        p := []
        Loop 10
            p.Push(v.Has(A_Index) ? v[A_Index] : "")
        ahkFunction := DllCall.Bind(this.hahkFunction, "Str", , "Str", p[1], "Str", p[2], "Str", p[3], "Str", p[4], "Str", p[5], "Str", p[6], "Str", p[7], "Str", p[8], "Str", p[9], "Str", p[10], "CDecl Str")
        
		temp := ahkFunction
		ahkFunction := ""
		return temp(FuncNmae)
    }
}
Im not using __Delete because im using #DllLoad in main script. In my testing using __Delete caused the script to often hang when trying to exit. #DllLoad atleast seems to allow the script to exit properly
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

Re: Can AutoHotkey.dll include a function for external calls?

22 Oct 2021, 14:25

Changing my main script to add a 500ms delay after i load the ahkdll libs seems to have all but solved the issues. does that make sense..? if so...is there a better way than using a sleep?

Code: Select all

;Directives
Persistent(true)
#Warn All, Off
#NoTrayIcon
#SingleInstance Force

;V2 Includes
#Include <JSON>
#Include <ADO>
#Include <V1Loader> ;Contains All V1 Function/Classes and helpers

;Preload DLL's
#DllLoad "%A_ScriptDir%\AutoHotKey32.dll"
#DllLoad "%A_ScriptDir%\lib\libmysql.dll"

dll := A_ScriptDir "\AutoHotkey32.dll"
V1L := IncludeLib("<V1_Libraries>",dll)
sleep(500)

User := EnvGet("username")
FirstLast := EnvGet("first.last")
UserEmail := EnvGet("testmail")
testphon := EnvGet("testphon")
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: Can AutoHotkey.dll include a function for external calls?

22 Oct 2021, 20:32

You can define a variable and wait until it is initialized so you know script is ready:
In ahk.dll:

Code: Select all

...
...
ready:=1
In ahk.exe

Code: Select all

While !DllCall(dll_path "\ahkgetvar","Str","ready","UInt",0,"Cdecl Str")
  Sleep 100

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 26 guests