Microsoft KB Archive/84928

= Microsoft Knowledge Base =

Differences Passing Fixed/Variable Length Strings to DLL/API
Last reviewed: February 17, 1995

Article ID: Q84928

The information in this article applies to:

- Microsoft Test for Windows, versions 1.0 and 2.0

SUMMARY
The following rules apply when you pass strings from Test Driver to a Windows DLL or API routine:


 * If you pass a fixed-length string, and the Declare statement for the external routine specifies the data type for that argument 'As Any' or 'As String * ', you must add a NULL-terminator (CHR$(0)) to the end of the string, or unpredictable results can occur (see the example below.)
 * You cannot pass a variable-length string as an argument for a routine that has been declared to use a fixed-length string. If you attempt to do so, Test Driver will give you a &quot;Type mismatch&quot; error (see the example below.) You can, however, pass a fixed- length string as an argument for a routine that has been declared to use a variable-length string. You do not need to append the NULL- terminator onto your fixed-length strings if the argument is declares 'As String'; Test Basic will do it for you.

MORE INFORMATION
The sample code below uses the Microsoft Windows API function call MessageBox to demonstrate this situation.

Steps to Reproduce Problem
 Start Microsoft Test Driver.  Enter the following code: Declare Sub MessageBox Lib &quot;USER&quot; (h%, m As String * 30, c$, f%) Dim junk As String * 10 Dim x As String * 30 Dim junk2 As String * 10

junk = &quot;blah blah&quot; junk2 = &quot;boo boo&quot; x = &quot;Fixed-Length String&quot; '<--Notice no NULL-terminator has been added to this string. MessageBox 0, x, &quot;Sample&quot;, 0  Press F5 to run the code.

Notice the message box on the screen with &quot;Fixed-Length String&quot; followed by garbage characters. MessageBox is a C function, and as such, will begin displaying the characters of a string starting at the first character of the string, and continue through the characters until it encounters a NULL-terminator character.

Because Test Basic does not automatically attach a NULL-terminator to a fixed-length string, and because the string &quot;x&quot; has no NULL-terminator, it continues through the contents of DGROUP until it encounters a NULL-terminator.

To prevent this from happening, add a NULL-terminating character, CHR$(0), to the end of the fixed-length string as follows:

x = &quot;Fixed Length String&quot; + CHR$(0) Another way to avoid the problem is to use variable-length strings, because Test Basic automatically appends a NULL-terminating character to them.

You cannot pass a variable-length string as an argument for a routine that has been declared to use a fixed-length string. If you attempt to do so, Test Driver will give you a &quot;Type mismatch&quot; error as follows:

Declare Sub MessageBox Lib &quot;USER&quot; (h%, m as string * 30, c$, f%) 'The DECLARE above sets up MessageBox to expect a fixed-length 'string. Dim x$ 'DIMs x$ as a variable-length string.

x$ = &quot;Fixed-Length String&quot; MessageBox 0, x$, &quot;Sample2&quot;, 0  'Will error with &quot;Type mismatch&quot; 'on x. You can, however, pass a fixed-length string as an argument for a routine that has been declared to use a variable-length string. The following is an example:

Declare Sub MessageBox Lib &quot;USER&quot; (h%, m$, c$, f%) ' The DECLARE above sets up MessageBox to expect a variable-length ' string. Dim x As String * 30 'DIMs x as a fixed-length string.

x = &quot;Fixed-Length String&quot; MessageBox 0, x, &quot;Sample3&quot;, 0 'No error occurs on the call.