Im' trying to fire events from a C++ ATL COM Object, but the code that gets generated by the "Implement Connection Points" option in VC++ doesn't work.
It generates the following code:
VOID Fire_ConnectedToTelephonyServerOrNot(LONG errorLevel, BSTR errorString, BSTR statusString)
{
T* pT = static_cast<T*>(this);
int nConnectionIndex;
CComVariant* pvars = new CComVariant[3];
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)
{
pvars[2] = errorLevel;
pvars[1] = errorString;
pvars[0] = statusString;
DISPPARAMS disp = { pvars, NULL, 3, 0 };
pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, NULL, NULL, NULL);
}
}
delete[] pvars;
}
However, when this is run the call to pDispatch->Invoke(...) causes an "Instruction at ... referenced memory at ... . The memory could not be 'read'." exception when the calling .exe is run.*
After a look at the code, my team-mate decided that the line ...
IDispatch* pDispatch = reinterpret_cast<IDispatch*>(sp.p);
... should be changed to use a dynamic_cast instead, ....
IDispatch* pDispatch = dynimic_cast<IDispatch*>(sp.p);
... and we then found that the dynamic cast causes an exception, because pDispatch is not the correct type.
Can anyone tell me how to go about firing events from an ATL COM Object? (They should be received by Visual Basic).
Thanks in Advance,
Stephen
*=[For extra weirdness... If the calling app. is run in debug-mode (in Visual Basic), the event will fire okay, and then after Visual Basic is closed, the memory read exception dialogue appears.]
It generates the following code:
VOID Fire_ConnectedToTelephonyServerOrNot(LONG errorLevel, BSTR errorString, BSTR statusString)
{
T* pT = static_cast<T*>(this);
int nConnectionIndex;
CComVariant* pvars = new CComVariant[3];
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)
{
pvars[2] = errorLevel;
pvars[1] = errorString;
pvars[0] = statusString;
DISPPARAMS disp = { pvars, NULL, 3, 0 };
pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, NULL, NULL, NULL);
}
}
delete[] pvars;
}
However, when this is run the call to pDispatch->Invoke(...) causes an "Instruction at ... referenced memory at ... . The memory could not be 'read'." exception when the calling .exe is run.*
After a look at the code, my team-mate decided that the line ...
IDispatch* pDispatch = reinterpret_cast<IDispatch*>(sp.p);
... should be changed to use a dynamic_cast instead, ....
IDispatch* pDispatch = dynimic_cast<IDispatch*>(sp.p);
... and we then found that the dynamic cast causes an exception, because pDispatch is not the correct type.
Can anyone tell me how to go about firing events from an ATL COM Object? (They should be received by Visual Basic).
Thanks in Advance,
Stephen
*=[For extra weirdness... If the calling app. is run in debug-mode (in Visual Basic), the event will fire okay, and then after Visual Basic is closed, the memory read exception dialogue appears.]