Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C++
Reference
Concepts
 Walkthrough: Creating a COM Server ...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:

Want more? Here are some additional resources on this topic:

Visual C++ 
Walkthrough: Creating a COM Server Using Wizards 

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

  1. On the File menu, click New, and then click Project.

    The New Project dialog box appears.

  2. In the Project Types pane, click Visual C++, and in the Templates pane, click the ATL Project icon.

  3. In the Name box, enter MyServer.

  4. Click OK.

    The ATL Project Wizard appears.

  5. Click Application Settings.

    You will see that the Attributed check box is not selected by default. Click the check box to select it.

  6. Click Finish to close the wizard and generate the project.

    The result is an inproc ATL COM server without any server objects.

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

  1. In Solution Explorer, right-click the MyServer project

  2. On the shortcut menu, click Add, and then click Add Class.

    The Add Class dialog box appears.

  3. In the Templates pane, click the ATL Simple Object item and click Open.

    The ATL Simple Object Wizard appears.

  4. In the Short Name text box, enter Object1.

  5. Click Finish to accept the remaining default values.

  6. In Class View, right-click the IObject1 node.

  7. On the shortcut menu, click Add, and then click Add Property.

  8. For Property type, enter SHORT.

  9. For Property name, enter GetANum.

  10. Click Finish.

  11. In the body of the get_GetANum method of CObject1, replace the comment with the following code:

    *pVal= 101;
  12. On the Build menu, click Build Solution.

To create the test client application

  1. In Solution Explorer, right-click the solution node and select Add, then select New Project.

    The New Project dialog box appears.

  2. In the Project Types pane, click Visual C++, and in the Templates pane, click the Win32 Project icon.

  3. In the Name text box, enter COMTest.

  4. Click OK.

    The Win32 Application Wizard appears.

  5. Click Application Settings and select Console application.

  6. Click Finish to close the dialog box and generate the project.

  7. In Solution Explorer, double-click the COMTest.cpp.

  8. 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;
       }
  9. On the Build menu, click Build Solution.

To run the test application

  1. At the command line, change to the COMTest\Debug directory.

  2. Run the application by entering the following command:

    comtest

    You will see the integer value being printed.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Perhaps some steps missing?      tvanriper   |   Edit   |   Show History
I am unable to use these steps to successfully build such a project.

Perhaps something is different in Visual Studio 2005 SP1.

When I get to step 2 of "To add and implement a server object":

  1. The menu item is simply 'Class...' instead of 'Add Class...', (although this is a very small quibble).
When I get to step 3 of "To add and implement a server object":
  1. The text of the button on the Add Class dialog is 'Add' instead of 'Open' (again, a very small quibble, but included for completeness' sake, in case maybe I'm missing something).
  2. Clicking 'Add' yields an error box with the following text:
ATL classes can only be added to MFC EXE and MFC Regular DLL

projects or project with full ATL support.

Prior to these steps, I was able to faithfully, and carefully, follow each of the steps as presented here.
question about not having to release the object      hlau   |   Edit   |   Show History
In the sample code, why is not necessary to explicitly call Release(), which is a IUnknown function, to release the object?
Tags What's this?: Add a tag
Flag as ContentBug
Replace identifier CObject1 through Object1      Roady4Road ... Thomas Lee   |   Edit   |   Show History
You have to replace the identifier "CObject1" through "Object1" in line 13 otherwise you will get a compiler error.

Line 13:
spUnknown.CoCreateInstance(__uuidof(CObject1));

but correct is:

spUnknown.CoCreateInstance(__uuidof(Object1));
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker