ComATLATLMFCMFC MFCIUnknownMFCCCmdTargetComMFCCom MFCCOM

1.

1.1

#pragma  once

typedef long HRESULT;

// {30DF3430-0266-11cf-BAA6-00AA003E0EED}
extern const GUID CLSID_Math;
//{ 0x30df3430, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed } }; ////////////////////////////////////////////////////////////////////////////////////// // {30DF3432-0266-11cf-BAA6-00AA003E0EED}
extern const GUID IID_IOPerator;
//{ 0x30df3432, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed } }; class IOPerator:public IUnknown
{
public: virtual HRESULT _stdcall Add(int nParam1, int nParam2, int* pResult) =0;
virtual HRESULT _stdcall Subtract(int nParam1, int nParam2, int* pResult) =0;
virtual HRESULT _stdcall Multiple(int nParam1, int nParam2, int* pResult) =0;
virtual HRESULT _stdcall Divide(int nParam1, int nParam2, int* pResult) =0;
}; // {30DF3433-0266-11cf-BAA6-00AA003E0EED}
extern const GUID IID_IAdvanceOPerator;
//{ 0x30df3433, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed } }; class IAdvanceOPerator:public IUnknown
{
public: virtual HRESULT _stdcall Abs(int nParam1, int* pResult) =0;
virtual HRESULT _stdcall Power(int nParam1, int nParam2, int* pResult) =0;
};
// CMyMath command target

class CMyMath : public CCmdTarget
{
DECLARE_DYNCREATE(CMyMath) public:
CMyMath();
virtual ~CMyMath(); virtual void OnFinalRelease(); protected:
DECLARE_OLECREATE(CMyMath)
DECLARE_MESSAGE_MAP()
DECLARE_DISPATCH_MAP() DECLARE_INTERFACE_MAP()
BEGIN_INTERFACE_PART(OPerator, IOPerator)
STDMETHOD_(HRESULT, Add)(int nParam1, int nParam2, int* pResult);
STDMETHOD_(HRESULT, Subtract)(int nParam1, int nParam2, int* pResult);
STDMETHOD_(HRESULT, Multiple)(int nParam1, int nParam2, int* pResult);
STDMETHOD_(HRESULT, Divide)(int nParam1, int nParam2, int* pResult);
END_INTERFACE_PART(OPerator) BEGIN_INTERFACE_PART(AdvanceOperator, IAdvanceOPerator)
STDMETHOD_(HRESULT, Abs)(int nParam1, int* pResult);
STDMETHOD_(HRESULT, Power)(int nParam1, int nParam2, int* pResult);
END_INTERFACE_PART(AdvanceOperator)
};

1.2

