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. 彩色图像--色彩空间 CMY(K)场地

    得知DIP文章63日 转载请注明文章出处:http://blog.csdn.net/tonyshengtan .出于尊重文章作者的劳动,转载请标明出处!文章代码已托管.欢迎共同开发:https://g ...

  2. React的React Native

    React的React Native React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React ...

  3. Mac下Jekyll安装

    之前一直用Wordpress,虽然功能强大,各种插件各种bug,如果想弄个主题,折腾得要命.最近改用jekyll+gitHub免费空间.记录一下. 我用的是Mac,所以只讲述Mac下如何安装,Wind ...

  4. 【核心研究】消息队列_MessageQueue

    消息队列排队过程中的消息.这第一条消息将首先被处理.但假设消息本身指定要处理的时间.我们必须等待,直到时间的消息处理能力.新闻MessageQueue正在使用Message类的表示,队列中的邮件保存结 ...

  5. 网络编程easy错误点-手知道

    通常的网络编程socket编程.实际上.socket编程并不仅仅是满足网络间不同主机之间的通信,它也能实现同一台主机上不同进程间的通信需求. 其体如今创建socket时的參数的不同: int sock ...

  6. CSDN Androidclient开展(两):基于如何详细解释Java使用Jsoup爬行动物HTML数据

    文章引用鸿扬大大的链接具体介绍怎样使用Jsoup包抓取HTML数据,是一个纯javaproject,并将其打包成jar包.希望了解怎样用java语言爬虫网页的能够看下. 杂家前文就又介绍用HTTP訪问 ...

  7. shell变量赋值进阶

    首先,要理解shell中变量的3种赋值情况: unset 例子. unset a 空字符串, null 例子. a='' 非空,即不是unset,并且不是空字符串 例子: a=1 or a=b等 然后 ...

  8. WPF 辅助开发工具

    原文:WPF 辅助开发工具 以下介绍的工具均为免费版,有些是源代码开放,希望对大家有用. Kaxaml 轻量级XAML 编辑器,可以同时进行图像和XAML 代码的编辑.最终生成开发人员想要的XAML ...

  9. hdu2203 KMP水的问题

    两种方法     首先是纯KMP #include<stdio.h> #include<string.h> #include<iostream> using nam ...

  10. Net平台下的消息队列介绍

    Net平台下的消息队列介绍   本系列主要记录最近学习消息队列的一些心得体会,打算形成一个系列文档.开篇主要介绍一下.Net平台下一些主流的消息队列框架.       RabbitMQ:http:// ...