DynaCall


Build in function, similar to DllCall but works with DllCall structures and Object syntax.
It is faster than DllCall, easier to use and saves a lot of typing and code.

objfunc:=DynaCall(FilePath . "\function",[parameter_definition,default_param1,default_param2,...])

objfunc.[DllCallReturnType](params...)

Parameters

Parameter

Description

function

Can be a function address, name of function or path\name for the function.
E.g. "GetProcAddress" or "kernel32\LoadLibrary"

parameter_definition

Int = i / Str = s / AStr = a / WStr = w / Short = h / Char = c / Float = f / Double = d / Ptr = t / Int64 = 6
U prefix and * or p is supported as well, for example: "ui=ui*s"
You can change order of parameters using an object declaration.
For example: DynaCall("SendMessage",["t=tuitt",3,2],hwnd,WM_ACTIVATE:=0x06)
So when calling the function first parameter is wParam and second Msg. See example below.

default_param

You can set the parameters already when a Token is created, that way you can call the function without passing parameters as these are alredy set. When default_value is not given, parameters will default to 0 or "".
CDecl calling convention can be changed using ==, for example "t==ui".

DllCall_Return_Type

Optional. Using this you can override default return type previously defined in parameter_definition.
Use same return types as in DllCall (Int/Str/AStr/WStr/Short/Char/Float/Double/Ptr/Int64).

How to call the function

Any Object syntax can be used to call the function. For example:

objfunc[params...]
objfunc.param
objfunc.(params...) ; NOTE it is not objfunc(params...)
objfunc.param1 := param2
objfunc.Str(params...) ; here we can use a different return type than defined in defnition.

Return Value

objfunc will contain the DynaToken object that can be used to call the dll function using any object syntax.

Remarks

DynaCall uses same ErrorLevel as DllCall.
DynaCall can be also used to call the function, therefore pass a DynaToken that was created via DynaCall previously, followed by parameters. E.g. DynaCall(objfunc,parameters...).

Related

AutoHotkey.dll, ahkdll, ahktextdll, ahkReady, addFile, addScript, ahkExec, ahkLabel, ahkFunction, ahkPostFunction, ahkassign, ahkgetvar, ahkTerminate, ahkReload, ahkFindFunc, ahkFindLabel, ahkPause, ahkExecuteLine, Alias, cacheEnable, FindFunc, FindLabel, getTokenValue, getVar, Static, AutoHotkeyMini, CriticalSection, CriticalObject, MemoryLoadLibrary, ResourceLoadLibrary, MemoryGetProcAddress, MemoryFreeLibrary, Other Changes

Example

SetWorkingDir % A_ScriptDir
DllCall("LoadLibrary","Str",dll:="AutoHotkey.dll") ;Load AutoHotkey.dll
;Create an Object that will hold all DllCall Tokens
ahk:=Object( "run1",DynaCall(dll . "\ahktextdll","ui=sss","MsgBox run1")
,"run2",DynaCall(dll . "\ahktextdll","ui=sss","MsgBox run2")
,"get",DynaCall(dll . "\ahkgetvar","s=sui","a")
,"set",DynaCall(dll . "\ahkassign","ui=ss") )
ahk.run1() ;run ahktextdll with default paramers (here first parameter was set when run1 was created
ahk.set.a := "test" ;same as ahk.set("a","test") or ahk.set["a","test"]
MsgBox % ahk.get() ;get variable a, first parameter was set to "a" when get was created
ahk.run2() ;same as run1 but a different DynaCall object
ahk.set.a := "var"
MsgBox % ahk.get.a  ;"a" will be passed as parameter to get (this is only possible with 1 parameter)
MsgBox % ahk.get["a"] "`n" ahk.get("a") ;same as above

;---------------------------------------------------------

ms := new _MouseSpeed

ms.get[OrigMouseSpeed] ; get origial mouse speed

InputBox,NewSpeed,Enter New Mouse Speed,Current Speed: %OrigMouseSpeed%
ms.set[NewSpeed] ; set/write new speed

ms.get[OrigMouseSpeed] ; get/read new speed
MsgBox New Speed was changed to %OrigMouseSpeed%

Class _MouseSpeed {
    Get:=DynaCall("SystemParametersInfo",["uiuiui*ui",3],SPI_GETMOUSESPEED := 0x70)
    Set:=DynaCall("SystemParametersInfo",["uiuitui",3],SPI_SETMOUSESPEED := 0x71)
}