Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/104239

From BetaArchive Wiki

FILE: Using VBX Controls in AN _AFXDLL DLL

Q104239



The information in this article applies to:


  • The Microsoft Foundation Classes (MFC), included with:
    • Microsoft Visual C++, 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

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

Q119591 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;
   } 

Additional query words:

Keywords : kbfile kbsample kb16bitonly kbMFC kbVBX kbVC
Issue type :
Technology : kbAudDeveloper kbMFC


Last Reviewed: May 8, 2001
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.