#include "stdafx.h"
#include "MyCom16.h"
#include "MyMath.h" // CMyMath IMPLEMENT_DYNCREATE(CMyMath, CCmdTarget) CMyMath::CMyMath()
{
EnableAutomation();
EnableAggregation();
} CMyMath::~CMyMath()
{
} void CMyMath::OnFinalRelease()
{
// When the last reference for an automation object is released
// OnFinalRelease is called. The base class will automatically
// deletes the object. Add additional cleanup required for your
// object before calling the base class. CCmdTarget::OnFinalRelease();
} BEGIN_MESSAGE_MAP(CMyMath, CCmdTarget)
END_MESSAGE_MAP() BEGIN_DISPATCH_MAP(CMyMath, CCmdTarget)
END_DISPATCH_MAP() // Note: we add support for IID_IMyMath to support typesafe binding
// from VBA. This IID must match the GUID that is attached to the
// dispinterface in the .IDL file. // {7259EA0F-0E64-4FF9-BBA1-332E82AFA0D3}
static const IID IID_IMyMath =
{ 0x7259EA0F, 0xE64, 0x4FF9, { 0xBB, 0xA1, 0x33, 0x2E, 0x82, 0xAF, 0xA0, 0xD3 } };
static const GUID IID_IOPerator =
{ 0x30df3432, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed }}; static const GUID IID_IAdvanceOPerator =
{ 0x30df3433, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed }}; // CLSID_Math
IMPLEMENT_OLECREATE(CMyMath, "MyCom16.MyMath", 0x30df3430, 0x266, 0x11cf, 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed) BEGIN_INTERFACE_MAP(CMyMath, CCmdTarget)
INTERFACE_PART(CMyMath,IID_IMyMath, Dispatch)
INTERFACE_PART(CMyMath,IID_IOPerator,OPerator)
INTERFACE_PART(CMyMath,IID_IAdvanceOPerator,AdvanceOperator)
END_INTERFACE_MAP() // CMyMath message handlers ULONG CMyMath::XOPerator::AddRef()
{
METHOD_PROLOGUE(CMyMath, OPerator);
return pThis->ExternalAddRef(); }
ULONG CMyMath::XOPerator::Release()
{
METHOD_PROLOGUE(CMyMath, OPerator);
return pThis->ExternalRelease(); } HRESULT CMyMath::XOPerator::QueryInterface(REFIID riid, void** ppObject)
{
METHOD_PROLOGUE_EX_(CMyMath, OPerator);
return pThis->ExternalQueryInterface((void *)&riid,ppObject);
} HRESULT CMyMath::XOPerator::Add( int nParam1, int nParam2, int* pResult )
{
*pResult = nParam1 + nParam2;
return S_OK;
} HRESULT CMyMath::XOPerator::Subtract( int nParam1, int nParam2, int* pResult )
{
*pResult = nParam1 - nParam2;
return S_OK;
} HRESULT CMyMath::XOPerator::Multiple( int nParam1, int nParam2, int* pResult )
{
*pResult = nParam1 * nParam2;
return S_OK;
} HRESULT CMyMath::XOPerator::Divide( int nParam1, int nParam2, int* pResult )
{
*pResult = nParam1 / nParam2;
return S_OK;
} ULONG CMyMath::XAdvanceOperator::AddRef()
{
METHOD_PROLOGUE(CMyMath, AdvanceOperator);
return pThis->ExternalAddRef(); }
ULONG CMyMath::XAdvanceOperator::Release()
{
METHOD_PROLOGUE(CMyMath, AdvanceOperator);
return pThis->ExternalRelease(); } HRESULT CMyMath::XAdvanceOperator::QueryInterface(REFIID riid, void** ppObject)
{
METHOD_PROLOGUE(CMyMath, AdvanceOperator);
return pThis->ExternalQueryInterface((void *)&riid,ppObject);
} HRESULT _stdcall CMyMath::XAdvanceOperator::Abs( int nParam1, int* pResult )
{
if(nParam1 < 0)
*pResult = -nParam1;
else
*pResult = nParam1;
return S_OK;
} HRESULT _stdcall CMyMath::XAdvanceOperator::Power( int nParam1, int nParam2, int* pResult )
{
*pResult =1;
for(int i=0;i<nParam2;i++)
*pResult *=nParam1;
return S_OK;
}

2

2.1

#pragma  once

typedef long HRESULT;

class IArea:public IUnknown
{
public: virtual HRESULT _stdcall Triangle(int width, int High, float* pResult) =0;
virtual HRESULT _stdcall Square(int lengh, float* pResult) =0;
virtual HRESULT _stdcall Cirle(int r, float* pResult) =0;
};
#pragma once
#include "IArea.h" // CMyMath2 command target class CMyMath2 : public CCmdTarget
{
DECLARE_DYNCREATE(CMyMath2) public:
CMyMath2();
virtual ~CMyMath2(); virtual void OnFinalRelease();
virtual BOOL OnCreateAggregates(); BEGIN_INTERFACE_PART(Area, IArea)
STDMETHOD_(HRESULT, Triangle)(int width, int High, float* pResult);
STDMETHOD_(HRESULT, Square)(int lengh, float* pResult);
STDMETHOD_(HRESULT, Cirle)(int r, float* pResult);
END_INTERFACE_PART(Area)
protected:
DECLARE_OLECREATE(CMyMath2)
DECLARE_MESSAGE_MAP()
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP() };

2.2

#include "MyMath2.h"

