SAFEARRAY * *类型比较复杂,不像其他类型可以直接传入Fire事件方法里。
先看基本类型参数处理方法: 举例LONG型
HRESULT Fire_CloseCompleted(LONG MethodID)
{
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] = MethodID;
DISPPARAMS disp = { pvars, NULL, 1, 0 };
pDispatch->Invoke(0x60010003, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
}
}
delete[] pvars;
return varResult.scode;
}
蓝色字体是与参数相关语句。
再看SAFEARRAY * *类型的处理方法:
HRESULT Fire_MessageReceived(SAFEARRAY * * Message)
{
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].vt = VT_ARRAY | VT_BYREF | VT_UI1; //参数类型
pvars[0].pparray = Message; //参数值
DISPPARAMS disp = { pvars, NULL, 1, 0 };
pDispatch->Invoke(0x60010008, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
}
}
delete[] pvars;
return varResult.scode;
}