In this walkthrough, you will use the Visual Studio integrated development environment (IDE) and the various wizards to create the same ATL COM server.
To create a simple ATL COM server
-
On the File menu, click New, and then click Project.
The New Project dialog box appears.
-
In the Project Types pane, click Visual C++, and in the Templates pane, click the ATL Project icon.
-
In the Name box, enter MyServer.
-
Click OK.
The ATL Project Wizard appears.
-
Click Application Settings.
You will see that the Attributed check box is not selected by default. Click the check box to select it.
-
Click Finish to close the wizard and generate the project.
The result is an inproc ATL COM server without any server objects.
-
On the Build menu, click Build Solution.
Building the project successfully registers the server with the operating system.
To add and implement a server object
-
In Solution Explorer, right-click the MyServer project
-
On the shortcut menu, click Add, and then click Add Class.
The Add Class dialog box appears.
-
In the Templates pane, click the ATL Simple Object item and click Open.
The ATL Simple Object Wizard appears.
-
In the Short Name text box, enter Object1.
-
Click Finish to accept the remaining default values.
-
In Class View, right-click the IObject1 node.
-
On the shortcut menu, click Add, and then click Add Property.
-
For Property type, enter SHORT.
-
For Property name, enter GetANum.
-
Click Finish.
-
In the body of the get_GetANum method of CObject1, replace the comment with the following code:
-
On the Build menu, click Build Solution.
To create the test client application
-
In Solution Explorer, right-click the solution node and select Add, then select New Project.
The New Project dialog box appears.
-
In the Project Types pane, click Visual C++, and in the Templates pane, click the Win32 Project icon.
-
In the Name text box, enter COMTest.
-
Click OK.
The Win32 Application Wizard appears.
-
Click Application Settings and select Console application.
-
Click Finish to close the dialog box and generate the project.
-
In Solution Explorer, double-click the COMTest.cpp.
-
Replace the default code with the following:
#include "stdafx.h"
#include <iostream>
#include "atlbase.h"
#import "..\MyServer\_MyServer.tlb" no_namespace
using namespace std;
struct OleCom {
OleCom() { CoInitialize(NULL);}
~OleCom() { CoUninitialize(); }
}olecom;
int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<IUnknown> spUnknown;
spUnknown.CoCreateInstance(__uuidof(CObject1));
CComPtr<IObject1> pI;
spUnknown.QueryInterface(&pI);
short res = 0;
pI->get_GetANum(&res);
cout << res << endl;
return 0;
}
-
On the Build menu, click Build Solution.
To run the test application
-
At the command line, change to the COMTest\Debug directory.
-
Run the application by entering the following command:
You will see the integer value being printed.
Tasks
Walkthrough: Creating a COM Server Using a Text Editor
Reference
Developing a COM DLL with COM Attributes