DllCall()

http://www.autohotkey.com/docs/commands/DllCall.htm

HomepageCommand List


Calls a function inside a DLL, such as a standard Windows API function.

Result := DllCall("[DllFile/]Function" [, Type1, Arg1, Type2, Arg2, "Cdecl ReturnType"])

Parameters

Result

DllCall returns the actual value returned by the function. If the function is of a type that does not return a value, the result is an undefined integer. If the function cannot be called due to an error, the return value is blank (an empty string).

[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.

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". For these standard DLLs, the letter "A" suffix that appears on some API functions may also be omitted. For example, "MessageBox" is the same as "MessageBoxA".

Performance can be dramatically improved when making repeated calls to a DLL by loading it beforehand.

In v1.0.46.08+, 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().

Type1, Arg1

Each of these pairs represents a single parameter to be passed to the function. The number of pairs is unlimited. For Type, see the types table below. For Arg, specify the value to be passed to the function.

Cdecl ReturnType

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). If you omit Cdecl but the call yields ErrorLevel An -- where n is the total size of the arguments you passed -- Cdecl might be required.

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"

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 types table below. The asterisk suffix is also supported.

Types of Arguments and Return Values

Str

A string such as "Blue" or MyVar. If the called function modifies the string and the argument is a naked variable, its contents will be updated. For example, the following call would convert the contents of MyVar to uppercase: DllCall("CharUpper", "str", MyVar)

However, if the function is designed to store a string larger than a variable's current capacity, ensure that the variable is large enough before calling the function. This can be achieved by calling VarSetCapacity(MyVar, 123), where 123 is the length that MyVar must be able to hold.

A str argument must not be an expression that evaluates to a number (such as i+1). If it is, the function is not called and ErrorLevel is set to -2.

The str type should generally be used with functions that expect LPSTR, LPCSTR, LPTSTR, char *, and similar types.

The asterisk variable "str *" is supported but rarely used. It can be used with functions that expect something like "char **" or "LPSTR *".

Int64

A 64-bit integer, whose range is -9223372036854775808 (-0x8000000000000000) to 9223372036854775807 (0x7FFFFFFFFFFFFFFF).

Int

A 32-bit integer (the most common integer type), whose range is -2147483648 (-0x80000000) to 2147483647 (0x7FFFFFFF). An Int is sometimes called a "Long".

An Int should also be used for each BOOL argument expected by a function (a BOOL value should be either 1 or 0).

An unsigned Int (UInt) is also used quite frequently, such as for DWORD and COLORREF. It is also used for almost all handles, such as HWND, HBRUSH, and HBITMAP.

Note: To pass a NULL handle or pointer, pass the integer 0.

Short

A 16-bit integer, whose range is -32768 (-0x8000) to 32767 (0x7FFF). An unsigned Short (UShort) can be used with functions that expect a WORD.

Char

An 8-bit integer, whose range is -128 (-0x80) to 127 (0x7F). An unsigned character (UChar) can be used with functions that expect a BYTE.

Float

A 32-bit floating point number, which provides 6 digits of precision.

Double

A 64-bit floating point number, which provides 15 digits of precision.

* or P

(suffix)

Append an asterisk (with optional preceding space) to any of the above types to cause the address of the argument to be passed rather than the value itself (the called function must be designed to accept it). Since the value of such an argument might be modified by the function, whenever a naked variable is passed as the argument, that variable's contents will be updated. For example, the following call would pass the address of MyVar to MyFunction but would also update MyVar to reflect any changes made to it by MyFunction: DllCall("MyDll/MyFunction", "int *", MyVar)

In general, an asterisk is used whenever a function has an argument type or return type that starts with "LP" (except a string type such as LPSTR, for which "str" should be used). The most common example is LPDWORD, which is a pointer to a DWORD. Since a DWORD is an unsigned 32-bit integer, use "UInt *" or "UintP" to represent LPDWORD.

