5

I've been following the 'tutorials' of how to expose a .NET framework through COM ( http://msdn.microsoft.com/en-us/library/zsfww439.aspx and http://msdn.microsoft.com/en-us/library/bd9cdfyx.aspx ). Everything works except for the events part. When I add events to the C# interface the following C++ code is generated:

struct __declspec(uuid("..."))
_MessageEventHandler : IDispatch
{};

struct __declspec(uuid("..."))
IConnection : IDispatch
{
  virtual HRESULT __stdcall add_MessageEvent (
    /*[in]*/ struct _MessageEventHandler * value ) = 0;
  virtual HRESULT __stdcall remove_MessageEvent (
    /*[in]*/ struct _MessageEventHandler * value ) = 0;
}

The problem is that I haven't found any info on how to use this in C++. Do I need to derive from _MessageEventHandler and implement operator()? Or something else entirely?

(Note that for the moment I'm also trying the more documented approach of using IConnectionPointContainer and IConnectionPoint.)

1
  • You probably expected to derive from _MessageEventHandler and get a call on IDispatch::Invoke() for the event - but the question is with what DISPID? Commented Jan 23, 2010 at 4:04

2 Answers 2

2

It has been a long time since I used COM and at that time I was using Visual C++ 6.0. I remember that implementing sinks for COM connection points was not a straightforward process. There were multiple ways for implementing them, depending if you used MFC or ATL. Maybe there are easier ways now. Here are couple of links that can help you:

Code Project - Sinking events from managed code in unmanaged C++
Code Project - COM - large number of articles about COM
Code Project - Handling COM Events in a Console Application
Code Project - Handling COM Events in a Console Application, Part II

Sign up to request clarification or add additional context in comments.

Comments

1

IDispatch is used for runtime binding languages like VB, you wouldn't normally need to do this for a strongly typed language like c#.

When you call a method through IDispatch, What you actually do is build an array containing the method id (called a dispid) and parameters, then hand that to a function that searches through a table of methods by dispid, when it finds one, it uses your parameter array to build a callstack and then call the method. (This is a oversimplification, of course).

So knowing that a class implements IDispatch tells you almost nothing.

So this is ether a false lead, or you are missing the declaration for the MessageEventHandler dispatch tables.

It's not at all surprising that you can't figure out how to implement from this, you are missing some vital information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.