CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

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 to 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 solution’s information i.e., projects in the 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 their properties.
  • “.def” file – DLL (Dynamic Link Libraries) exports symbols to the 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.
  • In other files, it generates “.h” files where declaration will be 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 the “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 the “Add” sub-menu, select the “Class…” menu item. Visual Studio will display the “Add Class” dialog.

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

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

Step 5. Select the “ATL Simple Object” template and click on Add button. Visual Studio will display the “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 the name of the class in the “Short name:” field. Visual Studio will automatically populate 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 the “Short name:” field.

Step 7. Click on the Next button to go to the next wizard in the “ATL Simple Object Wizard” dialog. Visual Studio will display the “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 the Next button to go to the next wizard. Visual Studio will display the “Options” wizard. Visual Studio displays Options to select the Threading model, Interface, Aggregation, and Support options for the component. Keep all default values and click on the 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 the “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 the “Hello, World!” message when we call this function. Remember that we should add this method to an interface “IHello”; Visual Studio automatically adds the method to “CHello” class. Because interface methods are exposed to the outer world; so, that we can use these methods from other applications.

Step 10. Open the “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 12. From the sub-menu, select the “Add Method…” menu item. Visual Studio will display the “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 the “SayHello” method to “IHello” interface class.

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

Open the “Hello.idl” file and observe that our method “SayHello” was added to the file. Also, observe that the “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 the “Hello, World!” message. Open the “Hello.cpp” file and add the below code into the “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 Windows Registry. So, now we are good to go. Our ATL COM Component is ready.

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

**

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top