Microsoft KB Archive/194609

= How To Pass Array of UDTs with Variable Length Strings to C/C++ =

Article ID: 194609

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q194609



SUMMARY
When you are working with an array of User-Defined types (UDTs) in Visual Basic, you may want to pass the array to a C/C++ DLL. However, the array cannot be passed directly because SAFEARRARYs are limited to Automation- safe data types, and a UDT is not a standard Automation data type.

Moreover, if the UDT contains variable length strings, you cannot pass a pointer to the first member of the array because Visual Basic will create a temporary copy of the member in order to handle normal UNICODE to ANSI conversion of strings.

This article demonstrates one way to bypass these problems and successfully pass an array of UDTs with variable length strings to C/C++ DLLs.



MORE INFORMATION
The solution provided here relies on the type library to prevent Visual Basic from copying the content of the array to the temporary space and transforming the strings between ANSI and UNICODE. See the REFERENCES section below for more information on this topic.

The UDT in this sample is:

Type TestUDT l As Long str As String End Type

Step-by-Step Example - Create the DLL and Type Library
 Open Visual C++ and select File|New. On the Projects tab, select "Win32 Dynamic-Link Library" and name the project "UDTArray." Select File|New again. On the Files tab, select "C++ Source File," name the file "UDTArray.c," and click OK. Repeat step 2 and this time choose "Text File" as the file type. Name the files "UDTArray.def" and "UDTArray.odl," respectively.  Next, add the following code to UDTArray.c:      #include       #include 

struct tagTestUDT {        long l;         BSTR str; } TestUDT;

void __stdcall ModifyStruct(struct tagTestUDT *t, long nTotalItem) {        // modify the last element long i = nTotalItem - 1; // the array begins from 0

(t+i)->l = 200;

SysFreeString((t+i)->str ); // free the string passed in         (t+i)->str = SysAllocString(OLESTR("Changed.")); }

  To make the functions exportable, add the following to UDTArray.def: EXPORTS ModifyStruct

  Declare your functions in a type library by adding the following to UDTArray.odl: [       uuid(D6229080-2CC3-11d2-9FD8-00C04F8EF4D4), helpstring("Pass Array of UDTs Helper") ]

library PassUDTLib {       //definition of the UDT typedef struct tagTestUDT {         long l;          BSTR str; }TestUDT;

[          helpstring("test"), dllname("UDTArray.dll") ]

module structDLL {          [             helpstring("Modify the elements of array"), entry("ModifyStruct") ]          void _stdcall ModifyStruct (            [in, out] TestUDT* t,             [in] long nTotalItem           ); };     };

</li> Go to Project|Settings, click the C/C++ Tab, and then choose "Code Generation" from the Category dropdown box. Change the "Struct Member Alignment" to be "4 Bytes" because Visual Basic uses 4 bytes alignment And Visual C++ uses 8 bytes by default.</li> Compile your DLL by choosing "Rebuild All" from the Build menu. When complete, copy the new DLL (UDTArray.dll) to your Visual Basic directory for testing.</li></ol>

Step-by-Step Example - The Visual Basic Client App
<ol> Open Visual Basic and create a new Standard EXE Project. Form1 is created by default.</li> From the Project menu, select References to call up the References dialog box, and then click Browse. Find the UDTArray.tlb file generated for your DLL.</li>  Add a CommandButton to Form1 and then add the following code to the code window for Form1: Private Sub Command1_Click ' Note: You do not need to redefine TestUDT because it is in       ' the type library we added to our project. Dim t(0 To 1) As TestUDT Dim i as long

t(0).l = 1 t(0).str = "test1"

t(1).l = 2 t(1).str = "Test2"

i = UBound(t) - LBound(t) + 1 ModifyStruct t(0), i 't(0)'s address will be passed to C

MsgBox "t(1).l = " & t(1).l & vbCrLf & "t(1).str = " & t(1).str End Sub

</li> Run the Visual Basic application and click the CommandButton. The message box displayed indicates that the last element of the array has been changed.</li></ol>

<div class="references_section">