Passing Arguments to DLL Functions

Valid data types for arguments passed to DLL functions

Since DLL functions are written in C, the arguments you pass to these functions must have the appropriate C data types. In addition to the standard 4Test data types, Silk Test Classic also supports the following C data types:

  • char, int, short, and long
  • unsigned char, unsigned int, unsigned short, and unsigned long
  • float and double
Note: Any argument you pass must have one of these data types (or be a record that contains fields of these types).

Passing string arguments

The char* data type in C is represented by the 4Test STRING data type. The default string size is 256 bytes.

The following code fragments show how a char array declared in a C struct is declared as a STRING variable in a 4Test record:

// C declaration
typedef struct
char szName[32]; 

// 4Test declaration
type REC is record
STRING sName, size=32 
To pass a NULL pointer to a STRING, use the NULL keyword in 4Test. If a DLL sets an out parameter of type char* to a value larger than 256 bytes, you need to initialize it in your 4Test script before you pass it to the DLL function. This will guarantee that the DLL does not corrupt memory when it writes to the parameter. For example, to initialize an out parameter named my_parameter, include the following line of 4Test code before you pass my_parameter to a DLL:
my_parameter = space(1000)

If the user calls a DLL function with an output string buffer that is less then the minimum size of 256 characters, the original string buffer is resized to 256 characters and a warning is printed. This warning, String buffer size was increased from x to 256 characters (where x is the length of the given string plus one) alerts the user to a potential problem where the buffer used might be shorter than necessary.

Passing arguments to functions that expect pointers

When passing pointers to C functions, use these conventions:

  • Pass a 4Test string variable to a DLL that requires a pointer to a character (null terminated).
  • Pass a 4Test array or list of the appropriate type to a DLL that requires a pointer to a numerical array.
  • Pass a 4Test record to a DLL that requires a pointer to a record. 4Test records are always passed by reference to a DLL.
  • You cannot pass a pointer to a function to a DLL function.

Passing arguments that can be modified by the DLL function

An argument whose value will be modified by a DLL function needs to be declared using the out keyword. If an argument is sometimes modified and sometimes not modified, then declare the argument as in and then, in the actual call to the DLL, preface the argument with the out keyword, enclosed in brackets.

For example, the third argument (lParam) to the SendMessage DLL function can be either in or out. Therefore, it is declared as follows:

// the lParam argument is by default an in argument
dll "user.dll"
SendMessage (HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam) 
Then, to call the DLL with an out argument, you use the keyword out, enclosed within brackets:
SendMessage (Open.hWnd, WM_GETTEXT, 256, [out] sText)

Passing window handles to a DLL function

If a parameter takes a window handle, use the hwnd property or the GetHandle method of the AnyWin class to get the window handle you need.