Beginner-Level COM Tutorial

This tutorial is for people who are just....and I mean just starting to know COM. I have not gone into details of what COM is and how COM works because you will find plenty of such stuff all around the web. Instead what I have done here is to give you some simple walk through steps to be followed to create a very basic COM server and client application . The article is there because when I started with COM nothing so simple was there to get me started.

So, Guys follow me and step into COM. If you need my assistance or think that the article may better be written in your way rather than mine, just email me.

Server

  1. Create a new project select ATL COM APPWIZARD, Select a appropraite name for the server i.e TESTSERVER.
  2. In ATL COM APPWIZARD select Executable(EXE) option as this is going to be an executable server.
  3. When the skeleton application is created, select Insert->New ATL object. The ATL Object Wizard will appear.
  4. Select Simple Object and press Next. The ATL Object Wizard Properties dialog will appear.
  5. Type in the short name for the object in the short name edit box like DosCom. Click Ok. CDosCom class is created for your simple ATL Object with an interface IDosCom.
  6. You can add methods to the IDosCom by right clicking on the IDosCom interface in the project window and selecting the Add Method option. The Add method to Interface dialog will appear.
  7. Type in the name of of the method as RunCmd and click Ok.
  8. Type in the following line into the method RunCmd():
    STDMETHODIMP CDosCom::RunCmd()
    {
     system("dir | more");
     
     return S_OK;
    }
    
  9. Compile the project and your simple server is ready.

Client

  1. Create a new project select Win32 Console Application , Select a appropraite name for the client i.e TESTCLIENT.
  2. Inert the following files from the TESTSERVER directory into the TESTCLIENT project by selecting the Project->Add to Project->Files option from the main menu. The Insert files into project dialog box will appear where you need to select the following files from the TESTSERVER directory and click the OK button.
    • TestServer.h
    • TestServer_i.c
  3. Create a new .cpp file and insert it into the client project.
  4. Type in the following code into the file:
    //*************************************************
    //
    //		   TESTCLIENT.CPP
    //
    //*************************************************
    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>
    #include <windows.h>
    #include "TestServer.h"
    
    int main(int argc, char** argv)
    {
     char ch;
     cout<< "\n*** This is the test client ***\n" ;
    
     // interface pointers
     LPUNKNOWN	pUnk;
     IDosCom		*pDosCom;
     //IDemo2		*pDemo2;
    
     // com call results
     HRESULT		hr;
    
     // Initialise COM
     hr = CoInitialize(NULL);
    
     // create an instance and get IUnknown pointer
     hr = 
     CoCreateInstance(CLSID_DosCom,
     NULL,
     CLSCTX_LOCAL_SERVER,
     IID_IUnknown,
     (VOID FAR **) &pUnk);
     if (FAILED(hr))
     {
      cout<<"\nCoCreate instance failed for the Demo Object";
      return -1;
     }
    
     // query for the IDemo interface
     hr = pUnk->QueryInterface(IID_IDosCom,(LPVOID FAR*) &pDosCom );
     if (FAILED(hr))
     {
      cout<<"\nQueryinterface failed for IDemo Interface";
      return -1;
     }
    
     // call the interface method
     hr = pDosCom->RunCmd();
     cout<<"\nThe method call returned "<< hr;
     cout<<"\n\n\nPress any key....";
     cin>>ch;
    
     // release interfaces
     pDosCom->Release();
     pUnk->Release();
    
     // uninitialize DCOM.
     CoUninitialize();
    
     return 0;
    }
    //*************************************************
    
  5. Compile the project and your simple client application is ready.
  6. Test the application by running the client application. The client will invoke the server and calls its methods to show us a directory of files in Dos window.

Downloads

Download demo project - 18 Kb


Comments

  • Very Good

    Posted by poorni on 04/19/2004 03:47am

    I tried for the past two days in Net to find a beginner tutorial for COM and at last i found this one and is really good to start with...........

    Reply
  • remove use of ch

    Posted by Legacy on 02/25/2004 12:00am

    Originally posted by: panda

    its not required.
    the program is not getting exit when you have cin>>ch, and not using later.

    Reply
  • Will confuse beginners

    Posted by Legacy on 12/11/2003 12:00am

    Originally posted by: Shil

    I think beginners should not be introduced to ATL, till they understand the concept of COM.

    Reply
  • unexpected end of file error!!

    Posted by Legacy on 07/04/2003 12:00am

    Originally posted by: Umair

    d:\com\trial2\testserver\testserver_i.c(51) : fatal error C1010: unexpected end of file while looking for precompiled header directive
    
    

    I copied the exact code but I received the above error

    how on earth do I handle the above Error?
    Thanx 4 Ur Help

    Reply
  • Made a great start with COM

    Posted by Legacy on 01/25/2003 12:00am

    Originally posted by: Jurs

    this really helped me understand COM a much better... hoping you can add using MFC dlls as well..

    thank a lot

    Reply
  • Good!

    Posted by Legacy on 01/21/2003 12:00am

    Originally posted by: ShakeWorld

    This article help begin com programing!
    Thank you!

    Reply
  • Bravo ma ala care l-ai facut.

    Posted by Legacy on 08/12/2002 12:00am

    Originally posted by: Dan Lazar

    Un tutorial foarte bun.
    M-a ajutat sa inteleg cum e cu cacatul asta de ATL
    (pentru ca ATL asta e cam de cacat dupa parerea mea).
    Foarte bine facut tutorialul.

    Reply
  • No such file or directory&

    Posted by Legacy on 07/25/2002 12:00am

    Originally posted by: soonsing

    TestServer_i.c
    
    fatal error C1083: Cannot open source file: 'C:\Documents and Settings\Administrator\desktop\TestServer\TestServer_i.c': No such file or directory
    Error executing cl.exe.

    TestClient.exe - 1 error(s), 0 warning(s)
    //////////////////////////////////////////////////////

    TestServer_i.c
    fatal error C1083: Cannot open source file: 'C:\Documents and Settings\Administrator\desktop\TestServer\TestServer_i.c': No such file or directory
    Error executing cl.exe.

    TestClient.exe - 1 error(s), 1 warning(s)
    /////////////////////////////////////////////////////////

    Compiling...
    TestServer.cpp
    c:\documents and settings\administrator\desktop\comtutorial\testserver.cpp(8) : fatal error C1083: Cannot open precompiled header file: 'Debug/TestServer.pch': No such file or directory
    Error executing cl.exe.

    Reply
  • Good

    Posted by Legacy on 07/24/2002 12:00am

    Originally posted by: cyril

    The code is cool. But I had a little problem because I was in haste.

    In addition, how do I register a com in control panel services

    Reply
  • Excellent ,but more good examples

    Posted by Legacy on 06/08/2002 12:00am

    Originally posted by: pavan

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds