Click here to Skip to main content
Click here to Skip to main content
Go to top

Method Enumeration in ATL COM Components

, 8 May 2001
Rate this:
Please Sign up or sign in to vote.
Explains Enumerating Methods in ATL COM
<!-- Download Links --> <!-- Add the rest of your HTML here -->

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
        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

Share

About the Author

s.prabhakarreddy

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

Comments and Discussions

 
GeneralGood joke PinmemberAnonymous10-May-01 21:45 
GeneralRe: Good joke PinmemberAndy Friedman14-May-01 6:06 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 9 May 2001
Article Copyright 2001 by s.prabhakarreddy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid