BUG: Unloading a Form After Assigning Text Property Causes GPF

Last reviewed: October 8, 1996
Article ID: Q150184
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SYMPTOMS

If the Text property of a Text box is set equal to the Text property of a Text box on a separate modal form, and if that statement is followed by unloading the modal form, a General Protection Fault results.

STATUS

Microsoft has confirmed this to be an issue in the Microsoft products listed at the beginning of this article. Microsoft is researching this issue and will post new information here in the Microsoft Knowledge Base as it becomes available.

WORKAROUND

Rather than use the Unload statement to unload the modal dialog box, use the PostMessage API function. The declaration for PostMessage is:

   #If Win32 Then

   Private Declare Function PostMessage Lib "user32" Alias _
   "PostMessageA"(ByVal hWnd As Long, ByVal wMsg As Long, _
   ByVal wParam As Long, ByVal lParam As Long) As Long

   #Else

   Private Declare Function PostMessage Lib "User" (ByVal hWnd As Integer,
   ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As
   Integer

   #End If

To post a close message for a form, set the first parameter to the hWnd of the target form, and the other three parameters as specified below:

   Const NILL = 0&

   Const WM_SYSCOMMAND = &H112

   Const SC_CLOSE = &HF060

so the following statement closes down Form2:

   PostMessage Form2.hWnd, WM_SYSCOMMAND, SC_CLOSE, NILL

MORE INFORMATION

Steps To Reproduce Problem

  1. Start a new project in Visual Basic. Form1 is created by default. Add a Text box to Form1. In the Click event for Form1, place the following code:

       Private Sub Form_Click()
         Form2.Show 1
       End Sub
    
    

  2. From the Insert menu, select Form to add another form to the project. On Form2, place a Text box. Add the following code to Form2:

       Private Sub Text1_DblClick()
         Form1.Text1 = Text1
         Unload Me
       End Sub
    
    

  3. Run the project by pressing F5. Click on Form1 to show Form2 modally. Double-click on the text box in Form2, and a General Protection Fault occurs.

In one test on Windows NT 3.51, with the 32-Bit Edition of Visual Basic, the error message was:

   The instruction at "0x00428646" referenced memory at "0x00d10cd8". The
   memory could not be "read".

In another test with, the 16-Bit Edition of Visual Basic, the error message was:

   VB caused a General Protection Fault in module VB.EXE at 0016:2BFB.

To work around the problem, place the declaration given in the Workaround Section above in the General Declarations section of Form2, and change the code in the DblClick event of the Text box on Form2 to:

   Private Sub Text1_DblClick()

   Const NILL = 0&

   Const WM_SYSCOMMAND = &H112

   Const SC_CLOSE = &HF060

   Form1.Text1.Text = Text1.Text

   PostMessage Form2.hWnd, WM_SYSCOMMAND, SC_CLOSE, NILL

   End Sub


Additional reference words: 4.00 vb4win vb4all
KBCategory: kbprg kbbuglist
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 8, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.