#include <iostream>
using namespace std; const float PI = 3.14;
static const GUID CLSID_Math =
{ 0x30df3430, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed } }; // CMyMath2
IMPLEMENT_DYNCREATE(CMyMath2, CCmdTarget) CMyMath2::CMyMath2()
{
EnableAutomation(); } CMyMath2::~CMyMath2()
{
} void CMyMath2::OnFinalRelease()
{
// When the last reference for an automation object is released
// OnFinalRelease is called. The base class will automatically
// deletes the object. Add additional cleanup required for your
// object before calling the base class.
if(m_xInnerUnknown !=NULL)
{
IUnknown *pUnk =(IUnknown *)m_xInnerUnknown;
pUnk->Release();
}
CCmdTarget::OnFinalRelease();
} BEGIN_MESSAGE_MAP(CMyMath2, CCmdTarget)
END_MESSAGE_MAP() BEGIN_DISPATCH_MAP(CMyMath2, CCmdTarget)
END_DISPATCH_MAP() // Note: we add support for IID_IMyMath2 to support typesafe binding
// from VBA. This IID must match the GUID that is attached to the
// dispinterface in the .IDL file. // {60B1DE57-1DE8-4759-B220-C35E03B2049D}
static const IID IID_IMyMath2 =
{ 0x60B1DE57, 0x1DE8, 0x4759, { 0xB2, 0x20, 0xC3, 0x5E, 0x3, 0xB2, 0x4, 0x9D } }; static const GUID IID_IArea =
{ 0x30df3452, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed }}; // CLSID_Math
IMPLEMENT_OLECREATE(CMyMath2, "MyCom9.MyMath2", 0x30df3450, 0x266, 0x11cf, 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed) BEGIN_INTERFACE_MAP(CMyMath2, CCmdTarget)
INTERFACE_PART(CMyMath2, IID_IMyMath2, Dispatch)
INTERFACE_PART(CMyMath2,IID_IArea,Area)
INTERFACE_AGGREGATE(CMyMath2,m_xInnerUnknown) //CMyMath2CMyMath
END_INTERFACE_MAP() //CMyMath2CMyMath
BOOL CMyMath2::OnCreateAggregates()
{
#if 0
//
::CoCreateInstance(CLSID_Math,(IUnknown *)this,CLSCTX_INPROC_SERVER,IID_IUnknown,(void **)&m_xInnerUnknown);
#else
LPUNKNOWN pUnk = GetControllingUnknown();
::CoCreateInstance(CLSID_Math,(IUnknown *)pUnk,CLSCTX_INPROC_SERVER,IID_IUnknown,(void **)&m_xInnerUnknown);
#endif
return TRUE;
} // CMyMath2 message handlers
HRESULT _stdcall CMyMath2::XArea::Triangle( int width, int High, float* pResult )
{
*pResult =width*High *1.0/2;
return S_OK;
} HRESULT _stdcall CMyMath2::XArea::Square( int lengh, float* pResult )
{
*pResult =lengh *lengh*1.0/2;
return S_OK;
} HRESULT _stdcall CMyMath2::XArea::Cirle( int r, float* pResult )
{
*pResult = PI *r*r;
return S_OK;
} ULONG CMyMath2::XArea::AddRef()
{
METHOD_PROLOGUE(CMyMath2, Area);
return pThis->ExternalAddRef(); } ULONG CMyMath2::XArea::Release()
{
METHOD_PROLOGUE(CMyMath2, Area);
return pThis->ExternalRelease();
} HRESULT CMyMath2::XArea::QueryInterface(REFIID riid, void** ppObject)
{
METHOD_PROLOGUE_EX_(CMyMath2, Area);
return pThis->ExternalQueryInterface((void *)&riid,ppObject);;
}

3.

