ID Number: Q84928
1.00
WINDOWS
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, 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 "Type mismatch" 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 still must append the NULL- terminator
onto your fixed-length string, because Test Basic will not do it
for you. If you leave the NULL-terminator off, unpredictable
results can occur.
This information applies to Microsoft Test for Windows version 1.0.
More Information:
The sample code below uses the Microsoft Windows API function call
MessageBox to demonstrate this situation.
Steps to Reproduce Problem
--------------------------
1. Start Microsoft Test Driver.
2. Enter the following code:
Declare Sub MessageBox Lib "USER" (h%, m As String * 30, c$, f%)
Dim junk As String * 10
Dim x As String * 30
Dim junk2 As String * 10
junk = "blah blah"
junk2 = "foo foo"
x = "Fixed-Length String" '<--Notice no NULL-terminator has been
added to this string.
MessageBox 0, x, "Sample", 0
3. Press F5 to run the code.
Notice the message box on the screen with "Fixed-Length String"
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 "x" 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 = "Fixed Length String" + 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 "Type mismatch" error as follows:
Declare Sub MessageBox Lib "USER" (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$ = "Fixed-Length String"
MessageBox 0, x$, "Sample2", 0 'Will error with "Type mismatch"
'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. You
still must append the NULL-terminator onto your fixed-length string
though, because Test Basic will not do it for you. If you leave the
NULL-terminator off, unpredictable results can occur. The following is
an example:
Declare Sub MessageBox Lib "USER" (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 = "Fixed-Length String" + Chr$(0) 'You still need to append
'a NULL-terminator.
MessageBox 0, x, "Sample3", 0 'No error occurs on the call.
Additional reference words: 1.00