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
- Create a new project select ATL COM APPWIZARD, Select a appropraite name for the server i.e TESTSERVER.
- In ATL COM APPWIZARD select Executable(EXE) option as this is going to be an executable server.
- When the skeleton application is created, select Insert->New ATL object. The ATL Object Wizard will appear.
- Select Simple Object and press Next. The ATL Object Wizard Properties dialog will appear.
- 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. - 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. - Type in the name of of the method as RunCmd and click Ok.
- Type in the following line into the method RunCmd():
STDMETHODIMP CDosCom::RunCmd() { system("dir | more"); return S_OK; }
- Compile the project and your simple server is ready.
Client
- Create a new project select Win32 Console Application , Select a
appropraite name for the client i.e TESTCLIENT. - 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
- Create a new .cpp file and insert it into the client project.
- 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<<"nnnPress any key...."; cin>>ch; // release interfaces pDosCom->Release(); pUnk->Release(); // uninitialize DCOM. CoUninitialize(); return 0; } //*************************************************
- Compile the project and your simple client application is ready.
- 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.