Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/107750

From BetaArchive Wiki
Knowledge Base


How to Pass User-Defined Structure Containing Strings to DLL

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.

  1. Start a new project in Visual Basic.
  2. 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)
    
                            
  3. Add a command button (Command1) to Form1.
  4. 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
    
                            
  5. Add two text controls (Text1 and Text2) to Form1.
  6. 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) ;
    
                            
  7. 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