Beginner-Level COM Tutorial
Posted
by Naveed Ahmed
on July 19th, 2000
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<<"\n\n\nPress 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.

Comments
Very Good
Posted by poorni on 04/19/2004 03:47amI 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...........
Replyremove use of ch
Posted by Legacy on 02/25/2004 12:00amOriginally posted by: panda
its not required.
Replythe program is not getting exit when you have cin>>ch, and not using later.
Will confuse beginners
Posted by Legacy on 12/11/2003 12:00amOriginally posted by: Shil
I think beginners should not be introduced to ATL, till they understand the concept of COM.
Replyunexpected end of file error!!
Posted by Legacy on 07/04/2003 12:00amOriginally posted by: Umair
ReplyMade a great start with COM
Posted by Legacy on 01/25/2003 12:00amOriginally posted by: Jurs
this really helped me understand COM a much better... hoping you can add using MFC dlls as well..
thank a lot
ReplyGood!
Posted by Legacy on 01/21/2003 12:00amOriginally posted by: ShakeWorld
This article help begin com programing!
ReplyThank you!
Bravo ma ala care l-ai facut.
Posted by Legacy on 08/12/2002 12:00amOriginally 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.
ReplyNo such file or directory&
Posted by Legacy on 07/25/2002 12:00amOriginally posted by: soonsing
ReplyGood
Posted by Legacy on 07/24/2002 12:00amOriginally 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
ReplyExcellent ,but more good examples
Posted by Legacy on 06/08/2002 12:00amOriginally posted by: pavan
ReplyLoading, Please Wait ...