Microsoft KB Archive/107750: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
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 "Name of DLL your create"
   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 = &quot;str1&quot;
       StringStruct.str1 = "str1"
       StringStruct.str2 = &quot;str2&quot;
       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 &quot;The .h file where you added the code above&quot;
<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
   &quot;Programmers Guide,&quot; Chapter 24, &quot;Calling Procedures in DLLs,&quot; in
   "Programmers Guide," Chapter 24, "Calling Procedures in DLLs," in
   &quot;User-Defined Types&quot; under &quot;Calling DLL Procedures with Specific Data
   "User-Defined Types" under "Calling DLL Procedures with Specific Data
   Types.&quot; */  
   Types." */  


       lstrcpyn(lpStringStruct-&gt;str1, &quot;change11&quot;, 8) ;
       lstrcpyn(lpStringStruct-&gt;str1, "change11", 8) ;
       lstrcpyn(lpStringStruct-&gt;str2, &quot;change22&quot;, 8) ;
       lstrcpyn(lpStringStruct-&gt;str2, "change22", 8) ;
   }
   }
                         </pre></li></ol>
                         </pre></li></ol>

Revision as of 10:37, 20 July 2020

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