#include "../MyCom16/Operator.h"
#include "../MyCom9/IArea.h" using namespace std; static const GUID CLSID_Math =
{ 0x30df3430, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed } }; static const GUID IID_IOPerator =
{ 0x30df3432, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed } }; static const GUID IID_IAdvanceOPerator =
{ 0x30df3433, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed } }; static const GUID CLSID_Math2 =
{ 0x30df3450, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed }}; static const GUID IID_IArea =
{ 0x30df3452, 0x266, 0x11cf, { 0xba, 0xa6, 0x0, 0xaa, 0x0, 0x3e, 0xe, 0xed }}; int _tmain(int argc, _TCHAR* argv[])
{
CLSID clsId;
IClassFactory *pMathFactory = NULL;
IUnknown* pUnknown = NULL;
IUnknown* pUnk = NULL;
IOPerator *pOPerator = NULL;
IAdvanceOPerator *pAdvanceOperator = NULL;
IArea *pArea = NULL;
IArea *pArea2 = NULL;
IAdvanceOPerator *pAdvanceOperator2 = NULL;
int nResult = 0;
HRESULT hRes; CoInitialize(NULL); ///////////////////////Test MyCom1///////////////////////////////////////////////////////////////////
#if 1
//CLSIDFromProgID(_T("Testcom1 Server"),&clsId);
hRes = CoGetClassObject(CLSID_Math, CLSCTX_SERVER, NULL, IID_IClassFactory, (void**) &pMathFactory);
if(FAILED(hRes))
{
return 0;
}
pMathFactory->CreateInstance(NULL,IID_IOPerator,(void **)&pOPerator);
pMathFactory->Release(); pOPerator->Add(5,6,&nResult);
cout<<"5+6 ="<<nResult<<endl;
pOPerator->Multiple(5,6,&nResult);
cout<<"5*6 ="<<nResult<<endl;
pOPerator->Divide(5,6,&nResult);
cout<<"5/6 ="<<nResult<<endl; pOPerator->QueryInterface(IID_IAdvanceOPerator,(void **)&pAdvanceOperator);
pAdvanceOperator->Abs(-123,&nResult);
cout<<"Abs(-123) ="<<nResult<<endl;
pAdvanceOperator->Power(5,3,&nResult);
cout<<"Power(5,3) ="<<nResult<<endl; pAdvanceOperator->QueryInterface(IID_IUnknown,(void **)&pUnknown);
pOPerator->QueryInterface(IID_IUnknown,(void **)&pUnk);
pUnk->QueryInterface(IID_IAdvanceOPerator,(void **)&pAdvanceOperator2);
if(pUnk == pUnknown)
cout<<"They are the same com obj"<<endl;
else
cout<<"They not equal obj" <<endl; pUnknown->Release();
pUnk->Release();
pAdvanceOperator->Release();
pOPerator->Release();
pAdvanceOperator2->Release(); pOPerator = NULL;
pAdvanceOperator = NULL;
pUnk = NULL;
pUnknown = NULL;
pAdvanceOperator2 =NULL;
#endif /* Com9 Com1
*/
#if 1
pOPerator = NULL;
pAdvanceOperator = NULL;
pUnk =NULL;
pUnknown = NULL;
//CLSIDFromProgID(_T("Testcom1 Server"),&clsId);
hRes = CoGetClassObject(CLSID_Math2, CLSCTX_SERVER, NULL, IID_IClassFactory, (void**) &pMathFactory);
if(FAILED(hRes))
{
return 0;
}
pMathFactory->CreateInstance(NULL,IID_IArea,(void **)&pArea);
pMathFactory->Release();
float fResult =0.0f;
pArea->Triangle(3,4,&fResult);
cout<<"Triangle(3,4) = "<<fResult<<endl; pArea->Cirle(5,&fResult);
cout<<"Cirle(5) = "<<fResult<<endl;
pArea->QueryInterface(IID_IOPerator,(void **)&pOPerator);
pOPerator->Add(5,6,&nResult);
cout<<"5+6 ="<<nResult<<endl;
pOPerator->Multiple(5,6,&nResult);
cout<<"5*6 ="<<nResult<<endl;
pOPerator->Divide(5,6,&nResult);
cout<<"5/6 ="<<nResult<<endl; pOPerator->QueryInterface(IID_IAdvanceOPerator,(void **)&pAdvanceOperator);
pAdvanceOperator->QueryInterface(IID_IArea,(void **)&pArea2); if(pArea == pArea2)
cout<<"They are the same com obj"<<endl;
else
cout<<"They not equal obj" <<endl; pArea->QueryInterface(IID_IUnknown,(void **)&pUnknown);
pOPerator->QueryInterface(IID_IUnknown,(void **)&pUnk);
if(pUnk ==pUnknown)
cout<<"They are the same com obj"<<endl;
else
cout<<"They not equal obj" <<endl; pUnknown->Release();
pUnk->Release();
pArea->Release();
pOPerator->Release();
pAdvanceOperator->Release();
pArea2->Release();
#endif ::CoUninitialize();
return 0;
}

