Click here to Skip to main content
11,716,133 members (83,617 online)
Click here to Skip to main content
Add your own
alternative version

COM from scratch - PART TWO

, 17 Apr 2004 164.5K 5.3K 128
An article about COM Library.
demoapp.zip
ComDemo.exe
demoappsource.zip
ComDemo
ComDemo.clw
ComDemo.dsp
ComDemo.dsw
Comp1
Comp1.clw
Comp1.def
Comp1.dsp
Comp1.dsw
Debug
icon1.ico
Release
res
Comp2
Comp2.clw
Comp2.def
Comp2.dsp
Comp2.dsw
Debug
Release
res
Comp3
Comp3.clw
Comp3.def
Comp3.dsp
Comp3.dsw
Debug
res
Debug
Release
res
bitmap1.bmp
bitmap2.bmp
bitmap3.bmp
bitmap4.bmp
bitmap5.bmp
bitmap6.bmp
bitmap7.bmp
bitmap8.bmp
bitmap9.bmp
bmp00001.bmp
bmp00002.bmp
bmp00003.bmp
bmp00004.bmp
bmp00005.bmp
bmp00006.bmp
butterfl.ico
com.bmp
ComDemo.ico
ComDemoDoc.ico
ico00001.ico
ico00002.bmp
ico00002.ico
ico00003.ico
ico00004.ico
icon1.ico
Toolbar.bmp
parttwosources.zip
part_two
part_two_client
Debug
part_two_client.dsp
part_two_client.dsw
part_two_component
component.def
Component.dll
MAKEFILE
part_two_improved_client_server
client
client.dsp
client.dsw
Debug
Component
Component.clw
Component.def
Component.dsp
Component.dsw
Debug
Component.dll
res
part_two_rebuildproblem
client
client.dsp
client.dsw
Debug
DLL.dll
DLL
Debug
DLL.dsp
DLL.dsw
// ComDemoView.cpp : implementation of the CComDemoView class
//

#include "stdafx.h"
#include "ComDemo.h"
//#include"C:\\Documents and Settings\\Aria\\Desktop\\DemoDll\\ComDemo\\Comp1\\Registry.h"
//#include"..\\ComDemo\\Comp1\\interface.h"
//#include"..\\ComDemo\\Comp2\\interface.h"
#include"..\\ComDemo\\Comp3\\interface.h"

#include "ComDemoDoc.h"
#include "ComDemoView.h"
#include <objbase.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//interface pointers for accesseing Component1
IUnknown*    g_pIUnknown1=NULL;
IComponent1* g_pIComponent1=NULL;

//interface pointers for accesseing Compoenen2
IUnknown*    g_pIUnknown2=NULL;
IComponent2* g_pIComponent2=NULL;


//interface pointers for accesseing Compoenen3
IUnknown*    g_pIUnknown3=NULL;
IComponent3* g_pIComponent3=NULL;




/////////////////////////////////////////////////////////////////////////////
// CComDemoView

IMPLEMENT_DYNCREATE(CComDemoView, CView)

BEGIN_MESSAGE_MAP(CComDemoView, CView)
	//{{AFX_MSG_MAP(CComDemoView)
	ON_COMMAND(ID_COMP2, OnComp2)
	ON_WM_LBUTTONDOWN()
	ON_WM_TIMER()
	ON_COMMAND(ID_COMP1, OnComp1)
	ON_COMMAND(ID_COMP3, OnComp3)
	ON_COMMAND(ID_SHOW, OnShow)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
    //ON_MESSAGE(WM_RELEASE,OnRelease)

	END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComDemoView construction/destruction

CComDemoView::CComDemoView()
{
	// TODO: add construction code here
	
}

CComDemoView::~CComDemoView()
{
}

BOOL CComDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CComDemoView drawing

void CComDemoView::OnDraw(CDC* pDC)
{
	CComDemoDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
CBrush brush;
brush.CreateSolidBrush(RGB(0,0,0));
CRect rect;
GetClientRect(&rect);
pDC->FillRect(&rect,&brush);
}

/////////////////////////////////////////////////////////////////////////////
// CComDemoView printing

BOOL CComDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CComDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CComDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CComDemoView diagnostics

#ifdef _DEBUG
void CComDemoView::AssertValid() const
{
	CView::AssertValid();
}

