Pass C++ callback function into COM out-of-process method

176 Views Asked by At

There is already answered question about passing C++ callback into in-process COM method. But the same technique doesn't work for out-of-process usage.

I have a C# interface and it has a method which registers callback/delegate with two parameters.

void ProcessNotification(ConsoleNotificationType type, IntPtr parameter);

This C# interface is registered using Out-of-process COM configuration (exe-file). Now I want to pass a C++ callback function into that method, but Visual Studio gives me an error:

InvalidFunctionPointerInDelegate

So, how can I pass my C++ callback function into the Interface's registering callback function?

1

There are 1 best solutions below

8
Michael Gunter On

There's nothing in COM that will allow you to do this so simply. You really can't even do it in any programmatic paradigm that I can think of. A pointer in one process is meaningless in another process.

The official answer is to define a callback interface and pass a reference to that interface on an implementing object instead of passing a simple function pointer. And, of course, you have to make sure the interface can be proxied. (Alternatively, you can implement IConnectionPointContainer on your server, which is fundamentally the same idea.)