Any help would be greatly appreciated!
Dan Carter
//-------------------------------------------------------------------------
// C# PhoneTest event source
using System;
using System.Runtime.InteropServices;
using System.Threading;
// Source interface with "event handlers" for COM objects to implement
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IPhoneEvents
{
void Ring();
}
// Delegate for the event
public delegate void RingEventHandler();
[ComSourceInterfaces(typeof(IPhoneEvents))]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Phone
{
public event RingEventHandler Ring;
public Thread fireMyEventThread;
public void BeginMyFireEventThread()
{
fireMyEventThread = new Thread(new ThreadStart(this.onRing));
fireMyEventThread.Start();
}
public void onRing()
{
int ii = 0;
while (ii < 3)
{
Ring();
Thread.Sleep(1000);
ii++;
}
}
}
//-------------------------------------------------------------------------
Simple VB6 Client that sinks the events as expected:
This works great... I get three pop up msgBoxes
Private WithEvents myPhone As Phone.Phone
Private Sub Form_Load()
Set myPhone = CreateObject("Phone")
End Sub
Private Sub myPhone_Ring()
MsgBox "Ring!"
End Sub
Private Sub Start_Click()
myPhone.BeginMyFireEventThread
End Sub
//-------------------------------------------------------------------------
// COM ATL EVENT SINK CLASS (CPhoneTestSink.h)
// NOTE: CPhoneTestSink.cpp has only the standard Invoke method implemented.
using namespace Phone;
class ATL_NO_VTABLE CPhoneTestSink :
public CComObjectRootEx<CComMultiThreadModel>,
public IDispatch
{
public:
BEGIN_COM_MAP(CPhoneTestSink)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY_IID(DIID_IPhoneEvents, IDispatch)
END_COM_MAP()
STDMETHOD(GetTypeInfoCount)(UINT* pctinfo);
STDMETHOD(GetTypeInfo)(UINT itinfo,
LCID lcid,
ITypeInfo** pptinfo);
STDMETHOD(GetIDsOfNames)(REFIID riid,
LPOLESTR* rgszNames,
UINT cNames,
LCID lcid,
DISPID* rgdispid);
STDMETHOD(Invoke)(DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS* pdispparams,
VARIANT* pvarResult,
EXCEPINFO* pexcepinfo,
UINT* puArgErr);
};
//-------------------------------------------------------------------------
// COM ATL CLIENT SIDE
HRESULT hr;
CComPtr<_Phone> m_pIPhone;
DWORD dwPhoneCookie;
CComPtr<IUnknown> pPhoneUnknown;
IUnknown *pUnkPhoneDispatchSink;
CComObject<CPhoneTestSink> *m_pPhoneEvents;
pPhoneUnknown = NULL;
dwPhoneCookie = NULL;
pUnkPhoneDispatchSink = NULL;
m_pPhoneEvents = NULL;
hr = m_pIPhone.CoCreateInstance(CLSID_Phone, NULL);
// hr is S_OK!
m_pIPhone->QueryInterface(IID_IUnknown,(void **)&m_pPhoneUnknown);
CComObject<CPhoneTestSink>::CreateInstance(&m_pPhoneEvents);
m_pPhoneEvents->QueryInterface(IID_IUnknown,
(void**)&m_pUnkPhoneDispatchSink);
if (m_pUnkPhoneDispatchSink)
{
hr = AtlAdvise(m_pPhoneUnknown,
m_pUnkPhoneDispatchSink,
DIID_IPhoneEvents,
&m_dwPhoneCookie);
// again, hr is S_OK! so AtlAdvise seems happy
}
m_pIPhone->BeginMyFireEventThread();
// I get an exception when I exit this thread
**If you made it this far, thank you!!**
--
dbcarter
The AtlAdvise will NOT work.. no matter what I tried... and I tried just
about everything!
hr = AtlAdvise(m_pPhoneUnknown,
m_pUnkPhoneDispatchSink,
DIID_IPhoneEvents,
&m_dwPhoneCookie);
Instead, get the Connection point directly...
LPCONNECTIONPOINTCONTAINER pCPC;
LPCONNECTIONPOINT pCP;
hr = m_pIPhone->QueryInterface(IID_IConnectionPointContainer,
(LPVOID*)&pCPC);
hr = pCPC->FindConnectionPoint(DIID_IPhoneEvents, &pCP);
hr = pCP->Advise(m_pPhoneSinkUnk, &m_dwPhoneCookie);
best of luck..
thanks for all the help from the interop community... not
I am experiencing the same problem what you have experienced sinking the .net events in the ATL COM.I was going through the Adam Nathan ".Net and COM" for the solution.but i didn't succeeded. VB6 client is working fine . But i have problems with ATL COM , seems my breakpoint never hit the COM sink event method.
I was seeing your sample,seems you got the solution to the COM Sink problem.
I have some queries regarding this dane,you mentioned ATLAdvise method will not work and try with connection points directly. But my point is, what are LPCONNECTIONPOINTCONTAINER and LPCONNECTIONPOINT supposed to be?Are they natively supported in vc++?
Can you help me with the full COM Code?
Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/