Function Reference
DllCall
Dynamically calls a function in a DLL.
Parameters
| dll |
The filename of the DLL to use. e.g. "user32.dll". A handle obtained from DllOpen can also be used (See Remarks). |
| return type |
The return type of the function (see below). |
| function |
The name, eg. "MessageBox" or the ordinal value, e.g. 62, of the function in the DLL to call. |
| type1 |
[optional] The type of the parameter (see remarks). |
| param1 |
[optional] The actual parameter (see remarks). |
| type n |
[optional] The type of the nth parameter (see remarks). |
| param n |
[optional] The actual nth parameter (see remarks). |
Valid Types are:
| Type |
Details |
| none |
no value (only valid for return type - equivalent to void in C) |
| BYTE |
an unsigned 8 bit integer |
| BOOLEAN |
an unsigned 8 bit integer |
| short |
a 16 bit integer |
| USHORT |
an unsigned 16 bit integer |
| WORD |
an unsigned 16 bit integer |
| int |
a 32 bit integer |
| long |
a 32 bit integer |
| BOOL |
a 32 bit integer |
| UINT |
an unsigned 32 bit integer |
| ULONG |
an unsigned 32 bit integer |
| DWORD |
an unsigned 32 bit integer |
| INT64 |
a 64 bit integer |
| UINT64 |
an unsigned 64 bit integer |
| ptr |
a general pointer (void *) |
| HWND |
a window handle (pointer) |
| HANDLE |
an handle (pointer) |
| float |
a single precision floating point number |
| double |
a double precision floating point number |
| INT_PTR, LONG_PTR, LRESULT, LPARAM |
an integer big enough to hold a pointer when running on x86 or x64 versions of AutoIt. |
| UINT_PTR, ULONG_PTR, DWORD_PTR, WPARAM |
an unsigned integer big enough to hold a pointer when running on x86 or x64 versions of AutoIt. |
| str |
an ANSI string (a minimum of 65536 chars is allocated). |
| wstr |
a UNICODE wide character string (a minimum of 65536 chars is allocated). |
| struct |
structure created with DllStructCreate() |
| * |
Add * to the end of another type to pass it by reference. For example "int*" passes a pointer to an "int" type. |
Conversions from Windows API types to AutoIt types:
| WINDOWS API Type |
AutoIt Type |
| LPCSTR/LPSTR |
str |
| LPCWSTR/LPWSTR |
wstr |
| LPVOID |
ptr |
| LPxyz |
xyz* |
| HINSTANCE |
handle |
| HRESULT |
long |
| LONGLONG/LARGE_INTEGER |
INT64 |
| ULONGLONG/ULARGE_INTEGER |
UINT64 |
| SIZE_T |
ULONG_PTR |
To use nested structures inside a structure you must re-define the nested structure. For example, a structure containing 2 POINT structures ("long;long") would be declared as "long;long;long;long". The first two long values correspond to the first POINT structure and the second two values correspond to the second POINT structure.
For more Windows API types see MSDN.
Return Value
| Success: |
@error = 0. |
| Failure: |
set @error |
| @error: |
1 unable to use the DLL file, |
|
2 unknown "return type", |
|
3 "function" not found in the DLL file, |
|
4 bad number of parameters, |
|
5 bad parameter. |
See remarks.
Remarks
If a dll filename is given then the DLL is automatically loaded and then closed at the end of the call. If you want to manually control the loading and unloading of the DLL then you should use DllOpen and DllClose and use a handle instead of a filename in this function.
By default, AutoIt uses the 'stdcall' calling method. To use the 'cdecl' method place ':cdecl' after the return type.
DllCall("SQLite.dll", "int:cdecl", "sqlite3_open", "str", $sDatabase_Filename , "long*", 0).
By default, AutoIt tries to use the ANSI version of a function name, i.e. MessageBoxA is attempted when MessageBox is given as the function name. To call the unicode version use MessageBoxW.
If the function call fails then @error is set to 1. Otherwise an array is returned that contains the function return value and a copy of all the parameters (including parameters that the function may have modified when passed by reference).
$return[0] = function return value
$return[1] = param1
$return[2] = param2
...
$return[n] = paramn
Related
DllCallbackFree, DllCallbackGetPtr, DllCallbackRegister, DllOpen, DllClose, DllStructCreate, DllStructGetPtr
Example
; *******************************************************
; Example 1 - calling the MessageBox API directly
; *******************************************************
Local $result = DllCall("user32.dll", "int", "MessageBox", "hwnd", 0, "str", "Some text", "str", "Some title", "int", 0)
; *******************************************************
; Example 2 - calling a function that modifies parameters
; *******************************************************
Local $hwnd = WinGetHandle("[CLASS:Notepad]")
$result = DllCall("user32.dll", "int", "GetWindowText", "hwnd", $hwnd, "str", "", "int", 32768)
MsgBox(0, "", $result[0]) ; number of chars returned
MsgBox(0, "", $result[2]) ; Text returned in param 2
; *******************************************************
; Example 3 - Show the Windows PickIconDlg
; *******************************************************
Local $sFileName = @SystemDir & '\shell32.dll'
; Create a structure to store the icon index
Local $stIcon = DllStructCreate("int")
Local $stString = DllStructCreate("wchar[260]")
Local $structsize = DllStructGetSize($stString) / 2
DllStructSetData($stString, 1, $sFileName)
; Run the PickIconDlg - '62' is the ordinal value for this function
DllCall("shell32.dll", "none", 62, "hwnd", 0, "ptr", DllStructGetPtr($stString), "int", $structsize, "ptr", DllStructGetPtr($stIcon))
$sFileName = DllStructGetData($stString, 1)
Local $nIconIndex = DllStructGetData($stIcon, 1)
; Show the new filename and icon index
MsgBox(0, "Info", "Last selected file: " & $sFileName & @LF & "Icon-Index: " & $nIconIndex)