I have a c# COM server defined some delegate for event handling.
This is the sample code from MSDN:
using System;
using System.Runtime.InteropServices;
namespace Test_COMObject
{
public delegate void ClickDelegate(int x, int y);
public delegate void ResizeDelegate();
public delegate void PulseDelegate();
// Step 1: Defines an event sink interface (ButtonEvents) to be
// implemented by the COM sink.
[GuidAttribute("1A585C4D-3371-48dc-AF8A-AFFECC1B0967")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface MyButtonEvents
{
void Click(int x, int y);
void Resize();
void Pulse();
}
// Step 2: Connects the event sink interface to a class
// by passing the namespace and event sink interface
// ("EventSource.ButtonEvents, EventSrc").
[ComSourceInterfaces(typeof(MyButtonEvents))]
public class MyButton
{
public event ClickDelegate Click;
public event ResizeDelegate Resize;
public event PulseDelegate Pulse;
public MyButton()
{
}
public void CauseClickEvent(int x, int y)
{
Click(x, y);
}
public void CauseResizeEvent()
{
Resize();
}
public void CausePulse()
{
Pulse();
}
}
}
but msdn only provides a VB sample to implement COM client event sink, can anybody give me a sample how unmanaged c++ to do this?
below is the vb sample to implement COM event sink
' COM client (event sink)
' This Visual Basic 6.0 client creates an instance of the Button class and
' implements the event sink interface. The WithEvents directive
' registers the sink interface pointer with the source.
Public WithEvents myButton As Button
Private Sub Class_Initialize()
Dim o As Object
Set o = New Button
Set myButton = o
End Sub
' Events and methods are matched by name and signature.
Private Sub myButton_Click(ByVal x As Long, ByVal y As Long)
MsgBox "Click event"
End Sub
Private Sub myButton_Resize()
MsgBox "Resize event"
End Sub
Private Sub myButton_Pulse()
End Sub
Language runtimes often hide the extensive plumbing involved with receiving events from a COM object. But if you do this in raw C++ then you need to get started by querying the interface pointer for IConnectionPointContainer. That lets you enumerate all out-going interfaces. Which then lets you obtain an IConnectionPoint for a particular interface. You then call its Advise() method to set your sink interface that gets the callbacks. Which gives you back a cookie, you need to store it. For later when you want to unsubscribe with the Unadvise call.
It is described fairly well in the MSDN Library, start reading here.