BUG: Visual Basic App Crashes Passing UDT with String and Enum

Article translations Article translations
Article ID: 221101 - View products that this article applies to.
This article was previously published under Q221101
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

SYMPTOMS

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.

RESOLUTION

Use the Long data type instead of Enum in the Type declaration of the Visual Basic UDT.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start Visual C++ 6.0. From the File menu, choose New.
  2. Select the Projects tab. Select Win32 Dynamic-Link Library from the list, and then enter MyDll in the Project Name field. Click OK.
  3. 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.
  4. 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.
  5. Paste the following code in the MyDll.cpp pane:
       #include <windows.h>
       // the UDT declaration
    
       enum VBenum
       {
          wf1 = 1,
          wf8 = 8,
          wf1e = 0x1E
       };
    
       typedef struct
       {
    	char	cArray[30];
    	VBenum	lformat;
       } mUDT; 
    
       // dll's export method
       int _stdcall passUDT(mUDT* pU)
       {
    	char* cp="hello, world!";
    	strncpy(pU->cArray, cp, 14);
    	pU->lformat = wf1e;
    	return 30;
       }
    						
  6. 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
    						
  7. From the Build menu, choose Rebuild All. Exit Visual C++.
  8. Start a new Visual Basic project. Form1 is created by default.
  9. Add two CommandButtons to Form1.
  10. 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
    						
  11. 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.
  12. Run the project and click Command2. Two Message Boxes display to confirm that no error occurred.
  13. Click Command1 and Visual Basic crashes without an error message.

Properties

Article ID: 221101 - Last Review: February 21, 2014 - Revision: 2.0
APPLIES TO
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
Keywords: 
kbnosurvey kbarchive kbbug kbdll kbpending KB221101

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com