Calling a DLL from within a 4Test Script

A declaration for a DLL begins with the keyword dll. The general format is:

dll dllname.dll
prototype
[prototype]...  

where dllname is the name of the dll file that contains the functions you want to call from your 4Test scripts and prototype is a function prototype of a DLL function you want to call.

Environment variables in the DLL path are automatically resolved. You do not have to use double backslashes (\\) in the code, single backslashes (\) are sufficient.

The Open Agent supports calling both 32bit and 64bit DLLs. You can specify which type of DLL the Open Agent should call by using the SetDllCallPrecedence method of the AgentClass class. If you do not know if the DLL is a 32bit DLL or a 64bit DLL, use the GetDllCallPrecedence function of the AgentClass Class. The Classic Agent provides support for calling 32bit DLLs only.

Silk Test Classic supports only the _stdcall calling convention.
Note: In some versions of Silk Test Classic, you can also use the _cdecl calling convention, although it is not officially supported. The _cdecl calling convention does not work with Silk Test 14.0 or later. Using the _cdecl calling convention might lead to unexpected failures of previously functioning DLL calls when migrating from the Classic Agent to the Open Agent or when upgrading Silk Test Classic to a newer version in which _cdecl does not work. If you are facing such failing DLL calls, ensure that you are using the _stdcall calling convention with the _stdcall naming decoration rules applied. For additional information on the DLL calling conventions, see /Gd, /Gr, /Gv, /Gz (Calling Convention).

Prototype syntax

A function prototype has the following form:
return-type func-name ( [arg-list] )
where:
return-type
The data type of the return value, if there is one.
func-name
An identifier that specifies the name of the function.
arg-list
A list of the arguments passed to the function, specified as follows:
[pass-mode] data-type identifier
where:
pass-mode
Specifies whether the argument is passed into the function (in), passed out of the function (out), or both (inout). If omitted, in is the default.

To pass by value, make a function parameter an in parameter.

To pass by reference, use an out parameter if you only want to set the parameter’s value; use an inout parameter if you want to get the parameter’s value and have the function change the value and pass the new value out.

data-type
The data type of the argument.
identifier
The name of the argument.

You can call DLL functions from 4Test scripts, but you cannot call member functions in a DLL.

Example

The following example writes the text hello world! into a field by calling the SendMessage DLL function from the DLL user32.dll.

use "mswtype.inc"
use "mswmsg32.inc"  

dll "user32.dll"
  inprocess ansicall INT SendMessage (HWND hWndParent, UINT  msg, WPARAM  wParam, LPARAM lParam) alias "SendMessageA"

testcase SetTextViaDllCall()
  SendMessage(UntitledNotepad.TextField.GetHandle(), WM_SETTEXT, 0, "hello world! ")