October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Beginner-Level COM Tutorial

  • July 19, 2000
  • By Naveed Ahmed
  • Send Email »
  • More Articles »

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 at nahmedmalik@hotmail.com.

Server

  1. Create a new project- select ATL COM APPWIZARD. Select an appropriate 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 appropriate name for the client i.e TESTCLIENT.
  2. Insert 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

History

Date Posted: July 19, 2000






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel