Managed event, unmanaged sink

M

MLM450

I have created a .NET component in C# that has 2 interfaces. One is an
incoming interface with methods being exposed to COM-aware clients. The
other is an outgoing event interface that the unmanaged C++ sink
implements. Here is the C# code:

using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Diagnostics;

// Outgoing events
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IEvents
{
void OnMyEvent(int MyCode);

}

// Incoming methods
public interface INotify
{
int Code { get; set; }
}

public delegate void MyDelegate(int nCode);

// Notify class
[ComSourceInterfaces("IEvents")]
[ClassInterface(ClassInterfaceType.None)]
public class Notify : INotify
{
private int m_Code = 5;

public event MyDelegate OnMyEvent;

public Notify()
{
}

public int Code
{
get { return m_Code; }
set
{
m_Code = value;
if (value >= 10)
{
try
{
if (null != OnMyEvent)
{
OnMyEvent(m_Code);
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}

}
}
}
}

In my unmanaged C++ client, I can access the INotify interface just
fine. But I want to access the event interface too. How do I do that?

Currently, I define the C++ pointer as:
ClassLibrary1::INotifyPtr pNotify;

And then my C++ class constructor's initializer has:
pNotify(__uuidof(ClassLibrary1::Notify))

Any suggestions would be appreciated.
Thanks!
 
H

Holger Grund

public delegate void MyDelegate(int nCode);

// Notify class
[ComSourceInterfaces("IEvents")]
[ClassInterface(ClassInterfaceType.None)]
public class Notify : INotify [...]

In my unmanaged C++ client, I can access the INotify interface just
fine. But I want to access the event interface too. How do I do that?
I've never done that. But typically, events are implemented with
connection points. You QI for the IConnectionPointContainer
and find the connection point for the event interface. You
can establish a connection to your implementation of the event
interface via Advise.

However, implementing dispinterfaces is a bit of a pain in
VC++. Unless you have any hard reason to do so, you
should not us InterfaceIsIDispatch (if you really want
late-binding you can still use a dual interface).

Anyway, you might want to use ATL to implement the
interface. See AtlAdvise and BEGIN_SINK_MAP.

-hg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top