How to provide access to type information from an MFC automation server at run time (185720)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
    • Microsoft Visual C++, 32-bit Professional Edition 4.2
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q185720

SUMMARY

This article shows how to provide access to type information from an MFC automation server at run time. MFC automation servers by default return E_NOTIMPL for IDispatch::GetTypeInfo().

MORE INFORMATION

The automation object in the following steps refers to a CCmdTarget derived object with automation enabled.

Step-by-step example

  1. Place the DECLARE_OLETYPELIB macro in the automation object's header file within the class definition.
  2. Place the IMPLEMENT_OLETYPELIB macro in the automation object's implementation file at global scope.
  3. Override CCmdTarget::GetDispatchIID() in your derived class, and then return the IID of the primary dispatch interface.

    Sample code

    :
          // In the header file for the automation object, say CmyAutoObject.
          virtual BOOL GetDispatchIID(IID* pIID);
    
          // In the implementation file for CmyAutoObject.
          BOOL CMyAutoObject::GetDispatchIID(IID* pIID)
          {
             *pIID = IID_IMyAutoObject;
             return TRUE;
          }
    					
  4. Call EnableTypeLib(), in the constructor for the automation object.
Make sure that the type library is registered using AfxOleRegisterTypeLib() or RegisterTypeLib(), or that the type library is available as a resource in the .dll or .exe file. For more information about how to add the type library as a resource, click the following article number to view the article in the Microsoft Knowledge Base:

122285 How to add type libraries as resources to .dll and .exe files

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Jaganathan Thangavelu, Microsoft Corporation

Modification Type:MajorLast Reviewed:9/1/2005
Keywords:kbAutomation kbhowto KB185720 kbAudDeveloper