一个用MFC实现Com聚合样本的更多相关文章

  1. OpenCV与MFC实战之图像处理 样本采集小工具制作 c++MFC课程设计

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/12111102.html 入门不久的人可以通过opencv实战来锻炼一下学习opencv的成果, ...

  2. Qt的信号槽,一个老MFC的经验

    最近在利用闲暇时间研究Qt,大概有3周了,看过了官网的white paper并浏览了一遍<C++ GUI Programming with Qt 4, 2nd Edition>.总的来说, ...

  3. 【SQL进阶】【CASE/IF、COUNT/SUM、多条记录拼接为一个内容】Day03:聚合分组查询

    〇.今日内容概述 一.聚合函数 1.SQL类别高难度试卷得分的截断平均值[去最高最低分求平均] 自己的想法 SELECT tag, difficulty, ROUND((SUM(score)-MIN( ...

  4. 使用MFC做一个简单的‘能自动生成小学生四则运算的软件’

    这是软件工程的第一次作业!但由于我们python还没入门,所以这次的要求是‘语言不限’. 小学期做过一个关于MFC的‘资金管理系统’,也正好可以有界面,所以就选择了自己很熟悉的MFC来做这个作业! 1 ...

  5. 在MFC中使用一个单独的类实现数据在各个类之间的传递

    第一步:使用VS2010创建一个基于MFC的单文档程序,然后  编译 运行 确定没有问题. 第二步:添加一个名叫CGszCommonData  类. 第三步:在应用程序类的头文件里 添加#includ ...

  6. VC++ MFC工程中中如何将一个工程的资源(如对话框)复制到另外一个工程

    问题的提出:在工程1中用到的资源,在工程2中已有现成的.即工程1中要用到的对话框和工程2的完全相同,而工程2中对该对话框的布局已设计好.控件变量都绑定好了.但由于该对话框的控件特别多,如果在工程1中再 ...

  7. 用ATL和MFC来创建ActiveX控件

    摘要:目前MFC和ATL代表了两种框架,分别面向不同类型的基于Windows的开发.MFC代表了创建独立的Windows应用的一种简单.一致的方法:ATL提供了一种框架来实现创建COM客户机和服务器所 ...

  8. Elasticsearch 第六篇:聚合统计查询

    h2.post_title { background-color: rgba(43, 102, 149, 1); color: rgba(255, 255, 255, 1); font-size: 1 ...

  9. 【翻译】MongoDB指南/聚合——聚合管道

    [原文地址]https://docs.mongodb.com/manual/ 聚合 聚合操作处理数据记录并返回计算后的结果.聚合操作将多个文档分组,并能对已分组的数据执行一系列操作而返回单一结果.Mo ...

随机推荐

  1. Scan IP relocate/failover其它段后不能ping通过

    或手动集群重启单个节点srvctl relocate scan_listener后.群集网络段ping IP,VIP.SCAN IP正常.其他段ping SCAN IP 不通.其原因是,该路由ARP表 ...

  2. Spring3.0学习笔记文档的官方网站(六)--3.4.1

    3.4 依靠 3.4.1 依赖注入     依赖注入两种方式:基于构造函数DI.基于setter方法DI. 3.4.1.1 基于构造函数DI     参数是引进一个对象的.和缺乏父母之前-子类关系: ...

  3. 微信消息体签名及加解密功能详细解析以及.net实现

    原文:微信消息体签名及加解密功能详细解析以及.net实现 前言 微信消息体签名及加密功能已上线,明文传输确实存在安全风险,鉴于微信的用户范围使用之广泛,必定会成为众矢之的.所以大家还是尽快接入安全模式 ...

  4. C++内存分配和拷贝构造函数写研究

    昨晚参加笔试,开错题,有印象中的概念,但目前尚不清楚是怎么回事,什么原理,导致错误的话题.现在总结. 一.C++写内存分配研究 问题考察例如以下,请先不要看答案,看看你是否能做对,呵呵: waterm ...

  5. 由一道面试题想到的:Finally

    找工作时,有这样一道题: try{}里面有一条return语句,那么紧跟在这个try后的finally{}里的代码会不会执行,什么时候执行,在return之前还是之后? 我没有怎么思考,根据脑子里仅有 ...

  6. 兼容安卓的javaproject1.0

    <pre class="java" name="code"> //兼容安卓的系统package cn.com.likeshow; import ja ...

  7. 从头开始学JavaScript (二)——变量及其作用域

    原文:从头开始学JavaScript (二)--变量及其作用域 一.变量 ECMAscript变量是松散型变量,所谓松散型变量,就是变量名称可以保存任何类型的数据,每个变量仅仅是一个用于保存值的占位符 ...

  8. update值与原值相同时,SQL Server会真的去update还是忽略呢?

    原文:update值与原值相同时,SQL Server会真的去update还是忽略呢? 考虑下面的情况: 当update值与原值相同时,SQL Server会真的去update还是忽略?例如: upd ...

  9. MSSQL发现第五到数据的第十

    第二十数据查询数据库,第十条数据,两起案件: 1,ID是连接的,当然这样的情况比較好查.直接SELECT就能够了.取ID大于5小于10就能够了, 这样的情况比較少. 2.ID不是连接的.假设要取第五条 ...

  10. C#随机双色球

    using System; using System.Collections.Generic; namespace ConsoleApplicationRandnumber { class Progr ...