Click here to Skip to main content
13,351,225 members (46,054 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked
Posted 8 Jul 2002

Hello World in COM using ATL

, 21 Apr 2004
Rate this:
Please Sign up or sign in to vote.
The objective of this tutorial is to demonstrate how to build a COM Server and an MFC Client using Visual C++ 6.0. We are going to develop a COM server that takes in a string as input parameter and returns the string prefixed with a "Hello".

Sample Image


The objective of this tutorial is to demonstrate how to build a COM Server and an MFC Client using Visual C++ 6.0. We are going to develop a COM server that takes in a string as input parameter and returns the string prefixed with a "Hello".


You will need to have worked in Windows before to understand the terminology like dialogs, buttons, edit boxes. Having worked in Visual C++ before would be an added advantage.

Creating the COM server

  1. Choose ATL COM app wizard and type in the Project name. In this case it is sample1
  2. Next choose server type as dynamic link library and click on finish.
  3. Click okay to get Class wizard to generate the files.
  4. Go to the class view, right click on “sample1 classes” and choose “New ATL object”.
  5. Now choose simple object.
  6. Type in the short name for the component, in this case CGreet, the wizard automatically fills in the rest of the names.
  7. Go to the class view again
  8. Right click on the spoon shaped icon saying ICGreet and choose “Add Method”
  9. Type in the name of the function as SayHello and fill in the parameters as [in] BSTR name, [out,retval] BSTR *retstr
  10. Now go to file view and type in the following code into CGreet.cpp as shown below
Compile the application and your server is ready
//{-----------code added by imran on 18 sept 2k-----------------------
// CCGreet
STDMETHODIMP CCGreet::SayHello(BSTR name, BSTR *retstr)

// This method's signature is
//SayHello([in] BSTR name, [out,retval] BSTR *retstr) 

     char str[20] ;
     sprintf(str,"hello "); 
              // copy the value of hello into the string variable

   //create a new variable called temp initialized with the value of str
     CComBSTR temp(str);

   //append the input parameter value to the temp variable
    temp += name ;
   //send the value back to the calling function
   *retstr = temp.Detach();
   return S_OK;

//-----------code added by imran on 18 sept 2k---------------------------}

Creating the COM Client

Here we create a COM client for the above COM server DLL. This is a MFC dialog based application with a edit box and two buttons.
  1. Select New MFC AppWizard from the project menu and type in the project name mfcclient in this case
  2. Choose Dialog based application and click Finish
  3. You would now be looking at the application in the resource view. Add an edit box to the application by selecting the edit box, next click on the dialog box and drag.
  4. Also create a CString (value) variable associated with it, call this variable m_edit – press CTRL and W to bring up the class wizard. Choose the member variables tab and choose IDC_EDIT1 and click on “Add Variable”, type in the name.
  5. Now a file called mfcclientdlg.cpp would have been generated .
  6. Double click the “Ok” button on the dialog, the wizard pops up a box asking a name for the function, choose the default name “OnOk” to go to the mfcclientdlg.cpp file
  7. Modify the file to look like the file below.
Run the application, enter a name in the text box and click the okay button, this will cause the contents of the edit box to change to the same text prefixed with a hello.
// mfcclientDlg.cpp : implementation file
//{----------------code added by imran -----------------
#include "..\sample1\sample1_i.c" // this is a server file
#include "..\sample1\sample1.h"  // this is a server file
//The above two server files should be included in the client header file

#include "Comdef.h"      //for usage of BSTR
#include "Atlbase.h"     // for usage of CComBSTR
//----------------code added by imran -----------------}

void CMfcclientDlg::OnOK()


//{-----------code added by imran on ---------------------------------- 
    HRESULT hr = CoInitialize(NULL); // Initialize COM library

    if (FAILED(hr)) //if initialization fails then exit

         AfxMessageBox("Failed to initialize COM library");

    //Get the below two constants from the servername_i.c
    const CLSID CLSID_CGreet = {0x242C8BCE,0x8D72,0x11D4,
    const IID IID_ICGreet = {0x242C8BCD,0x8D72,0x11D4,

    char mname[20];
    CString cResult;

    // sprintf(mname,"Imran"); <- can be used to write 
    // a string to a char array

    // ICGreet is the name of the interface in the CGreet 
    // class (which is the server)
    // pointer to the interface

    ICGreet *ptrICGreet = NULL;

    //create the com
    hr = CoCreateInstance(CLSID_CGreet, NULL, 
                          (LPVOID*) &ptrICGreet);

    if (SUCCEEDED(hr))
         CComBSTR mresult;

         //get the name typed into the edit box and
         //copy it into the char array 
    // called mname

         //typecast the char to a bstr before it is passed to the COM
         _bstr_t bstresult(mname);

         //finally... call the COM function via the interface pointer

         //method signature is SayHello([in] BSTR name, 
         //[out,retval] BSTR *retstr)
         //copy the result into a CString so that
         //it can be displayed in a message box

         cResult = mresult;

         //copy the result into a CString value variable 
         //associated with the edit box

         m_edit = mresult;

         //update the display on the screen

         //free the COM pointer...



//-----------code added by imran on 18 sept 2k---------------------------}

    // CDialog::OnOK();



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Imran Ebrahim
Product Manager
India India
Imran is a software professional with 9 years of development experience and has worked on JAVA, VB6, VC6, C# & some JavaScript.

He is interested in Hi-Performance code, OO Methodology, OS API's, Optimizing Windows, Tweaking IE.

For the last few years Imran has specialized in developing robust, hi-performance applications & GUI's (Swing) for MNC's.

You may also be interested in...


Comments and Discussions

GeneralGood Article Pin
Nitin_an19-Aug-08 0:21
memberNitin_an19-Aug-08 0:21 
Generaluhmm Pin
drprestin12-Nov-07 14:24
memberdrprestin12-Nov-07 14:24 
GeneralRe: uhmm Pin
drprestin12-Nov-07 15:03
memberdrprestin12-Nov-07 15:03 
I decided to just download the sample and open it from VC++ 6.0 but I get error on compilation :
error C2065: 'sprintf' : undeclared identifier
GeneralRe: uhmm Pin
drprestin12-Nov-07 15:07
memberdrprestin12-Nov-07 15:07 
GeneralDLL TO EXE Pin
drprestin13-Nov-07 10:53
memberdrprestin13-Nov-07 10:53 
GeneralRe: uhmm Pin
drprestin15-Nov-07 8:52
memberdrprestin15-Nov-07 8:52 
Generalnice but having a problem Pin
chetanprakash7-Nov-05 1:37
memberchetanprakash7-Nov-05 1:37 
GeneralRe: nice but having a problem Pin
coldsun5-Mar-07 22:26
membercoldsun5-Mar-07 22:26 
GeneralTo author Pin
daniel23-Jan-08 20:02
memberdaniel23-Jan-08 20:02 
GeneralRe: To author Pin
raja_sharma_anubhav5-Feb-09 3:11
memberraja_sharma_anubhav5-Feb-09 3:11 
QuestionWHAT UP Pin
ShamanYoung15-Jun-05 17:25
memberShamanYoung15-Jun-05 17:25 
Answerhave soluted! Pin
ShamanYoung15-Jun-05 18:04
memberShamanYoung15-Jun-05 18:04 
GeneralA general COM question Pin
comServ12326-Oct-04 6:42
membercomServ12326-Oct-04 6:42 
GeneralClass not registered Pin
act_x19-Oct-04 13:22
memberact_x19-Oct-04 13:22 
Generalclassid Pin
purusingh8-Aug-04 21:28
memberpurusingh8-Aug-04 21:28 
GeneralMatlab COM Pin
dnqhung20-May-04 19:33
memberdnqhung20-May-04 19:33 
GeneralRe: Matlab COM Pin
carinbahar17-Aug-05 3:22
membercarinbahar17-Aug-05 3:22 
GeneralUse MFC Dll with Visual Studio 2003 Pin
creeder03-May-04 0:37
membercreeder03-May-04 0:37 
GeneralPlug-in for MS Outlook Pin
Atif Bashir30-Jul-03 17:33
memberAtif Bashir30-Jul-03 17:33 
Generalsprintf Pin
MoMad24-Oct-02 8:28
memberMoMad24-Oct-02 8:28 
GeneralRe: sprintf Pin
Le Viet Linh31-Jul-03 19:40
sussLe Viet Linh31-Jul-03 19:40 
GeneralRe: sprintf Pin
w_o_l_f_e17-Apr-04 23:22
memberw_o_l_f_e17-Apr-04 23:22 
GeneralRe: sprintf Pin
Jubjub22-Apr-04 17:18
memberJubjub22-Apr-04 17:18 
GeneralRe: sprintf Pin
cisla@cisaplicaciones.net9-Jun-05 7:38
membercisla@cisaplicaciones.net9-Jun-05 7:38 
GeneralRe: sprintf Pin
Jubjub9-Jun-05 13:06
memberJubjub9-Jun-05 13:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 22 Apr 2004
Article Copyright 2002 by Imran Ebrahim
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid