Click here to Skip to main content
15,894,646 members
Articles / Desktop Programming / ATL

Hello World in COM using ATL

Rate me:
Please Sign up or sign in to vote.
4.41/5 (30 votes)
21 Apr 20042 min read 197.9K   4.7K   56  
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".
// mfcclientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mfcclient.h"
#include "mfcclientDlg.h"

//{----------------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 -----------------}


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMfcclientDlg dialog

CMfcclientDlg::CMfcclientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMfcclientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMfcclientDlg)
	m_edit = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMfcclientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMfcclientDlg)
	DDX_Text(pDX, IDC_EDIT1, m_edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMfcclientDlg, CDialog)
	//{{AFX_MSG_MAP(CMfcclientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMfcclientDlg message handlers

BOOL CMfcclientDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMfcclientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMfcclientDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMfcclientDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMfcclientDlg::OnOK() 
{

//{-----------code added by imran on 18 sept 2k----------------------------------	
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 = {0x1638ED3F,0x43E1,0x11D6,{0x89,0xB2,0x00,0x01,0x03,0x02,0x15,0x8B}};
	const IID IID_ICGreet = {0x1638ED3E,0x43E1,0x11D6,{0x89,0xB2,0x00,0x01,0x03,0x02,0x15,0x8B}};

	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,CLSCTX_INPROC_SERVER,IID_ICGreet,(LPVOID*) &ptrICGreet);

	if (SUCCEEDED(hr))
	{
		
		CComBSTR mresult;

		//get the name typed into the edit box and copy it into the char array called mname
		GetDlgItemText(IDC_EDIT1,mname,20);

		//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
		ptrICGreet->SayHello(bstresult,&mresult);
		//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;
		MessageBox(cResult);

		//copy the result into a CString value variable associated with the edit box
		m_edit = mresult;

		//update the display on the screen
		UpdateData(FALSE);
		//free the COM pointer...
		ptrICGreet->Release();
		

	}
	//MessageBox(mname);

	CoUninitialize();
//-----------code added by imran on 18 sept 2k--------------------------------}
	
	//CDialog::OnOK();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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.

http://techspot121.blogspot.com/

Comments and Discussions