DynaCall()

Auto Hotkey

DynaCall

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

OutputVar := DynaCall("DllFile\Function", "ParameterDefinition", Default1, Default2, Default3, ...)
Function Example: TrueSleep := DynaCall("kernel32\Sleep", "ui", 100)
Calling the Func: TrueSleep[1000], %TrueSleep%(1000)

Parameters

OutputVar

The name of the variable in which to store the DynaCall object which is used to call the dll function.

[DllFile\]Function

The DLL or EXE file name followed by a backslash and the name of the function. For example: "MyDLL\MyFunction" (".dll" is default and can be omitted). If an absolute path isn't specified, DllFile is assumed to be in the system's PATH or A_WorkingDir.

DllFile may be omitted when calling a function that resides in User32.dll, Kernel32.dll, ComCtl32.dll, or Gdi32.dll. For example, "User32\IsWindowVisible" produces the same result as "IsWindowVisible".

If no function can be found by the given name, a "W" (Unicode) suffix is automatically appended. For example, "MessageBox" is the same as "MessageBoxW".

This parameter may also consist solely of an an integer, which is interpreted as the address of the function to call. Sources of such addresses include COM and RegisterCallback.

ParameterDefinition

For definition you will need to use the short version of DllCall types.

DllCall DynaCall equivalent
Inti
Strs
AStra
WStrw
Shorth
Charc
Floatf
Doubled
PTRt
Int64i6
CDecl Use == instead of =, for example "t==uis".
U prefix and * or p This is supported as well, for example: "ui=ui*s", "ui=uips"

The syntax for parameter definition is [  Return type   =[=]    ]   ParamType    ParamType    ParamType   ...
You can have any amount of space or tabs between parameters, those will be simply ignored.

  • Return type and = or == are optional and can be left out.

Changing order of parameters

    It is possible to change the order of parameters to use when the function is called.
    Therefore we need to use an object for parameter definition with following syntax.
      [ definition, FirstParam, SecondParam, ...]
    • Definition - Same as above
    • FirstParam - First parameter to use when function is called
    • SecondParam - Second parameter to use when function is called
    • This is best explained in following example.

      AHKCMD:=DynaCall("SendMessage", ["t=tuitt", 3], A_ScriptHwnd, 0x111)
      MsgBox % A_IsSuspended
      AHKCMD[ 65305 ] ; suspend script, following is the same call:
      ; DllCall("SendMessage", "PTR", A_ScriptHwnd, "UInt", 0x111, "PTR", 65305, "PTR", 0)

      MsgBox % A_IsSuspended
Default Value

The default value to use when the parameter is omitted in function call.
Note, this will be always the original order of parameters as specified in ParameterDefinition.

Examples

AHKCMD:=DynaCall("SendMessage", ["t=tuitt", 3], A_ScriptHwnd, 0x111) ; define a DynaCall object.
AHKCMD[ 65305 ] ; call the actual function
AHKCMD.65305
%AHKCMD%(65305)
AHKCMD.PTR( 65305 ) ; here (PTR) we can specify any AHK return type.

Related

DllCall, #DllImport, WinApi