【转载】COM 连接点
原文:COM 连接点
CLR 完全介绍 COM 连接点 Thottam R. Sriram
来自:http://msdn.microsoft.com/zh-cn/magazine/cc163361.aspx#S1
代码下载位置: CLRInsideOut2007_09.exe (252 KB)
HRESULT Add(int nFirst, int nSecond)
HRESULT AdditionStarted()
HRESULT AdditionCompleted(int nResult)
HRESULT Add([in] int nFirst, [in] int nSecond)
library ATLConnectionPointServerLib
{
importlib("stdole2.tlb");
[
uuid(7F45FEA6-4D7C-489C-A852-19BA8B29D8AB),
helpstring("_IAddEvents Interface")
]
dispinterface _IAddEvents
{
properties:
methods:
[id(1), helpstring("AdditionStarted")]HRESULT AdditionStarted();
[id(2), helpstring("AdditionStarted")]
HRESULT AdditionCompleted(int nResult);
};
[
uuid(15B6C26A-0416-4C8F-9533-89F318355E31),
helpstring("Add Class")
]
coclass Add
{
[default] interface IAdd;
[default, source] dispinterface _IAddEvents;
};
};
STDMETHODIMP CAdd::Add(int nFirst, int nSecond)
{
// Fire AdditionStarted event
Fire_AdditionStarted();
int nResult = nFirst + nSecond;
Sleep(1000); // simulate the addition taking a long time
// Fire AdditionCompleted event
Fire_AdditionCompleted(nResult);
return S_OK;
}
- 它必须向您提供 _IAddEvents 接口的实现。
- 它必须向您提供指向服务器 Add 接口的接口指针。
- 它必须获取 Add 接口 ConnectionPoinContainer 的 ConnectionPoint,并添加接收器接口。
- 它必须调用 Add 方法,并等待服务器事件被触发。
- 它必须彻底关闭并退出。
class CSink : _IAddEvents
{
private:
DWORD m_dwRefCount;
public:
CSink::CSink() {m_dwRefCount = 0;}
CSink::~CSink() {}
HRESULT STDMETHODCALLTYPE AdditionStarted()
{
printf("C++ SINK: Addition started event fired ... \n");
return S_OK;
};
HRESULT STDMETHODCALLTYPE AdditionCompleted(int nResult)
{
printf("C++ SINK: Addition completed event fired ... \n");
printf("C++ SINK: Addition result: %d \n",nResult);
return S_OK;
};
...
- 获取指向服务器 Add 接口的接口指针。
- 从 Add 接口获取 ConnectionPointContainer 的 ConnectionPoint。
- 向服务器注册接收器接口。
CoInitialize(NULL);
hr = CoCreateInstance(
CLSID_Add, NULL, CLSCTX_ALL, IID_IAdd, (void **)&pAdd);
if(hr != S_OK) { return; }
// Using the interface for add,
// query for IConnectionPointContainer interface
hr = pAdd->QueryInterface(
IID_IConnectionPointContainer,(void **)&pCPC);
if ( !SUCCEEDED(hr) ) { return; }
// Using the IConnectionPointContainer,
// get the IConnectionPoint interface
hr = pCPC->FindConnectionPoint(DIID__IAddEvents,&pCP);
if ( !SUCCEEDED(hr) ) { return; }
// Create an instance of the sink object to pass
// to the server
pSink = new CSink();
if ( NULL == pSink ) { return; }
// Get the interface pointer to CSink's IUnknown pointer, which you
// will pass to the server
hr = pSink->QueryInterface (IID_IUnknown,(void **)&pSinkUnk);
if(!SUCCEEDED(hr)) { return; }
// Pass the sink interface to the server through the Advise
hr = pCP->Advise(pSinkUnk,&dwAdvise);
if(!SUCCEEDED(hr)) { return; }
pAdd->Add(1, 5);
// Release the IConnectionPointContainer interface.
if(pCPC != NULL) pCPC->Release();
// Unadvise the event call back we registered.
if(pCP != NULL) { pCP->Unadvise(dwAdvise); }
if(pSinkUnk != NULL) { pSinkUnk->Release(); }
// Disconnect from the server.
if(pCP != NULL) { pCP->Release(); }
// Release interfaces.
if(pAdd != NULL) { pAdd->Release(); }
CoUninitialize();
return;
cl COMConnectionPointClient.cpp
C++ SINK: Addition started event fired ...
C++ SINK: Addition completed event fired ...
C++ SINK: Addition result: 6
tlbimp ATLConnectionPointServer.dll
public class ManagedSink :_IAddEvents
{
public void AdditionStarted()
{
Console.WriteLine("C# SINK: Addition started event fired ...");
}
public void AdditionCompleted(int nResult)
{
Console.WriteLine("C# SINK: Addition completed event fired ...");
Console.WriteLine("C# SINK: Addition result: {0}", nResult);
return;
}
};
// Pass the sink interface to the server through the Advise
hr = pCP->Advise(pSinkUnk,&dwAdvise);
if(!SUCCEEDED(hr)) { return;}
ManagedSink ms = new ManagedSink();
AddClass a = new AddClass();
a.AdditionStarted += ms.AdditionStarted;
a.AdditionCompleted += ms.AdditionCompleted;
a.Add(1, 5);
a.AdditionStarted -= ms.AdditionStarted;
a.AdditionCompleted -= ms.AdditionCompleted;
csc /r:ATLConnectionPointServerLib.dll ManagedClient.cs
C# SINK: Addition started event fired ...
C# SINK: Addition completed event fired ...
C# SINK: Addition result: 6
【转载】COM 连接点的更多相关文章
- 【转载】COM 组件设计与应用(十六)——连接点(vc.net)
原文:http://vckbase.com/index.php/wv/1257.html 一.前言 上回书介绍了回调接口,在此基础上,我们理解连接点就容易多了. 二.原理 图一.连接点组件原理图.左侧 ...
- 【转载】COM 组件设计与应用(十五)——连接点(vc6.0)
原文:http://vckbase.com/index.php/wv/1256.html 一.前言 上回书介绍了回调接口,在此基础上,我们理解连接点就容易多了. 二.原理 图一.连接点组件原理图.左侧 ...
- [转载]Office Visio快捷键
“帮助”任务窗格和“帮助”窗口 使用“帮助”任务窗格和“帮助”窗口 通过“帮助”任务窗格,您可以访问“Microsoft Office Visio 帮助”的全部内容,该窗格显示为 Microsoft ...
- c#winform使用WebBrowser 大全[超长文转载]
1.主要用途:使用户可以在窗体中导航网页. 2.注意:WebBrowser 控件会占用大量资源.使用完该控件后一定要调用 Dispose 方法,以便确保及时释放所有资源.必须在附加事件的同一线程上调用 ...
- [转载] ZooKeeper实现分布式队列Queue
转载自http://blog.fens.me/zookeeper-queue/ 让Hadoop跑在云端系列文章,介绍了如何整合虚拟化和Hadoop,让Hadoop集群跑在VPS虚拟主机上,通过云向用户 ...
- [matlab] 18.图与网络 (转载)
基本概念: 图论[Graph Theory]是数学的一个分支.它以图为研究对象.图论中的图是由若干给定的点及连接两点的线所构成的图形,这种图形通常用来描述某些事物之间的某种特定关系,用点代表事物,用连 ...
- 【转载】COM小结
原文:http://blog.csdn.net/byxdaz/article/details/6595210 一.Com概念 所谓COM(Componet Object Model,组件对象模型),是 ...
- GetLastError 错误码大全(转载)
转载自:GetLastError GetLastError GetLastError返回的值通过在api函数中调用SetLastError或SetLastErrorEx设置.函数 并无必要设置上一 ...
- ConcurrentHashMap源码分析(JDK8版本<转载>)
注:本文源码是JDK8的版本,与之前的版本有较大差异 转载地址:http://blog.csdn.net/u010723709/article/details/48007881 ConcurrentH ...
随机推荐
- 【ubuntu】首选项和应用程序命令(preference & application)
gnome-control-center 系统设置 gnome-control-center region 键盘布局 gnome-control-center screen 屏幕 gno ...
- Watir资源列表【转】
Watir简介 "Watir" (发音与 water相近) 全写是 "Web Application Testing in Ruby".Watir是一款用Rub ...
- 利用foreach对页面控件的遍历 及三目运算符的使用
1.利用foreach对页面控件的遍历 及三目运算符的使用 利用div将一组CheckBox放在一起用于遍历 <body> <form id="form1" ru ...
- SlidingMenu实现app侧滑功能
很多APP都有侧滑菜单的功能,部分APP左右都是侧滑菜单~SlidingMenu 这个开源项目可以很好帮助我们实现侧滑功能,如果对SlidingMenu 还不是很了解的童鞋,可以参考下本篇博客. 本片 ...
- Sed文本替换一例
使用 Sed 完成文本替换操作任务是非常合适的. 现在, 假设我要将一个原有 Java 项目中的一些包及下面的类移到另一个项目中复用. Project javastudy: Packages: alg ...
- 前端开发自动化工作流工具,JavaScript自动化构建工具grunt、gulp、webpack介绍
前端开发自动化工作流工具,JavaScript自动化构建工具grunt.gulp.webpack介绍 前端自动化,这样的一个名词听起来非常的有吸引力,向往力.当今时代,前端工程师需要维护的代码变得及为 ...
- is_user_logged_in()
function is_user_logged_in() { $user = wp_get_current_user(); return $user->exists(); } wp_get_cu ...
- 模块已加载,但对dllregisterServer的调用失败
在注册dll或者ocx的时候, 经常会遇到这么一个问题: 模块 已加载,但对dllregisterServer的调用失败,错误代码为0x8004***** 网上有网友回复说需要在管理员的模式下进行注 ...
- java技术用ssh从linux服务器下载数据
通常需要从linux服务器获取数据文件,而通常能有的访问方式只有ssh,所以就可以用ssh进行数据下载. java连接远程主机的方式有多种,这里和大家分享的是通过ssh方式连接远程主机,使用的是jsc ...
- Centos7 安装配置NFS
一.安装 NFS 服务器所需的软件包 # yum install -y nfs-utils 二.编辑exports文件 # vim /etc/exports /data/disk1/video *(a ...