共用方式為


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 Finish to close the wizard and generate the project.

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

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

  7. On the shortcut menu, click Properties.

  8. Click on Linker. Change the Per-UserRedirection option to Yes.

  9. Click OK.

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

See Also

Concepts

Developing a COM DLL with COM Attributes

Walkthrough: Creating a COM Server Using a Text Editor