Microsoft KB Archive/107750: Difference between revisions
(importing KB archive) |
m (Text replacement - ">" to ">") |
||
(One intermediate revision by the same user not shown) | |||
Line 66: | Line 66: | ||
' Enter the following Declare statement as one, single line | ' Enter the following Declare statement as one, single line | ||
Declare Sub MyStructProc Lib | Declare Sub MyStructProc Lib "Name of DLL your create" | ||
(lpStringStruct As MYSTRINGSTRUCT) | (lpStringStruct As MYSTRINGSTRUCT) | ||
Line 74: | Line 74: | ||
<pre class="codesample"> Sub Command1_Click () | <pre class="codesample"> Sub Command1_Click () | ||
Dim StringStruct As MYSTRINGSTRUCT | Dim StringStruct As MYSTRINGSTRUCT | ||
StringStruct.str1 = | StringStruct.str1 = "str1" | ||
StringStruct.str2 = | StringStruct.str2 = "str2" | ||
MyStructProc StringStruct | MyStructProc StringStruct | ||
TEXT1.Text = StringStruct.str1 | TEXT1.Text = StringStruct.str1 | ||
Line 95: | Line 95: | ||
</pre></li> | </pre></li> | ||
<li><p>Add the following code to your .c file:</p> | <li><p>Add the following code to your .c file:</p> | ||
<pre class="codesample"> #include | <pre class="codesample"> #include "The .h file where you added the code above" | ||
void FAR PASCAL MyStructProc(LPSTRINGSTRUCT lpStringStruct) | void FAR PASCAL MyStructProc(LPSTRINGSTRUCT lpStringStruct) | ||
Line 103: | Line 103: | ||
structures are passed from Visual Basic to a DLL is fully described | 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 | 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. | Types." */ | ||
lstrcpyn(lpStringStruct- | lstrcpyn(lpStringStruct->str1, "change11", 8) ; | ||
lstrcpyn(lpStringStruct- | lstrcpyn(lpStringStruct->str2, "change22", 8) ; | ||
} | } | ||
</pre></li></ol> | </pre></li></ol> |
Latest revision as of 17:44, 20 July 2020
Article ID: 107750
Article Last Modified on 10/23/2003
APPLIES TO
- Microsoft Visual Basic 3.0 Professional Edition
This article was previously published under Q107750
SUMMARY
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.
MORE INFORMATION
The following step-by-step example passes a user-defined structure that contains strings to a DLL to manipulate.
- Start a new project in Visual Basic.
From the File menu, choose New Module (ALT F M). MODULE1.BAS will be created by default. Add the following code to the .BAS module:
' 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)
- Add a command button (Command1) to Form1.
Add the following code to the Command1_Click event of Form1:
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
- Add two text controls (Text1 and Text2) to Form1.
Create the C code needed to make the DLL. In the .h file of the DLL a user-defined type will create a mirror image of the type you defined in the Visual Basic .BAS file. Char str[8] is equivalent to Visual Basic declaration of str1 as String * 8. This structure definition takes up 16 bytes in memory as does the Visual Basic structure definition.
typedef struct STRINGSTRUCT{ char str1[8] ; char str2[8] ; } FAR * LPSTRINGSTRUCT ; /* Declaration of the function */ void FAR PASCAL MyStructProc(LPSTRINGSTRUCT) ;
Add the following code to your .c file:
#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: KB107750