void CComDemoView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CComDemoDoc* CComDemoView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CComDemoDoc)));
	return (CComDemoDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CComDemoView message handlers


////////////////////////////
void CComDemoView::OnComp1()
{
    
    CString str;
    int i;
    CStdioFile fr;
    //Not allow creation of the component more than one
    fr.Open("C:\\Comp1IUnknown.txt",CFile::modeRead);
    fr.ReadString(str);
    fr.Close();
    i=atoi(str);
    
    if(i==0)// 0 means component1 is not created
    {
        
        
        //CoInitialize(NULL); is called in the InitInstance() function at the start of the program
        HRESULT hr = ::CoCreateInstance(CLSID_Component1,NULL,CLSCTX_INPROC_SERVER,IID_IUnknown,(void**)&g_pIUnknown1) ;
        
        if (SUCCEEDED(hr))
        {
            TRACE(_T("Succeeded getting IComponent1.\n")) ;
            CStdioFile fw;
            COLORREF color=RGB(0,255,255);
            hr=g_pIUnknown1->QueryInterface(IID_IComponent1,(void**)&g_pIComponent1);
            if(SUCCEEDED(hr))
            {
                g_pIComponent1->DefineWindow_Com1(false);
                g_pIComponent1->ShowWndBackground_Com1(color);
                
                fw.Open ("C:\\Comp1IUnknown.txt",CFile::modeWrite);
                fw.WriteString ("1");// not possible to make more componet1 until the one which is already created is released
                fw.Close();
            }
            SetTimer(1,100,NULL);// checking the clients acions every 100 ms
        }
        else
        {
            TRACE(_T("Component1 creation failed"));
            AfxMessageBox("Build the dlls and move them in the folder 'C:\\Codeproject' and activate the registeration file(registerdlls.reg)");
        }
    }
}


////////////////////////////
void CComDemoView::OnComp2() 
{
	CString str;
int i;
CStdioFile fr;
fr.Open("C:\\Comp2IUnknown.txt",CFile::modeRead);
fr.ReadString(str);
fr.Close();
i=atoi(str);
if(i==0)
{
    
    //CoInitialize(NULL); is called in the ExitINstance() function at the end of the program
    HRESULT hr = ::CoCreateInstance(CLSID_Component2,NULL,CLSCTX_INPROC_SERVER,IID_IUnknown,(void**)&g_pIUnknown2) ;
    
    if (SUCCEEDED(hr))
    {
        TRACE(_T("Succeeded getting IComponent2.\n")) ;
        CStdioFile fw;
        COLORREF color=RGB(255,255,255);
        hr=g_pIUnknown2->QueryInterface(IID_IComponent2,(void**)&g_pIComponent2);
        
        if(SUCCEEDED(hr))
        {
            g_pIComponent2->DefineWindow_Com2(false);
            g_pIComponent2->ShowWndBackground_Com2(color);
            //g_pIComponent2->StopFlying();
            fw.Open ("C:\\Comp2IUnknown.txt",CFile::modeWrite);
            fw.WriteString ("1");
            fw.Close();
        }
        
        SetTimer(1,100,NULL);
    }
    else
    {
        TRACE(_T("Component2 creation failed"));
        AfxMessageBox("Build the dlls and move them in the folder 'C:\\Codeproject' and activate the registeration file(registerdlls.reg)");
    }
}
}


	




////////////////////////////////////////////////////////////
void CComDemoView::OnLButtonDown(UINT nFlags, CPoint point) 
{
    CString str;
	CStdioFile fr;
	fr.Open("C:\\Comp1IUnknown.txt",CFile::modeRead);
	fr.ReadString(str);
	fr.Close();
	if(str!="0")
	{
		if(NULL!=g_pIComponent1)
		g_pIComponent1->MenuitemNotselected1();
	}
	fr.Open("C:\\Comp2IUnknown.txt",CFile::modeRead);
	fr.ReadString(str);
	fr.Close();
	if(str!="0")
	{
		if(NULL!=g_pIComponent2)
		g_pIComponent2->MenuitemNotselected2();
	}
	
	
	CView::OnLButtonDown(nFlags, point);
}



//--------------------------------------------------------------------//
// This function checks out  menu item selections from the components
// windows and  the corresponding methods belonging to the components 
// will be called.
//--------------------------------------------------------------------//
void CComDemoView::OnTimer(UINT nIDEvent)
{
    CString str;
    int i;
    CStdioFile fr;
    
    
    //Checking out the menu item selection for componenet1 and response to that
    fr.Open("C:\\Comp1Notifyclient.txt",CFile::modeRead);
    fr.ReadString(str);
    fr.Close();
    i=atoi(str);
    
    if(i!=0)// 1 means that the user has selected "Night();" menu item from component 1's menu,
    //and here this action takes place by calling the component Night() method.
    {
        
        if(NULL!=g_pIComponent1)
            g_pIComponent1->Night();//Calling the Night() method of Component1
        //Init the notification file  back
        str="0";
        fr.Open("C:\\Comp1Notifyclient.txt",CFile::modeWrite|CFile::typeText);
        fr.WriteString(str);
        fr.Close();
        
    }
    
    
    //Checking out the menu item selection for componenet 2 and response to that
    fr.Open("C:\\Comp2Notifyclient.txt",CFile::modeRead);
    fr.ReadString(str);
    fr.Close();
    i=atoi(str);
    
    if(i!=0)
    {
        
        if(NULL!=g_pIComponent2)
            g_pIComponent2->StartFlying();//Calling the StopFlying() method of Component2
        //intit the notification file back
        str="0";
        fr.Open("C:\\Comp2Notifyclient.txt",CFile::modeWrite|CFile::typeText);
        fr.WriteString(str);
        fr.Close();
        
    }
    
    
    //Checking out the menu item selection for componenet 3 and response to that
    fr.Open("C:\\Comp3Notifyclient.txt",CFile::modeRead);
    fr.ReadString(str);
    fr.Close();
    i=atoi(str);
    COLORREF whitecolor=RGB(0,0,0);
    
    switch(i)
    {
        // 1 is coded to call the Night() method of the inner component1
        case 1:
            if(NULL!=g_pIComponent3)
                g_pIComponent3->Night();
            break;
            
            // 2 is coded to call the StartFlying method of the inner component2
        case 2:
            if(NULL!=g_pIComponent3)
                //calling the ShowWndBackground_Com2 once more in order to whipe the
                // window before flying
                g_pIComponent3->ShowWndBackground_Com2(whitecolor);
            g_pIComponent3->StartFlying();
            break;
            
            // 3 is coded to call the StopFlying method of the inner component2
        case 3:
            if(NULL!=g_pIComponent3)
                g_pIComponent3->StopFlying();
            break;
    }
    
    //initialize the notification file
    str="0";
    fr.Open("C:\\Comp3Notifyclient.txt",CFile::modeWrite|CFile::typeText);
    fr.WriteString(str);
    fr.Close();
    
    CView::OnTimer(nIDEvent);
}




////////////////////////////
void CComDemoView::OnComp3()
{
    
    CString str;
    int i;
    CStdioFile fr;
    fr.Open("C:\\Comp3IUnknown.txt",CFile::modeRead);
    fr.ReadString(str);
    fr.Close();
    i=atoi(str);
    if(i==0)
    {
        HRESULT hr = ::CoCreateInstance(CLSID_Component3,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_IComponent3,
        (void**)&g_pIUnknown3) ;
        
        if (SUCCEEDED(hr))
        {
            TRACE(_T("Succeeded getting IComponent3.\n")) ;
            COLORREF color=RGB(0,255,255);
            hr=g_pIUnknown3->QueryInterface(IID_IComponent3,(void**)&g_pIComponent3);
            if(SUCCEEDED(hr))
            {
                //Outer Component, Component3
                g_pIComponent3->DefineWindow_Com3();
                g_pIComponent3->ShowWndBackground_Com3(color);
                
                //Inner Component1
                g_pIComponent3->DefineWindow_Com1(true);
                g_pIComponent3->ShowWndBackground_Com1(color);
                
                //Inner Component2
                g_pIComponent3->DefineWindow_Com2(true);
                g_pIComponent3->ShowWndBackground_Com2(color);
                
                fr.Open ("C:\\Comp3IUnknown.txt",CFile::modeWrite);
                fr.WriteString ("1");
                fr.Close();
                SetTimer(1,100,NULL);
            }
            
        }
        
        else
        {
            AfxMessageBox("Build the dlls and move them in the folder 'C:\\Codeproject' and activate the registeration file(registerdlls.reg)");
            TRACE(_T("Component3 creation failed"));
            
        }
        
    }
}




///////////////////////////
void CComDemoView::OnShow() 
{
	// TODO: Add your command handler code here
	CRect rect;
	GetClientRect(&rect);
	CBitmap bmp, *poldbmp;
	CDC memdc;           
	CClientDC dc(this);
	bmp.LoadBitmap("IDB_COM" );
	// Create a compatible memory DC
	memdc.CreateCompatibleDC( &dc );
	// Select the bitmap into the DC
	poldbmp = memdc.SelectObject( &bmp );
	// Copy (BitBlt) bitmap from memory DC to screen DC
	dc.BitBlt(rect.left ,rect.top , rect.Width() , rect.Height() , &memdc, 0, 0, SRCCOPY );
	memdc.SelectObject( poldbmp ); 
}

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

Share

About the Author

Aria Ansari
Software Developer
Denmark Denmark
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 18 Apr 2004
Article Copyright 2004 by Aria Ansari
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid