Calling DLL functions
Overview
Scripter allows importing and calling external DLL functions, by inserting special directives on declaration of script routines, indicating library name and, optionally, the calling convention, beyond the function signature.
External libraries are loaded by Scripter on demand, before function
calls, if not loaded yet (dynamically or statically). To load and unload
libraries explicitly, functions LoadLibary and FreeLibrary from unit
Windows
can be used.
Note
To enable DLL function calls, you must set AllowDLLCalls property to true.
Pascal syntax
function functionName(arguments): resultType; [callingConvention];
external 'libName.dll' [name ExternalFunctionName];
For example, the following declaration:
function MyFunction(arg: integer): integer; external 'CustomLib.dll';
imports a function called MyFunction from "CustomLib.dll". Default calling convention, if not specified, is register. Scripter also allows to declare a different calling convention (stdcall, register, pascal, cdecl or safecall) and to use a different name for DLL function, like the following declaration:
function MessageBox(hwnd: pointer; text, caption: string; msgtype: integer): integer;
stdcall; external 'User32.dll' name 'MessageBoxW';
that imports MessageBoxW function from "User32.dll" (Windows API library), named "MessageBox" to be used in script.
Declaration above can be used to functions and procedures (routines without result value).
Basic syntax
function lib "libName.dll" [alias ExternalFunctionName] [callingConvention]
functionName(arguments) as resultType
For example, the following declaration:
function lib "CustomLib.dll" MyFunction(arg as integer) as integer
imports a function called MyFunction from "CustomLib.dll". Default calling convention, if not specified, is stdcall. Scripter also allows to declare a different calling convention (stdcall, register, pascal, cdecl or safecall) and to use a different name for DLL function, like the following declaration:
function MessageBox lib "User32.dll" alias "MessageBoxA" stdcall
(hwnd as pointer, text as string, caption as string, msgtype as integer) as integer
that imports MessageBoxA function from "User32.dll" (Windows API library), named "MessageBox" to be used in script.
Declaration above can be used to functions and subs (routines without result value).
Supported types
Scripter support following basic data types on arguments and result of external functions:
- Integer
- Boolean
- Char
- Extended
- String
- Pointer
- PChar
- Object
- Class
- WideChar
- PWideChar
- AnsiString
- Currency
- Variant
- Interface
- WideString
- Longint
- Cardinal
- Longword
- Single
- Byte
- Shortint
- Word
- Smallint
- Double
- Real
- DateTime
- TObject descendants (class must be registered in scripter with DefineClass)
Others types (records, arrays, etc.) are not supported yet. Arguments of
above types can be passed by reference, by adding var
(Pascal) or
byref
(Basic) in param declaration of function.