Note: "char *" is not the same as "str" because "char *" passes the address of an 8-bit number, but "str" passes the address of a series of characters. Also, it is not necessary to call VarSetCapacity on asterisk variables in which the function will store numbers.

U (prefix)

Prepend the letter U to any of the integer types above to interpret it as an unsigned integer (UInt64, UInt, UShort, and UChar). Strictly speaking, this is necessary only for return values and asterisk variables because it does not matter whether an argument passed by value is unsigned or signed (except for Int64).

A 32-bit unsigned integer (UInt) is an appropriate substitute for any DWORD, HWND, or similar argument expected by a function. Also, an HWND value (the handle of a window) is the same as the window's unique ID.

If a negative integer is specified for an unsigned argument, the integer wraps around into the unsigned domain. For example, when -1 is sent as a UInt, it would become 0xFFFFFFFF. However, this technique is not supported for 64-bit integers (UInt64).

Unsigned 64-bit integers produced by a function are not fully supported. Therefore, to work with numbers greater or equal to 0x8000000000000000, omit the U prefix and interpret any negative values received from the function as large integers. For example, a function that yields -1 as an Int64 is really yielding 0xFFFFFFFFFFFFFFFF if it is designed to yield a UInt64.

Note: When specifying an argument type or return type that does not contain a space or asterisk, the quotes around it may be omitted. For example, str can be used in place of "str" and CDecl in place of "CDecl". In addition, the letter P may be used in place of asterisk to allow the quotes to be omitted there as well. For example: UIntP.

ErrorLevel

ErrorLevel is set to one of the following values to indicate whether the call succeeded or failed.

0: Success.

-1 (negative 1): The [DllFile/]Function parameter is a floating point number. A string or positive integer is required.

-2: The return type or one of the specified arg types is invalid. This error can also be caused by passing an expression that evaluates to a number to a string (str) argument.

-3: The specified DllFile could not be accessed. If no explicit path was specified for DllFile, the file must exist in the system's PATH or A_WorkingDir. This error might also occur if the user lacks permission to access the file.

-4: The specified function could not be found inside the DLL.

N (any positive number): The function was called but it aborted with fatal exception number N (for example, 0xC0000005 means "access violation"). In such cases, the function returns a blank value (empty string), but any asterisk variables are still updated. An example of a fatal exception is dereferencing an invalid pointer such as NULL. Since a Cdecl function never produces the "An" error in the next paragraph, it may generate an exception when too few arguments are passed to it.

An (the letter A followed by an integer n): The function was called but was passed too many or too few arguments. "n" is the number of bytes by which the argument list was incorrect. If n is positive, too many arguments (or arguments that were too large) were passed, or the call requires CDecl. If n is negative, too few arguments were passed. This situation should be corrected to ensure reliable operation of the function. The presence of this error may also indicate that an exception occurred, in which case the function returns a blank value.

Exceptions and A_LastError

In spite of the built-in exception handling, it is still possible to crash a script with DllCall. This can happen when a function does not directly generate an exception but yields something inappropriate, such as a bad pointer or a string that is not terminated. This might not be the function's fault if the script passed it an unsuitable value such as a bad pointer or a "str" with insufficient capacity. A script can also crash when it specifies an inappropriate argument type or return type, such as claiming that an ordinary integer yielded by a function is an asterisk variable or str.

The built-in variable A_LastError contains the result of the operating system's GetLastError() function, which is called immediately after the function is called (this has no measurable impact on performance). A_LastError is a number between 0 and 4294967295 (always formatted as decimal, not hexadecimal). Like ErrorLevel, A_LastError is a per-thread setting; that is, interruptions by other threads cannot change it. However, A_LastError is also set by Run/RunWait.

Performance

When making repeated calls to a DLL, performance can be dramatically improved by loading it explicitly (this is not necessary for a standard DLL such as User32 because it is always resident). This practice avoids the need for DllCall to internally call LoadLibrary and FreeLibrary each time. For example:


