为ATL中生成的事件对象建立接收器(Sink)对象,必须要手工实现IDispatch的一些方法吗?内附代码,如何快捷的建立Sink对象?
描述:
我在ATL中实现了事件接口 _ICommuniEvents : public IDispatch
--------如何为其快捷的建立Sink对象?
ATL自动为其实现了Fire方法
HRESULT Fire_OnRecv(LONG lResult)
{
CComVariant varResult;
T* pT = static_cast<T*>(this);
int nConnectionIndex;
CComVariant* pvars = new CComVariant[1];
int nConnections = m_vec.GetSize();
for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++)
{
pT->Lock();
CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);
pT->Unlock();
IDispatch* pDispatch = reinterpret_cast<IDispatch*>(sp.p);
if (pDispatch != NULL)
{
VariantClear(&varResult);
pvars[0] = lResult;
DISPPARAMS disp = { pvars, NULL, 1, 0 };
pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
}
}
delete[] pvars;
return varResult.scode;
}
我在令一个程序中要实现Sink对象
class Sink : public _ICommuniEvents
{
public:
ULONG __stdcall AddRef();
ULONG __stdcall Release();
HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
public:
STDMETHOD(OnRecv)(long IResult);
public:
Sink():m_cRef(0){};
virtual ~Sink();
private:
long m_cRef;
};
可是如果加上以下代码编译就会出错:
Sink* pSink;
pSink= new Sink();
///////报错如下/////////////////////////////////
Compiling...
ClientDlg.cpp
(211) : error C2259: 'Sink' : cannot instantiate abstract class due to following members:
j:\我的文件\srcserver\client\sink.h(17) : see declaration of 'Sink'
(211) : warning C4259: 'long __stdcall IDispatch::GetTypeInfoCount(unsigned int *)' : pure virtual function was not defined
e:\microsoft visual studio\vc98\include\oaidl.h(2697) : see declaration of 'GetTypeInfoCount'
(211) : warning C4259: 'long __stdcall IDispatch::GetTypeInfo(unsigned int,unsigned long,struct ITypeInfo ** )' : pure virtual function was not defined
e:\microsoft visual studio\vc98\include\oaidl.h(2700) : see declaration of 'GetTypeInfo'
(211) : warning C4259: 'long __stdcall IDispatch::GetIDsOfNames(const struct _GUID &,unsigned short ** ,unsigned int,unsigned long,long *)' : pure virtual function was not defined
e:\microsoft visual studio\vc98\include\oaidl.h(2705) : see declaration of 'GetIDsOfNames'
(211) : warning C4259: 'long __stdcall IDispatch::Invoke(long,const struct _GUID &,unsigned long,unsigned short,struct tagDISPPARAMS *,struct tagVARIANT *,struct tagEXCEPINFO *,unsigned int *)' : pure virtua
l function was not defined
e:\microsoft visual studio\vc98\include\oaidl.h(2712) : see declaration of 'Invoke'
(211) : error C2259: 'Sink' : cannot instantiate abstract class due to following members:
j:\我的文件\srcserver\client\sink.h(17) : see declaration of 'Sink'
(211) : warning C4259: 'long __stdcall IDispatch::GetTypeInfoCount(unsigned int *)' : pure virtual function was not defined
e:\microsoft visual studio\vc98\include\oaidl.h(2697) : see declaration of 'GetTypeInfoCount'
(211) : warning C4259: 'long __stdcall IDispatch::GetTypeInfo(unsigned int,unsigned long,struct ITypeInfo ** )' : pure virtual function was not defined
e:\microsoft visual studio\vc98\include\oaidl.h(2700) : see declaration of 'GetTypeInfo'
(211) : warning C4259: 'long __stdcall IDispatch::GetIDsOfNames(const struct _GUID &,unsigned short ** ,unsigned int,unsigned long,long *)' : pure virtual function was not defined
e:\microsoft visual studio\vc98\include\oaidl.h(2705) : see declaration of 'GetIDsOfNames'
(211) : warning C4259: 'long __stdcall IDispatch::Invoke(long,const struct _GUID &,unsigned long,unsigned short,struct tagDISPPARAMS *,struct tagVARIANT *,struct tagEXCEPINFO *,unsigned int *)' : pure virtua
l function was not defined
e:\microsoft visual studio\vc98\include\oaidl.h(2712) : see declaration of 'Invoke'
Error executing cl.exe.
Creating browse info file...