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. C#中实现并发

    C#中实现并发的几种方法的性能测试 0x00 起因 去年写的一个程序因为需要在局域网发送消息支持一些命令和简单数据的传输,所以写了一个C/S的通信模块.当时的做法很简单,服务端等待链接,有用户接入后开 ...

  2. 王立平--android特权

    //同意应用程序访问和更改checkin数据库"properties"数据表 android.permission.ACCESS_CHECKIN_PROPERTIES //同意应用 ...

  3. DDD领域驱动设计初探

    DDD领域驱动设计初探1 前言:又有差不多半个月没写点什么了,感觉这样很对不起自己似的.今天看到一篇博文里面写道:越是忙人越有时间写博客.呵呵,似乎有点道理,博主为了证明自己也是忙人,这不就来学习下D ...

  4. 前端插件@user

    分享一个 @user 前端插件   开源地址:https://github.com/yuezhongxin/Mention.js 插件效果:类似于微博或 github 中 @user 列表效果. 这是 ...

  5. VARCHAR2 他们占几个字节? NLS_LENGTH_SEMANTICS,nls_language

    ORACLE初始化参数:NLS_LENGTH_SEMANTICS 初始化參数NLS_LENGTH_SEMANTICS用于指定CHAR列或VARCHAR2列的长度定义方式,默认值为BYTE. 当设置该參 ...

  6. java.util.Timer demo good

    package timer; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import org ...

  7. PHP使用MySQL数据库

    原文:PHP使用MySQL数据库 PHP使用MySQL数据库,从建立连接到结果的显示. 完整代码如下: <?php //连接MySQL $db = mysql_connect("loc ...

  8. easyUI的combobox实现级联

    先简介下combobox: easyUI重写了select,取而代之的是combobox,有例如以下几种方式能够创建一个combobox 1.使用select标签,并加上class="eas ...

  9. C++ 之 exception

    本文讲关于C++的异常的全部东西: 绝对不让异常逃离析构函数 阻止exception逃离析构函数,主要是两个原因: 1 防止在异常处理过程中的栈展开行为时,将调用terminate函数.程序将会结束, ...

  10. 收藏的一些javascript片段

    原文:收藏的一些javascript片段 学习js也很有一段时间,收集了一些js的片段.特地整理排版了一下,以一个js初学者的视界来分析注释了这些代码段,暂且不去讨论它的性能和优化问题,相信会对一些初 ...