This article was previously published under Q221101
This article has been archived. It is offered "as is" and will no longer be updated.
If a Visual Basic application retrieves a User Defined Type (UDT) containing String and Enum fields from a C++ DLL, the Visual Basic application crashes on the call to the DLL file. No error message is displayed.
Use the Long data type instead of Enum in the Type declaration of the Visual Basic UDT.
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
Steps to Reproduce Behavior
Start Visual C++ 6.0. From the File menu, choose New.
Select the Projects tab. Select Win32 Dynamic-Link Library from the list, and then enter MyDll in the Project Name field. Click OK.
In the Win32 Dynamic-Link Library dialog box, select the An empty DLL project option, and click Finish. Click OK in the following dialog box.
From the File menu, choose New. Select the Files tab, choose C++ Source File from the list, and then enter MyDll.cpp in the File Name field. Click OK.
From the File menu, click New. Select the Files tab, and then choose Text File from the list. Enter MyDll.def in the File Name field.
Paste the following code in the MyDll.def pane:
; The DEF File LIBRARY MyDll EXPORTS passUDT @1
From the Build menu, choose Rebuild All. Exit Visual C++.
Start a new Visual Basic project. Form1 is created by default.
Add two CommandButtons to Form1.
Copy the following code into the General Declarations section of Form1:
Private Enum MyEnum wf1 = &H1& wf8 = &H8& wf1e = &H1E& End Enum Private Type MyUDT1 myStr As String * 30 mL As MyEnum End Type Private Type MyUDT2 myStr As String * 30 mL As Long End Type Private Declare Function passUDT1 Lib "c:\MyDll\Debug\MyDll.dll" _ Alias "passUDT" (myU As MyUDT1) As Long Private Declare Function passUDT2 Lib "c:\MyDll\Debug\MyDll.dll" _ Alias "passUDT" (myU As MyUDT2) As Long ' Visual Basic crashes on the call to passUDT1. Private Sub Command1_Click() Dim mU As MyUDT1 MsgBox passUDT1(mU) MsgBox mU.myStr End Sub ' Visual Basic works correctly on the call to passUDT2. Private Sub Command2_Click() Dim mU As MyUDT2 MsgBox passUDT2(mU) MsgBox mU.myStr End Sub
Modify the two Declare statements in the preceding code example so that they point to the location of the MyDll.dll file on your system.
Run the project and click Command2. Two Message Boxes display to confirm that no error occurred.
Click Command1 and Visual Basic crashes without an error message.