【发布时间】:2011-08-28 07:11:00
【问题描述】:
我的 ActiveX/ATL 库中有 2 个事件
dispinterface _IMyEvents
{
properties:
methods:
[id(1), helpstring("method OnReceiveData")] HRESULT OnReceiveData([in] long BytesReceived, [in, out] VARIANT_BOOL * Proceed);
[id(2), helpstring("method OnReceiveDataEx")] HRESULT OnReceiveDataEx([in] long BytesReceived, [in] BSTR DataChunk, [in, out] VARIANT_BOOL * Proceed);
};
它们以标准 ATL 的方式调用:
template <class T>
class CProxy_IMyEvents : public IConnectionPointImpl<T, &DIID__IMyEvents, CComDynamicUnkArray>
{
//Warning this class may be recreated by the wizard.
public:
HRESULT Fire_OnReceiveData(LONG BytesReceived, VARIANT_BOOL * Proceed)
{
CComVariant varResult;
T* pT = static_cast<T*>(this);
int nConnectionIndex;
CComVariant* pvars = new CComVariant[2];
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[1] = BytesReceived;
pvars[0].vt = VT_BYREF|VT_BOOL;
pvars[0].pboolVal = Proceed;
DISPPARAMS disp = { pvars, NULL, 2, 0 };
pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
}
}
delete[] pvars;
return varResult.scode;
}
我需要知道应用程序是否特别订阅了 OnReceiveDataEx 事件(它有额外的参数 DataChunk 需要计算)。如果应用只监听 OnReceiveData,我不需要构建 DataChunk 字符串,因为没有人会得到它,并且可以优化性能。
但是,ATL 只允许我知道是否有人订阅了任何事件,但不能知道具体是哪些事件(因此我只能确定监听我的事件的对象(接收器)的数量,而不是数量和正在收听的特定事件的名称)。有什么办法可以克服吗?
例如,在 .net 中,您可以独立于其他事件检查订阅者的事件。
【问题讨论】: