Want more? Here are some additional resources on this topic:
In this walkthrough, you will use the Visual Studio integrated development environment (IDE) and the various wizards to create the same 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.
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:
*pVal= 101;
In Solution Explorer, right-click the solution node and select Add, then select New Project.
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.
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; }
At the command line, change to the COMTest\Debug directory.
Run the application by entering the following command:
comtest
You will see the integer value being printed.
Perhaps something is different in Visual Studio 2005 SP1.
When I get to step 2 of "To add and implement a server object":
ATL classes can only be added to MFC EXE and MFC Regular DLLprojects or project with full ATL support.
projects or project with full ATL support.
Line 13:spUnknown.CoCreateInstance(__uuidof(CObject1));but correct is:spUnknown.CoCreateInstance(__uuidof(Object1));