Microsoft KB Archive/221101

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

Article ID: 221101

Article Last Modified on 6/24/2004

-

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

-



This article was previously published under Q221101



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.



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.  Paste the following code in the MyDll.cpp pane: #include   // 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; }                       </li>  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 </li> From the Build menu, choose Rebuild All. Exit Visual C++.</li> Start a new Visual Basic project. Form1 is created by default.</li> Add two CommandButtons to Form1.</li>  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 </li> 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.</li> Run the project and click Command2. Two Message Boxes display to confirm that no error occurred.</li> Click Command1 and Visual Basic crashes without an error message.</li></ol>

Additional query words: Structure

Keywords: kbbug kbdll kbpending KB221101

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.