Click here to Skip to main content
15,881,559 members
Articles / Desktop Programming / ATL
Article

Method Enumeration in ATL COM Components

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
8 May 20012 min read 58.1K   533   22   2
Explains Enumerating Methods in ATL COM

Introduction

Usually when we are working with COM objects/controls in Visual Basic or ASP, we see method enumeration, like ListView.ColumnHeaders.Add (some arguments). In order to develop such COM components in VC++ using the ATL COM wizard, which allows the user to enumerate the methods of one COM object inside the method of another COM object, this article helps in getting the method enumeration when using a ATL based COM components in the Visual Basic and ASP Environments. It is also possible to access them in the VC++. Here are set of steps to be followed in building such Components

Steps in Developing the Component

  1. Using the ATL COM wizard, create a DLL project called CoolCodeComponent.
  2. Insert a 'Simple' object called "coolcode", with class name Ccoolcode and Interface name Icoolcode
  3. Add a Method called EnumMethod to the Icoolcode interface as follows
    HRESULT EnumMethod([out,retval] pEnumComponent *Res)

    Note : Here the pEnumComponent is the pointer to the interface whose methods are to be enumerated. It will be declared as a typedef in the .IDL in step 8 in this article.

  4. Insert another 'Simple' COM Object into the workspace called 'EnumComponent' with class name CEnumComponent and Interface name IEnumComponent.
  5. Add two methods to this interface:
    HRESULT Add(int x, int y,[out,retval] int *Sum) 

    Note : this method is used to add two numbers and return their sum

    HRESULT Multiply(int x, int y, [out,retval] int *Mul)

    Note : this method is used to multiply two numbers and return their product

  6. Provide the implementation for the above two methods as follows inside the EnumComponent.cpp
    STDMETHODIMP CEnumComponent::Add(int x, int y, int *Sum)
    {
    	*Sum =  x + y; // Add this line
    	return S_OK;
    }
    STDMETHODIMP CEnumComponent::Multiply(int x, int y, int *Mul)
    {
    	*Mul = x * y; // Add this line
    	return S_OK;
    }
  7. In coolcode.h file make the following changes:
    • add the header file of EnumComponent
      #include "EnumComponent.h"
    • declare a member variable to CEnumComponent like
      public :
           CComPtr<IEnumComponent> pEnumComp;
      
    • In the constructor of coolcode create the EnumComponent
      Ccoolcode()
      {
          pEnumComp.CoCreateInstance(CLSID_EnumComponent); // Add this line
      }
    • In the destructor of coolcode, release the interface to IEnumComponent
      ~Ccoolcode()
      {
          if(pEnumComp != NULL)
      	pEnumComp.Release();
      }
  8. Changes to the .IDL file:
    • Declare a variable of type IEnumComponent before the import "oaidl.idl";
      typedef IEnumComponent *pEnumComponent;
  9. In coolcode.cpp, add the following code to EnumMethod
    STDMETHODIMP Ccoolcode::EnumMethod(pEnumComponent *Res)
    {
          *Res = pEnumComp; // Add this line
          return S_OK;
    }

Now build the component in Win32 Release MinDependency mode

How to Use

Check the component in Visual Basic:

  1. Create a reference to
    CoolCodeComponent 1.0 Type Library
  2. Add a command button to the form and write the following code in button click
    VB
    Private Sub Command1_Click()
       Dim a As New COOLCODECOMPONENTLib.coolcode
            MsgBox a.EnumMethod.Add(10, 12)
            MsgBox a.EnumMethod.Multiply(10, 20)
    End Sub
    
  3. When you run the application it will display the result of the two method calls in message box.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Presently working in VisualSoft in VC++ and COM

Comments and Discussions

 
GeneralGood joke Pin
10-May-01 21:45
suss10-May-01 21:45 
I was at first confused with this article. Method enumeration in COM? If you have dispatch interface, you have it already. If you don't have dispatch, you can't get it.
Then I read it. And it was a good morning laugh. The best part is that it has average vote of almost 4! For an article that describes that you can return interface pointer from COM method. LOL


GeneralRe: Good joke Pin
Andy Friedman14-May-01 6:06
Andy Friedman14-May-01 6:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.