FILE: Using VBX Controls in AN _AFXDLL DLL

Last reviewed: July 22, 1997
Article ID: Q104239
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++ for Windows, versions 1.0, 1.5

SUMMARY

There are two techniques for using VBX controls in a dialog box that is created in a dynamic-link library (DLL) built with _AFXDLL (Microsoft Foundation Class Library Extension DLL):

  • The application can call the Create() member function of the CVBControl class to create the VBX control. Call Create() in the OnInitDialog() function (for a modal dialog box) or in the OnCreate() function (for a modeless dialog box).
  • Reregister the "VBControl" class in the DLL so that it can be used from the DLL. Also, change the resource instance handle during the OnInitDialog() function so that resources in the DLL will be used while the controls are being created.

The AFXVBX sample demonstrates the techniques described below.

The following file is available for download from the Microsoft Software Library:

 ~ Afxvbx.exe (size: 24369 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from
               Online Services


MORE INFORMATION

A dialog box template that uses VBX controls specifies "VBControl" as the window class for each of the VBX controls. MFC registers the "VBControl" class when a programmer calls EnableVBX(). This is typically done in the InitInstance() function of a program. However, the class is registered as a local class rather than a global class. This means that a DLL used by the application does not see the registered window class and when Windows tries to create a dialog box in a DLL that uses the "VBControl" class, the dialog box creation will fail.

To register the "VBControl" class for the DLL, the following code can be used:

   // Check to see if the class is already registered.
   if(::GetClassInfo(hInstDLL,"VBControl",&wndClass)==0)
    {
     // If not registered, get the class information from the
     // application. This assumes that the application has called
     // EnableVBX to register the VBControl class.
     VERIFY(::GetClassInfo(AfxGetInstanceHandle(),"VBControl",
                           &wndClass));
     wndClass.hInstance = hInstDLL; // Change the instance handle so
                                    // it is that of the DLL and not
                                    // the App.
     VERIFY(::RegisterClass(&wndClass));  // Register the class.
    }

Also, if you are using Visual C++, version 1.0 (and NOT 1.50, 1.51, OR 1.52), then you should add the following code to the OnInitDialog() function for the dialog box that is created in the DLL:

   // PLEASE NOTE: THIS CODE ONLY TO BE USED WITH
   // VISUAL C++ VERSION 1.0 (MFC VERSION 2.0)

   BOOL CVBXDialog::OnInitDialog()
   {
    HINSTANCE hOldInstance = _AfxGetAppData()->appCurrentInstanceHandle;
    _AfxGetAppData()->appCurrentInstanceHandle = hInstDLL;
    CDialog::OnInitDialog();
    _AfxGetAppData()->appCurrentInstanceHandle = hOldInstance;

    // Add your initialization code below.
    return TRUE;
   }
 

	
	


Keywords : kb16bitonly kbfile kbsample MfcVBX
Technology : kbmfc
Version : 1.0 1.5
Platform : WINDOWS


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: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.