How To Develop COM Application Using VC++

Rate this article
5.00 out of 5
In this video tutorial we will show you how to develop a COM component and call it from COM enabled applications like VB. After viewing the video tutorial, download the source code and try to modify the code so as to get a feel of what is learned in this video tutorial.



Source Code

SystemInfoCtrl.cpp 

// SystemInfoCtrl.cpp : Implementation of DLL Exports.


// Note: Proxy/Stub Information
//      To build a separate proxy/stub DLL, 
//      run nmake -f SystemInfoCtrlps.mk in the project directory.

#include "stdafx.h"
#include "resource.h"
#include <initguid.h>
#include "SystemInfoCtrl.h"

#include "SystemInfoCtrl_i.c"
#include "SysInfo.h"


CComModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_SysInfo, CSysInfo)
END_OBJECT_MAP()

class CSystemInfoCtrlApp : public CWinApp
{
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CSystemInfoCtrlApp)
public:
    virtual BOOL InitInstance();
    virtual int ExitInstance();
//}}AFX_VIRTUAL

//{{AFX_MSG(CSystemInfoCtrlApp)
// NOTE - the ClassWizard will add and remove member functions here.
//    DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CSystemInfoCtrlApp, CWinApp)
//{{AFX_MSG_MAP(CSystemInfoCtrlApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
//    DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CSystemInfoCtrlApp theApp;

BOOL CSystemInfoCtrlApp::InitInstance()
{
    _Module.Init(ObjectMap, m_hInstance, &LIBID_SYSTEMINFOCTRLLib);
    return CWinApp::InitInstance();
}

int CSystemInfoCtrlApp::ExitInstance()
{
    _Module.Term();
    return CWinApp::ExitInstance();
}

/////////////////////////////////////////////////////////////////////////////
// Used to determine whether the DLL can be unloaded by OLE

STDAPI DllCanUnloadNow(void)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// Returns a class factory to create an object of the requested type

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    return _Module.GetClassObject(rclsid, riid, ppv);
}

/////////////////////////////////////////////////////////////////////////////
// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer(void)
{
    // registers object, typelib and all interfaces in typelib
    return _Module.RegisterServer(TRUE);
}

/////////////////////////////////////////////////////////////////////////////
// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
    return _Module.UnregisterServer(TRUE);
}

TestSystemInfoVCDlg.cpp

// TestSystemInfoVCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestSystemInfoVC.h"
#include "TestSystemInfoVCDlg.h"

#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()

/////////////////////////////////////////////////////////////////////////////
// CTestSystemInfoVCDlg dialog

CTestSystemInfoVCDlg::CTestSystemInfoVCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestSystemInfoVCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestSystemInfoVCDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestSystemInfoVCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestSystemInfoVCDlg)
DDX_Control(pDX, IDC_SYSINFO1, m_SysInfo);
//}}AFX_DATA_MAP
}

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

/////////////////////////////////////////////////////////////////////////////
// CTestSystemInfoVCDlg message handlers

BOOL CTestSystemInfoVCDlg::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 CTestSystemInfoVCDlg::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 CTestSystemInfoVCDlg::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 CTestSystemInfoVCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CTestSystemInfoVCDlg::OnOK() 
{
// TODO: Add extra validation here
BSTR strOS;
m_SysInfo.GetOSInfo(&strOS);
CString strOS1=strOS;
AfxMessageBox(strOS1);
}



Download Source Code

Joined Date :14-Nov-2012
Articles
Posted
60
Messages
Posted
3

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.




   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud