How to Get the Type of Another OLE Control from a Custom OCX

Last reviewed: October 10, 1997
Article ID: Q153580
4.00    | 4.00
WINDOWS | WINDOWS NT kbusage kbole kbhowto kbcode

The information in this article applies to:

  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic for Windows, 16-bit and 32-bit, version 4.0
  • Microsoft Visual C++, 32-bit Edition, version 4.x

SUMMARY

The type of an OLE control is the coclass name for the OLE control as defined in its ODL file. For example, the type of Textbox control is TextBox, the type of the Data Bound Grid control from Apex is DBGrid, and so on. In Visual Basic, the type of an OLE Control appears to the right of the control name in the Editbox portion of the property browser. This article shows how the type of an OLE custom control can be programmatically obtained.

MORE INFORMATION

Step-by-Step Example

  1. Create a rudimentary 32-bit OLE Control using the MFC CDK that ships with Visual C++ 4.x. This article assumes that this custom OCX is called Test (coclass name) and that the control class name is CTestCtrl.

  2. Use the Visual C++ MFC ClassWizard to add the following method to your control class. The external and internal name of this method is TypeOfControl. It takes a parameter of type LPDISPATCH, which is a pointer to the primary IDispatch Interface of the OLE control whose Type you want to find, and returns the type as a BSTR:

          BSTR CTestCtrl::TypeOfControl(LPDISPATCH lpDisp)
          {
    
             // TODO: Add your dispatch handler code here
    
             IProvideClassInfo *pProvideClassInfo;
             LPTYPEINFO pTypeInfo;
             BSTR bstrType = NULL;
    
             if(lpDisp->QueryInterface(IID_IProvideClassInfo, (LPVOID *)
                &pProvideClassInfo) ==  NOERROR)
          {
           if (pProvideClassInfo->GetClassInfo(&pTypeInfo) == NOERROR)
           {
              pTypeInfo->GetDocumentation(MEMBERID_NIL, &bstrType, NULL, NULL,
                                          NULL);
              pTypeInfo->Release();
             }
    
             pProvideClassInfo->Release();
          }
    
          return bstrType;
          }
    
    

  3. Build the Test OLE control from Visual C++ 4.x. This automatically registers the control if everything compiles properly.

  4. From Visual Basic 4.0 32-bit, open a new project, and add the Test OLE control to the Visual Basic toolbox by choosing it from the Tools\Custom Controls menu.

  5. Add an instance of the Test OLE control to Form1. This will be named Test1.

  6. Add a DBGrid control to Form1. This will be named DBGrid1.

  7. Add the following code to the general declarations portion of Form1:

          Private Sub Form_Click()
    
             Debug.Print Test1.TypeOfControl(dbgrid1.object)
          End Sub
    
    

  8. Run the Visual Basic program, and click the form. You should see DBGrid printed in the Debug window.


Additional reference words: 4.00 vb4win vb4all vbctrl
KBCategory: kbusage kbole kbhowto kbcode
KBSubcategory: IAPOLE
Keywords : IAPOLE kbcode kbhowto kbole kbusage
Version : 4.00 | 4.00
Platform : NT 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: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.