请问带有事件的普通COM怎么使用

zzutnicholas 2009-10-26 10:15:22
一个C#开发的带有事件的COM,我通过VC的import得到tlh文件如下

// Created by Microsoft (R) C/C++ Compiler Version 15.00.30729.01 (16016968).
//
// f:\my documents\visual studio 2008\projects\ex_vs9_com\test3\debug\cscomtest.tlh
//
// C++ source equivalent of Win32 type library cscomtest.tlb
// compiler-generated file created 10/21/09 at 18:28:40 - DO NOT EDIT!

#pragma once
#pragma pack(push, 8)

#include <comdef.h>

namespace cscomtest {

//
// Forward references and typedefs
//

struct __declspec(uuid("0b8a6e50-19e5-4b64-8fcc-c45199656fe6"))
/* LIBID */ __cscomtest;
struct __declspec(uuid("47c976e0-c208-4740-ac42-41212d3c34f0"))
/* dispinterface */ MyCom_Events;
struct __declspec(uuid("368c19c3-a726-49b5-a2d8-482680adda1d"))
/* dual interface */ IMyCom;
struct /* coclass */ MyCom;
struct /* coclass */ OKHander;
struct __declspec(uuid("4c0dcd67-7bd0-3249-86e8-cedc6f13e676"))
/* dual interface */ _OKHander;

//
// Smart pointer typedef declarations
//

_COM_SMARTPTR_TYPEDEF(MyCom_Events, __uuidof(MyCom_Events));
_COM_SMARTPTR_TYPEDEF(IMyCom, __uuidof(IMyCom));
_COM_SMARTPTR_TYPEDEF(_OKHander, __uuidof(_OKHander));

//
// Type library items
//

struct __declspec(uuid("47c976e0-c208-4740-ac42-41212d3c34f0"))
MyCom_Events : IDispatch
{
//
// Wrapper methods for error-handling
//

// Methods:
HRESULT OK ( );
};

struct __declspec(uuid("368c19c3-a726-49b5-a2d8-482680adda1d"))
IMyCom : IDispatch
{
//
// Wrapper methods for error-handling
//

long Add (
long a,
long b );
HRESULT ThrowException1 ( );
HRESULT ThrowException2 ( );
HRESULT ThrowException3 ( );
HRESULT OnOK ( );

//
// Raw methods provided by interface
//

virtual HRESULT __stdcall raw_Add (
/*[in]*/ long a,
/*[in]*/ long b,
/*[out,retval]*/ long * pRetVal ) = 0;
virtual HRESULT __stdcall raw_ThrowException1 ( ) = 0;
virtual HRESULT __stdcall raw_ThrowException2 ( ) = 0;
virtual HRESULT __stdcall raw_ThrowException3 ( ) = 0;
virtual HRESULT __stdcall raw_OnOK ( ) = 0;
};

struct __declspec(uuid("498627af-bcd0-4a1f-95e4-739953847b60"))
MyCom;
// interface _Object
// [ default ] interface IMyCom
// [ default, source ] dispinterface MyCom_Events

struct __declspec(uuid("f2ab1b1c-26c1-3a16-933e-f52135b61f45"))
OKHander;
// [ default ] interface _OKHander
// interface _Delegate
// interface _Object
// interface ICloneable
// interface ISerializable

struct __declspec(uuid("4c0dcd67-7bd0-3249-86e8-cedc6f13e676"))
_OKHander : IDispatch
{};

//
// Named GUID constants initializations
//

extern "C" const GUID __declspec(selectany) LIBID_cscomtest =
{0x0b8a6e50,0x19e5,0x4b64,{0x8f,0xcc,0xc4,0x51,0x99,0x65,0x6f,0xe6}};
extern "C" const GUID __declspec(selectany) DIID_MyCom_Events =
{0x47c976e0,0xc208,0x4740,{0xac,0x42,0x41,0x21,0x2d,0x3c,0x34,0xf0}};
extern "C" const GUID __declspec(selectany) IID_IMyCom =
{0x368c19c3,0xa726,0x49b5,{0xa2,0xd8,0x48,0x26,0x80,0xad,0xda,0x1d}};
extern "C" const GUID __declspec(selectany) CLSID_MyCom =
{0x498627af,0xbcd0,0x4a1f,{0x95,0xe4,0x73,0x99,0x53,0x84,0x7b,0x60}};
extern "C" const GUID __declspec(selectany) CLSID_OKHander =
{0xf2ab1b1c,0x26c1,0x3a16,{0x93,0x3e,0xf5,0x21,0x35,0xb6,0x1f,0x45}};
extern "C" const GUID __declspec(selectany) IID__OKHander =
{0x4c0dcd67,0x7bd0,0x3249,{0x86,0xe8,0xce,0xdc,0x6f,0x13,0xe6,0x76}};

//
// Wrapper method implementations
//

#include "f:\my documents\visual studio 2008\projects\ex_vs9_com\test3\debug\cscomtest.tli"

} // namespace cscomtest

#pragma pack(pop)



虽然我会调用这个COM,但是不知道怎么使用里面的事件

