0

I have created COM server in C++ that is used from C# application. Now I have to add one more method to my COM object and one of its parameter must be function pointer to callback function. C++ original function looks like this:

typedef int (__stdcall *tCallBackFunc)(int Param);

void MyFunc(void* CallBackFunc)
{
  int Param;
  //some code
  ((tCallBackFunc)(CallBackFunc))(Param); //call callback function
}

I need to add MyFunc as COM object method. VS VC++ "Add Method" dialog offers some predefined types for COM method parameter like "int*" or "bstr", I tried to define "CallBackFunc" parameter as "int*" type, but I was not able to make it work in C#:

public delegate int  tCallBackFunc(int Param);
...
tCallBackFunc cb; //I will initialize it when it is compiled
MyCOMObject.MyFunc(cb); //cannot find any way to cast cb to "ref int"

It seems I use wrong approach, is there a fast way to declare function pointer as parameter of COM object method in C++ and then use it in C#?

2 Answers 2

7

The COM way is to make the parameter an interface pointer, and have the interface declare the callback function.

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

1 Comment

Sounds like not an easy solution, I will have to create an interface and then use it in C#. I look for the fastest way, I found some articles how to use EnumWindows API in C# (it requires callback function), it would be fine for me to use the same way if possible, but I cannot declare (redeclare) COM method via [DllImport]
1

+1 for Frederik's answer.

@Michael: It will prove a lot easier than your imagined solution. Especially with

  • object lifetimes
  • process boundaries
  • thread safety (apartment boundaries, IOW)
  • standards conformance (casting function pointers quickly enters the domain of Undefined Behaviour). Why should you worry? Standards conformance must concern you in case a compiler update introduces another optimization that makes your old (non-conformant) code break

4 Comments

Ok, can you please give me some link that demonstrates how to create interface in c++ to support callback and then use it from C#? I googled but found no good articles, may be I used wrong keywords as I don't know what I need exactly to use the solution you recommend. I'm afraid it will take a lot of time, if so I will use my C++ DLL via [DllImport] instead of COM.
I don't have a sample, but it is quite simple: it is just a COM interface (going the other way: you pass it to the 'server' to call you). It is quite similar to a Source Interface (IDL/COM terminology) which might help you googling relevant examples. (But using a callback interface is way simpler in practice than actual COM source interfaces; that COM technology really only helps adding 'syntactic sugar' to events when consuming them from languages like, e.g. VB because they will be exposed as regular Events. You might be interested in that, by the way.
I would start here: msdn.microsoft.com/en-us/library/1hee64c7.aspx C# appears to wrap and handle the connection point mechanisms very well.
@KenBrittain Thx! `` @Michael for the record source interfaces and event sinks are referring to the same technology of connection points

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.