Introduction
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".
Prerequisite
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
- Choose ATL COM app wizard and type in the Project name. In this case it is
sample1
- Next choose server type as dynamic link library and click on finish.
- Click okay to get Class wizard to generate the files.
- Go to the class view, right click on
�sample1 classes�
and choose �New ATL object�.
- Now choose simple object.
- Type in the short name for the component, in this case
CGreet
, the wizard automatically fills in the rest of the names.
- Go to the class view again
- Right click on the spoon shaped icon saying
ICGreet
and choose �Add Method�
- Type in the name of the function as
SayHello
and fill in the parameters as [in] BSTR name, [out,retval] BSTR *retstr
- 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
STDMETHODIMP CCGreet::SayHello(BSTR name, BSTR *retstr)
{
char str[20] ;
sprintf(str,"hello ");
CComBSTR temp(str);
temp += name ;
*retstr = temp.Detach();
return S_OK;
}
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.
- Select
New MFC AppWizard
from the project menu and type in the project name mfcclient
in this case
- Choose
Dialog based application
and click Finish
- 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.
- 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.
- Now a file called
mfcclientdlg.cpp
would have been generated .
- 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
- 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.
#include "..\sample1\sample1_i.c" // this is a server file
#include "..\sample1\sample1.h" // this is a server file
#include "Comdef.h" //for usage of BSTR
#include "Atlbase.h" // for usage of CComBSTR
void CMfcclientDlg::OnOK()
{
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
AfxMessageBox("Failed to initialize COM library");
}
const CLSID CLSID_CGreet = {0x242C8BCE,0x8D72,0x11D4,
{0xAC,0x91,0x00,0xB0,0xD0,0x69,0x54,0x6F}};
const IID IID_ICGreet = {0x242C8BCD,0x8D72,0x11D4,
{0xAC,0x91,0x00,0xB0,0xD0,0x69,0x54,0x6F}};
char mname[20];
CString cResult;
ICGreet *ptrICGreet = NULL;
hr = CoCreateInstance(CLSID_CGreet, NULL,
CLSCTX_INPROC_SERVER,IID_ICGreet,
(LPVOID*) &ptrICGreet);
if (SUCCEEDED(hr))
{
CComBSTR mresult;
GetDlgItemText(IDC_EDIT1,mname,20);
_bstr_t bstresult(mname);
ptrICGreet->SayHello(bstresult,&mresult);
cResult = mresult;
MessageBox(cResult);
m_edit = mresult;
UpdateData(FALSE);
ptrICGreet->Release();
}
CoUninitialize();
}