How to Pass User-Defined Structure Containing Strings to DLLID: Q107750
|
This articles shows by example how to pass a user-defined structure that contains strings to a DLL. The example enables a DLL to read and write the strings in a user-defined structure.
The following step-by-step example passes a user-defined structure that
contains strings to a DLL to manipulate.
' Fixed-length string elements of a structure are packed in memory
' as are other values in Visual Basic. The following structure takes up
' 16 bytes of memory:
'
Type MYSTRINGSTRUCT
str1 As String * 8
str2 As String * 8
End Type
' Enter the following Declare statement as one, single line
Declare Sub MyStructProc Lib "Name of DLL your create"
(lpStringStruct As MYSTRINGSTRUCT)
Sub Command1_Click ()
Dim StringStruct As MYSTRINGSTRUCT
StringStruct.str1 = "str1"
StringStruct.str2 = "str2"
MyStructProc StringStruct
TEXT1.Text = StringStruct.str1
TEXT2.Text = StringStruct.str2
End Sub
typedef struct STRINGSTRUCT{
char str1[8] ;
char str2[8] ;
} FAR * LPSTRINGSTRUCT ;
/* Declaration of the function */
void FAR PASCAL MyStructProc(LPSTRINGSTRUCT) ;
#include "The .h file where you added the code above"
void FAR PASCAL MyStructProc(LPSTRINGSTRUCT lpStringStruct)
{
/* You need to use lstrcpyn because the structure from Visual
Basic is packed, and the strings are not Null terminated. The way
structures are passed from Visual Basic to a DLL is fully described
beginning on page 566 in the Visual Basic version 3.0 for Windows
"Programmers Guide," Chapter 24, "Calling Procedures in DLLs," in
"User-Defined Types" under "Calling DLL Procedures with Specific Data
Types." */
lstrcpyn(lpStringStruct->str1, "change11", 8) ;
lstrcpyn(lpStringStruct->str2, "change22", 8) ;
}
Additional query words: 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 16, 1999