// TODO: code your application's behavior here.
try
{
CoInitialize(NULL);//NULL换成0也可以
cscomtest::IMyComPtr pIMyCom(__uuidof(cscomtest::MyCom));
cout<<"out:"<<pIMyCom->Add(1,2)<<endl;
//pIMyCom->ThrowException1();
//cscomtest::_OKHanderPtr
IConnectionPointContainer* pp;
pIMyCom.QueryInterface(IID_IConnectionPointContainer,(void**)&pp);

IConnectionPoint* p2;
pp->FindConnectionPoint(DIID_MyCom_Events,&p2);


CoUninitialize();
}
catch (_com_error& e)
{
cerr<<e.Description();
}


谢谢回帖!
...全文
126 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
oyljerry 2009-10-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zzutnicholas 的回复:]
引用 10 楼 tttyd 的回复:
引用 8 楼 zzutnicholas 的回复:
引用 7 楼 tttyd 的回复:
1. 那个例子里面应该没有test3.cpp文件啊,你自己写的吧
2. CSink在上面的例子中并不是抽象类,到你这里怎么成了抽象类了。


高人您真是细心啊,源代码确实是有问题:
其中的User project

Error 1 error C2259: 'CSink' : cannot instantiate abstract class f:\my documents\visual studio 2008\projects\comtut15src\comtut15src\use\usedlg.h 53 Use


不知道 你做了什么修改。
那demo代码可以直接编译的,我这没有任何问题。
你出现的这个错误估计是你自己定义实现了CSink类,从IDispatch派生,但是IDispatch中的7个方法你在CSink类没有完全实现?



高人,我说的是下载的源码,我没有做任何改动

唯一不同的是源码是VC6的,我用的是VS2008,做了自动的转换,请问会有影响吗?
[/Quote]
转换不会影响代码,只会转换工程文件...你还是要检察一下出错的代码
zzutnicholas 2009-10-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 tttyd 的回复:]
引用 8 楼 zzutnicholas 的回复:
引用 7 楼 tttyd 的回复:
1. 那个例子里面应该没有test3.cpp文件啊,你自己写的吧
2. CSink在上面的例子中并不是抽象类,到你这里怎么成了抽象类了。


高人您真是细心啊,源代码确实是有问题:
其中的User project

Error 1 error C2259: 'CSink' : cannot instantiate abstract class f:\my documents\visual studio 2008\projects\comtut15src\comtut15src\use\usedlg.h 53 Use


不知道 你做了什么修改。
那demo代码可以直接编译的,我这没有任何问题。
你出现的这个错误估计是你自己定义实现了CSink类,从IDispatch派生,但是IDispatch中的7个方法你在CSink类没有完全实现?

[/Quote]

高人,我说的是下载的源码,我没有做任何改动

唯一不同的是源码是VC6的,我用的是VS2008,做了自动的转换,请问会有影响吗?
雪影 2009-10-26
  • 打赏
  • 举报
回复
这里有一个例子

http://www.vckbase.com/document/viewdoc/?id=1538
tator 2009-10-26
  • 打赏
  • 举报
回复
具体怎么做呢?我想如果有事件发生会自动响应相关的回调!
雪影 2009-10-26
  • 打赏
  • 举报
回复
需要在客户端实现DIID_MyCom_Events事件调用的接口。响应COM组件的回调事件。
oyljerry 2009-10-26
  • 打赏
  • 举报
回复
你建立连接点后,就要实现DIID_MyCom_Events对应接口的方法中进行事件响应.
jameshooo 2009-10-26
  • 打赏
  • 举报
回复
需要自己实现MyCom_Events接口才能被回调
雪影 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zzutnicholas 的回复:]
引用 7 楼 tttyd 的回复:
1. 那个例子里面应该没有test3.cpp文件啊,你自己写的吧
2. CSink在上面的例子中并不是抽象类,到你这里怎么成了抽象类了。


高人您真是细心啊,源代码确实是有问题:
其中的User project

Error 1 error C2259: 'CSink' : cannot instantiate abstract class f:\my documents\visual studio 2008\projects\comtut15src\comtut15src\use\usedlg.h 53 Use

[/Quote]
不知道 你做了什么修改。
那demo代码可以直接编译的,我这没有任何问题。
你出现的这个错误估计是你自己定义实现了CSink类,从IDispatch派生,但是IDispatch中的7个方法你在CSink类没有完全实现?
wshcdr 2009-10-26
  • 打赏
  • 举报
回复
实现sink
zzutnicholas 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 tttyd 的回复:]
1. 那个例子里面应该没有test3.cpp文件啊,你自己写的吧
2. CSink在上面的例子中并不是抽象类,到你这里怎么成了抽象类了。
[/Quote]

高人您真是细心啊,源代码确实是有问题:
其中的User project

Error 1 error C2259: 'CSink' : cannot instantiate abstract class f:\my documents\visual studio 2008\projects\comtut15src\comtut15src\use\usedlg.h 53 Use
雪影 2009-10-26
  • 打赏
  • 举报
回复
1. 那个例子里面应该没有test3.cpp文件啊,你自己写的吧
2. CSink在上面的例子中并不是抽象类,到你这里怎么成了抽象类了。
zzutnicholas 2009-10-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tttyd 的回复:]
这里有一个例子

http://www.vckbase.com/document/viewdoc/?id=1538
[/Quote]

非常感谢你!

但是有个问题,我按照您给的网页download里面的例子,用VS2008打开编译,说:
Error 1 error C2259: 'CSink' : cannot instantiate abstract class f:\my documents\visual studio 2008\projects\ex_vs9_com\test3\test3.cpp 83 test3

是不是在VS2005、VS2008里要做一些改动

请问该如何改动呢?

谢谢

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