hModule := DllCall("LoadLibrary", "str", "MyFunctions.dll") ; Avoids the need for DllCall() in the loop to load the library. Loop, C:/My Documents/*.*, , 1 result := DllCall("MyFunctions/BackupFile", "str", A_LoopFileFullPath) DllCall("FreeLibrary", "UInt", hModule) ; To conserve memory, the DLL may be unloaded after using it.


In v1.0.46.08+, even faster performance can be achieved by looking up the function's address beforehand. For example:

; In the following example, if the DLL isn't yet loaded, use LoadLibrary in place of GetModuleHandle.
MulDivProc := DllCall("GetProcAddress", uint, DllCall("GetModuleHandle", str, "kernel32"), str, "MulDiv")
Loop 500
    DllCall(MulDivProc, int, 3, int, 4, int, 3)

Also, adding the line #NoEnv anywhere in the script improves DllCall's performance when unquoted parameter types are used (e.g. int vs. "int").

Finally, when passing a string-variable to a function that will not change the length of the string, performance is improved by passing the variable by address (e.g. &MyVar) rather than as a "str" (especially when the string is very long). The following example converts a string to uppercase: DllCall("CharUpper", uint, &MyVar)

Structures and Arrays

A structure is a collection of members (fields) stored adjacently in memory. Most members tend to be integers.

Functions that accept the address of a structure (or a memory-block array) can be called by storing the structure's raw binary data in a normal variable. The following steps are generally used:

1) Call VarSetCapacity(MyStruct, 123, 0) to ensure that the target variable is large enough to hold the structure's data. Replace 123 with a number that is at least as large as the size of the structure. Specifying zero as the last parameter is optional; it initializes all members to be binary zero, which is typically used to avoid calling NumPut() as often in the next step.

2) If the target function uses the values initially in the structure, call NumPut(123, MyStruct, 4) to initialize any members that should be non-zero. Replace 123 with the integer to be put into the target member (or specify &Var to store the address of a variable). Replace 4 with the offset of the target member (see step #4 for description of "offset").

3) Call the target function, passing the address of MyStruct as a UInt argument. For example, DllCall("MyDll/MyFunc", UInt, &MyStruct). The function will examine and/or change some of the members.

4) Use MyInteger := NumGet(MyStruct, 4) to retrieve any desired integers from the structure. Replace 4 with the offset of the target member in the structure. The first member is always at offset 0. The second member is at offset 0 plus the size of the first member (typically 4). Members beyond the second are at the offset of the previous member plus the size of the previous member. Most members -- such as DWORD, Int, and other types of 32-bit integers -- are 4 bytes in size.

See Structure Examples for actual usages.

Known Limitations

When a variable's address (e.g. &MyVar) is passed to a function and that function alters the length of the variable's contents, subsequent uses of the variable may behave incorrectly. To fix this, do one of the following: 1) Pass MyVar as a "str" argument rather than as a uint/address; 2) In v1.0.44.03+, call VarSetCapacity(MyVar, -1) to update the variable's internally-stored length after calling DllCall.

Any binary zero stored in a variable by a function hides all data to the right of the zero; that is, such data cannot be accessed or changed by most commands and functions. However, such data can be manipulated by the address and dereference operators (& and *), as well as DllCall itself.

A function that returns the address of one of the strings that was passed into it might return an identical string in a different memory address than expected. For example calling CharLower(CharUpper(MyVar)) in a programming language would convert MyVar's contents to lowercase. But when the same is done with DllCall(), MyVar would be uppercase after the following call because CharLower would have operated on a different/temporary string whose contents were identical to MyVar:


MyVar = ABC result := DllCall("CharLower", str, DllCall("CharUpper", str, MyVar, str), str)


To work around this, change the two underlined "str" values above to UInt. This interprets CharUpper's return value as a pure address that will get passed as an integer to CharLower.

VBScript, JScript, and Component Object Model (COM)

VBScript and JScript may be embedded in a script via Windows Scripting for AutoHotkey, which also provides access to COM.

Also, COM may be used directly via DllCall as demonstrated at www.autohotkey.com/wiki/index.php?title=COM_Wrappers.

Related

PostMessage, OnMessage(), RegisterCallback(), Run, VarSetCapacity, Functions, SysGet, MSDN Library

Examples

; Example: Calls the Windows API function "MessageBox" and report which button the user presses.

WhichButton := DllCall("MessageBox", "int", "0", "str", "Press Yes or No", "str", "Title of box", "int", 4)
MsgBox You pressed button #%WhichButton%.

 

; Example: Changes the desktop wallpaper to the specified bitmap (.bmp) file.

DllCall("SystemParametersInfo", UInt, 0x14, UInt, 0, Str, A_WinDir . "/winnt.bmp", UInt, 2)

 

; Example: Calls the API function "IsWindowVisible" to find out if a Notepad window is visible.

DetectHiddenWindows On
if not DllCall("IsWindowVisible", "UInt", WinExist("Untitled - Notepad"))  ; WinExist() returns an HWND.
    MsgBox The window is not visible.

 

; Example: Calls the API's wsprintf() to pad the number 432 with leading zeros to make it 10 characters wide (0000000432).

VarSetCapacity(ZeroPaddedNumber, 20)  ; Ensure the variable is large enough to accept the new string.
DllCall("wsprintf", "str", ZeroPaddedNumber, "str", "%010d", "int", 432, "Cdecl")  ; Requires the Cdecl calling convention.
MsgBox %ZeroPaddedNumber%

 


; Example: Demonstrates QueryPerformanceCounter(), which gives more precision than A_TickCount's 10ms.


 

; Example: This is a hotkey that temporarily reduces the mouse cursor's speed, which facilitates precise positioning.
; Hold down the F1 key to slow down the cursor. Release it to return to original speed.

F1::
SPI_GETMOUSESPEED = 0x70
SPI_SETMOUSESPEED = 0x71
; Retrieve the current speed so that it can be restored later:
DllCall("SystemParametersInfo", UInt, SPI_GETMOUSESPEED, UInt, 0, UIntP, OrigMouseSpeed, UInt, 0)
; Now set the mouse to the slower speed specified in the next-to-last parameter (the range is 1-20, 10 is default):
DllCall("SystemParametersInfo", UInt, SPI_SETMOUSESPEED, UInt, 0, UInt, 3, UInt, 0)
KeyWait F1  ; This prevents keyboard auto-repeat from doing the DllCall repeatedly.
return

F1 up::DllCall("SystemParametersInfo", UInt, 0x71, UInt, 0, UInt, OrigMouseSpeed, UInt, 0)  ; Restore the original speed.

 


; Example: When passed a window's Unique ID and the text or ClassNN of one of its controls, ; the following function returns the HWND (unique ID) of that control. ; v1.0.43.06+: This function has been superseded by the following command, which is more accurate. ControlGet, OutputVar, Hwnd,, ClassNN, WinTitle


 

; Example: Monitors the active window and display the position of its vertical scroll bar in its
; focused control (with real-time updates). This requires v1.0.43.06+ because it uses ControlGet Hwnd.

#Persistent
SetTimer, WatchScrollBar, 100
return

WatchScrollBar:
ActiveWindow := WinExist("A")
if not ActiveWindow  ; No active window.
    return
ControlGetFocus, FocusedControl, ahk_id %ActiveWindow%
if not FocusedControl  ; No focused control.
    return
; Display the vertical or horizontal scroll bar's position in a ToolTip:
ControlGet, ChildHWND, Hwnd,, %FocusedControl%, ahk_id %ActiveWindow%
ToolTip % DllCall("GetScrollPos", "UInt", ChildHWND, "Int", 1)  ;  Last parameter is 1 for SB_VERT, 0 for SB_HORZ.
return

 


; Example: This is a working script that writes some text to a file then reads it back into memory (requires v1.0.34+). ; This method can be used to help performance in cases where multiple files are being read or written simultaneously. FileSelectFile, FileName, S16,, Create a new file: if FileName = return GENERIC_WRITE = 0x40000000 ; Open the file for writing rather than reading. CREATE_ALWAYS = 2 ; Create new file (overwriting any existing file). hFile := DllCall("CreateFile", str, FileName, Uint, GENERIC_WRITE, Uint, 0, UInt, 0, UInt, CREATE_ALWAYS, Uint, 0, UInt, 0) if not hFile { MsgBox Can't open "%FileName%" for writing. return } TestString = This is a test string.`r`n ; When writing a file this way, use `r`n rather than `n to start a new line. DllCall("WriteFile", UInt, hFile, str, TestString, UInt, StrLen(TestString), UIntP, BytesActuallyWritten, UInt, 0) DllCall("CloseHandle", UInt, hFile) ; Close the file. ; Now that the file was written, read its contents back into memory. GENERIC_READ = 0x80000000 ; Open the file for reading rather than writing. OPEN_EXISTING = 3 ; This mode indicates that the file to be opened must already exist. FILE_SHARE_READ = 0x1 ; This and the next are whether other processes can open the file while we have it open. FILE_SHARE_WRITE = 0x2 hFile := DllCall("CreateFile", str, FileName, UInt, GENERIC_READ, UInt, FILE_SHARE_READ|FILE_SHARE_WRITE, UInt, 0, UInt, OPEN_EXISTING, Uint, 0, UInt, 0) if not hFile { MsgBox Can't open "%FileName%" for reading. return } ; Make the variable empty for testing purposes, but ensure it retains sufficient capacity: BytesToRead := VarSetCapacity(TestString, StrLen(TestString)) DllCall("ReadFile", UInt, hFile, str, TestString, UInt, BytesToRead, UIntP, BytesActuallyRead, UInt, 0) DllCall("CloseHandle", UInt, hFile) ; Close the file.


 


; Example: Hides the mouse cursor when you press Win+C. To later show the cursor, press Win+C again. ; This script is from www.autohotkey.com/forum/topic6107.html OnExit, ShowCursor ; Ensure the cursor is made visible when the script exits. return ShowCursor: SystemCursor("On") ExitApp #c::SystemCursor("Toggle") ; Win+C hotkey to toggle the cursor on and off. SystemCursor(OnOff=1) ; INIT = "I","Init"; OFF = 0,"Off"; TOGGLE = -1,"T","Toggle"; ON = others { static AndMask, XorMask, $, h_cursor ,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13 ; system cursors , b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13 ; blank cursors , h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13 ; handles of default cursors if (OnOff = "Init" or OnOff = "I" or $ = "") ; init when requested or at first call { $ = h ; active default cursors VarSetCapacity( h_cursor,4444, 1 ) VarSetCapacity( AndMask, 32*4, 0xFF ) VarSetCapacity( XorMask, 32*4, 0 ) system_cursors = 32512,32513,32514,32515,32516,32642,32643,32644,32645,32646,32648,32649,32650 StringSplit c, system_cursors, `, Loop %c0% { h_cursor := DllCall( "LoadCursor", "uint",0, "uint",c%A_Index% ) h%A_Index% := DllCall( "CopyImage", "uint",h_cursor, "uint",2, "int",0, "int",0, "uint",0 ) b%A_Index% := DllCall("CreateCursor","uint",0, "int",0, "int",0 , "int",32, "int",32, "uint",&AndMask, "uint",&XorMask ) } } if (OnOff = 0 or OnOff = "Off" or $ = "h" and (OnOff < 0 or OnOff = "Toggle" or OnOff = "T")) $ = b ; use blank cursors else $ = h ; use the saved cursors


 


; Structure Example: Pass the address of a RECT structure to GetWindowRect(), which sets the structure's ; members to the positions of the left, top, right, and bottom sides of a window (relative to the screen). Run Notepad WinWait Untitled - Notepad ; This also sets the "last found window" for use with WinExist() below. VarSetCapacity(Rect, 16) ; A RECT is a struct consisting of four 32-bit integers (i.e. 4*4=16). DllCall("GetWindowRect", UInt, WinExist(), UInt, &Rect) ; WinExist() returns an HWND. MsgBox % "Left " . NumGet(Rect, 0, true) . " Top " . NumGet(Rect, 4, true) . " Right " . NumGet(Rect, 8, true) . " Bottom " . NumGet(Rect, 12, true)


 

; Structure Example: Pass to FillRect() the address of a RECT structure that indicates a part of the
; screen to temporarily paint red.

VarSetCapacity(Rect, 16, 0)  ; Set capacity to hold four 4-byte integers and initialize them all to zero.
NumPut(A_ScreenWidth//2, Rect, 8)  ; The third integer in the structure is "rect.right".
NumPut(A_ScreenHeight//2, Rect, 12) ; The fourth integer in the structure is "rect.bottom".
hDC := DllCall("GetDC", UInt, 0)  ; Pass zero to get the desktop's device context.
hBrush := DllCall("CreateSolidBrush", UInt, 0x0000FF)  ; Create a red brush (0x0000FF is in BGR format).
DllCall("FillRect", UInt, hDC, Str, Rect, UInt, hBrush)  ; Fill the specified rectangle using the brush above.
DllCall("ReleaseDC", UInt, 0, UInt, hDC)  ; Clean-up.
DllCall("DeleteObject", UInt, hBrush)  ; Clean-up.

 

; Structure Example: Change the system's clock to the specified date and time. Use caution when
; changing to a date in the future as it may cause scheduled tasks to run prematurely!

SetSystemTime("20051008142211")  ; Pass it a timestamp (local, not UTC).

SetSystemTime(YYYYMMDDHHMISS)
; Sets the system clock to the specified date and time.
; Caller must ensure that the incoming parameter is a valid date-time stamp
; (local time, not UTC). Returns non-zero upon success and zero otherwise.
{
    ; Convert the parameter from local time to UTC for use with SetSystemTime().
    UTC_Delta -= %A_NowUTC%, Seconds  ; Seconds is more accurate due to rounding issue.
    UTC_Delta := Round(-UTC_Delta/60)  ; Round to nearest minute to ensure accuracy.
    YYYYMMDDHHMISS += %UTC_Delta%, Minutes  ; Apply offset to convert to UTC.

    VarSetCapacity(SystemTime, 16, 0)  ; This struct consists of 8 UShorts (i.e. 8*2=16).

    StringLeft, Int, YYYYMMDDHHMISS, 4    ; YYYY (year)
    NumPut(Int, SystemTime, 0, "UShort")
    StringMid, Int, YYYYMMDDHHMISS, 5, 2  ; MM (month of year, 1-12)
    NumPut(Int, SystemTime, 2, "UShort")
    StringMid, Int, YYYYMMDDHHMISS, 7, 2  ; DD (day of month)
    NumPut(Int, SystemTime, 6, "UShort")
    StringMid, Int, YYYYMMDDHHMISS, 9, 2  ; HH (hour in 24-hour time)
    NumPut(Int, SystemTime, 8, "UShort")
    StringMid, Int, YYYYMMDDHHMISS, 11, 2 ; MI (minute)
    NumPut(Int, SystemTime, 10, "UShort")
    StringMid, Int, YYYYMMDDHHMISS, 13, 2 ; SS (second)
    NumPut(Int, SystemTime, 12, "UShort")

    return DllCall("SetSystemTime", UInt, &SystemTime)
}

 

More Structure Examples:

1) See the WinLIRC client script for a demonstration of how to use DllCall() to make a network connection
to a TCP/IP server and receive data from it.

2) The operating system offers standard dialog boxes that prompt the user to pick a font and/or color, or an icon.
These dialogs use structures and are demonstrated at www.autohotkey.com/forum/topic17230.html.