#DllImport

Creates a script function for a dll or exe function.

#DllImport Function_Name, DllFile\Function , Type1, Arg1, Type2, Arg2, Cdecl ReturnType
Example: #DllImport ExecScript, %A_AhkPath%\ahkExec,Str,,CDecl

Parameters

Function_Name

The name of new function. See example.

[DllFile\]Function

The DLL or EXE file name followed by a backslash and the name of the function. For example: "MyDLL\MyFunction" (the file extension ".dll" is the default when omitted). If an absolute path isn't specified, DllFile is assumed to be in the system's PATH or A_WorkingDir. Following built-in variables can be used too: A_ScriptDir, A_AhkPath, A_DllPath, A_AppData, A_AppDataCommon.

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.
Note: 9MyFunction or 0MyDll.dll\Function are not supported because they will be assumed integers and will cause an exception to be trown.

This parameter may also be a hex string that represents the code.
Hex code for 64-bit can be appended after 32-bit code separated using colon (:) [32-bit hex]:[64-bit hex]. See also examples below.

Type1, Arg1 (optional)

Each of these pairs represents a single parameter to be passed to the function. The number of pairs is unlimited. For Type, see DllCall types table. Type can be specified without quotes.

For Arg, specify the default value to be passed to the function. This parameters will be only used if correspoinding parameter is omitted by function call. If Arg is a string it must not be enclosed in quotes ("") and all parameters starting with integer will be converted to digits, hexadecimal values are supported too.

Cdecl ReturnType (optional)

The word Cdecl is normally omitted because most functions use the standard calling convention rather than the "C" calling convention (functions such as wsprintf that accept a varying number of arguments are one exception to this). Note that most object-oriented C++ functions use the thiscall convention, which is not supported.

If present, the word Cdecl should be listed before the return type (if any). Separate each word from the next with a space or tab. For example: "Cdecl Str".

Since a separate "C" calling convention does not exist in 64-bit code, Cdecl may be specified but has no effect on 64-bit builds of AutoHotkey.

ReturnType: If the function returns a 32-bit signed integer (Int), BOOL, or nothing at all, ReturnType may be omitted. Otherwise, specify one of the argument types from the DllCall types table. The asterisk suffix is also supported.

General Remarks

String parameters for Arg must not be enclosed in quotes (""). Parameters starting with number are assumed numbers, so for example 0ABC will be considered 0. Hexadecimal values are allowed too, so 0xA will be converted to 10.

Related

DynaCall, WinApi

Examples

#DllImport, SendMsg,user32\SendMessage,PTR,0,UInt,0x999,PTR,111,PTR,222,PTR
OnMessage(0x999,"Ox999")

; DllCall("SendMessage","PTR",A_ScriptHwnd,"UINT",0x999,"PTR",111,"PTR",222,"PTR")
SendMsg(A_ScriptHwnd)
SendMsg ,A_ScriptHwnd

; DllCall("SendMessage","PTR",A_ScriptHwnd,"UINT",0x999,"PTR",1,"PTR",2,"PTR")
SendMsg(A_ScriptHwnd,,1,2) 
SendMsg ,A_ScriptHwnd,,1,2
ExitApp

Ox999(w:=0,l:=0,m:=0,h:=0){
  MsgBox w "`n" l "`n" m "`n" h
  return 1
}

; Import RGB_TO_BGR function as hex (separated by :), first hex is 32-bit and second hex 64-bit version.
; DWORD RGB_TO_BGR(COLORREF rgb){
; 	return ((rgb & 255) << 16) | (((rgb >> 8) & 255) << 8) | (rgb >> 16);
; }
#DllImport,RGB_TO_BGR,8B4C24040FB6C18BD1C1E01081E200FF00000BC2C1E9100BC1C3:0FB6C18BD1C1E910C1E01081E200FF00000BC20BC1C3,UInt,,CDecl UInt
MsgBox format("0x{1:X}",RGB_TO_BGR(0xAABBCC))