Visual C++: Creating an ATL COM Component (Part – 2)

In our previous article we have created an empty ATL COM Project. Now we need to add our functionality to the project. Remember that we are going to create a COM component using ATL (Active Template Library). Most of the code for our project was generated by Visual Studio. Now we need to add our code into the project.

Upon creation of the project Visual Studio generates some files and the files will be shown up in the Solution Explorer window. Solution Explorer is the pane where Visual Studio displays the current solutions information i.e., projects in solution, files, resources etc.,.

The following files Visual Studio creates when we create an ATL COM Project.

  • “.idl” file – This is the IDL source for the project. IDL (Interface Definition Language) file is used to define COM components and its properties.
  • “.def” file – DLL (Dynamic Link Libraries) exports symbols to outside world. This symbol information will be added into “.DEF” files.
  • “.rc” file – Resource file used to keep project resources; like strings, bitmaps, icons etc.,.
  • “.rgs” file – Where Visual Studio maintains registry information for the project. Using this information Visual Studio registers the COM Components with Windows Registry.
  • Other files it generates “.h” files where declaration will put and “.cpp” files where implementation will be added.

In order to create a COM component, we need to create a Class for it.

Step 1. Select the solution from “Solution Explorer” pane and right click on it. Visual Studio will display a context-menu.

Visual Studio 2012 - "ATL COM Project" - "Solution Explorer" - "Add" - Sub menu

Visual Studio 2012 – “ATL COM Project” – “Solution Explorer” – “Add” – Sub menu

Step 2. Select Add menu item from context-menu. Visual Studio will display a sub-menu.

Step 3. From “Add” sub-menu, select “Class…” menu item. Visual Studio will display “Add Class” dialog.

Visual Studio 2012 - "ATL COM Project" - "Add Class" dialog

Visual Studio 2012 – “ATL COM Project” – “Add Class” dialog

Step 4. From “Add Class” dialog, select “ATL” sub-category under “Visual C++” category from left side pane. Visual Studio will display available templates in middle pane.

Step 5. Select “ATL Simple Object” template and click on Add button. Visual Studio will display “ATL Simple Object Wizard” dialog to add a class.

Visual Studio 2012 - "ATL Simple Object Wizard" - "Names" wizard

Visual Studio 2012 – “ATL Simple Object Wizard” – “Names” wizard

Step 6. Provide name of the class in “Short name:” field. Visual Studio will automatically populates the data for all other fields while entering the class name. For example: I have if we want to create a class “CHello”, provide “Hello” in “Short name:” field.

Step 7. Click on Next button to go to the next wizard in “ATL Simple Object Wizard” dialog. Visual studio will display “File Type Options” wizard.

Visual Studio 2012 - "ATL Simple Object Wizard" - File Type Options"

Visual Studio 2012 – “ATL Simple Object Wizard” – File Type Options”

Step 8. Click on Next button to go to the next wizard. Visual Studio will display “Options” wizard. Visual Studio displays Options to select Threading model, Interface, Aggregation and Support options for the component. Keep all default values and click on Finish button.

Visual Studio 2012 - "ATL Simple Object Wizard" - "Options"

Visual Studio 2012 – “ATL Simple Object Wizard” – “Options”

Step 9. Visual Studio will create a class based on the given data. In our example; Visual Studio creates a class “CHello”.

Now open “Class View” pane and observe that “CHello” class is created. Our class is ready; we need to add methods to it. Just add one method “SayHello” to display “Hello, World!” message when we call this function. Remember that we should add this method to an interface “IHello”; Visual Studio automatically add the method to “CHello” class. Because interface methods are exposed to outer world; so, that we can use these methods from other applications.

Step 10. Open “Class View” pane and right click on “IHello” class. Visual Studio will display a context-menu.

Visual Studio 2012 - "Class View" - "Add Menu" for an Interface

Visual Studio 2012 – “Class View” – “Add Menu” for an Interface

Step 11. From context-menu, select Add menu item. Visual Studio will display a sub-menu.

Step 12. From sub-menu, select “Add Method…” menu item. Visual Studio will display “Add Method Wizard” dialog.

Visual Studio 2012 - "Add Method Wizard" - "Names" wizard

Visual Studio 2012 – “Add Method Wizard” – “Names” wizard

Step 13. We are going to add “SayHello” method to “IHello” interface class.

  • Enter method name “SayHello” in “Method name:” field.
  • Click on Finish button to add the method.

Open “Hello.idl” file and observe that our method “SayHello” added into the file. Also observe that “SayHello” function is added into “CHello” class; Open “Hello.h” and “Hello.cpp” files to find “SayHello” entries.

Step 14. Now add the code to display “Hello, World!” message. Open “Hello.cpp” file and add the below code into “SayHello” function. After adding the code the function looks like below:

STDMETHODIMP CHello::SayHello(void)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	AfxMessageBox(L"Hello, World!");

	return S_OK;
}

Step 15. Now Build the project. Visual Studio will compile and register the component in Widows Registry. So, now we good to go. Our ATL COM Component is ready.

We can use this COM component in other applications like C#, Visual Basic, etc.,.

**

Leave a Reply