0

I need to be able to pass a context object when i register a call back using the following method, which is linked to a C++ DLL file so cant change the method parameters. The callback returns this as one of the arguments when the callback method is executed.

public static void RegisterCallback(UInt32 instance, CALLBACK callbackFunction, UInt32 context)

The last argument is the only one I'm having trouble with. I'm assuming that its meant to be the memory address (or a reference) of the object.

The C++ code has this argument as void*

Any ideas?

Carlos
  • 938
  • 7
  • 12

1 Answers1

2

This is very common in a C-style api that permits registering a callback. It plays the exact same role as, say, IAsyncResult.AsyncState in managed code for example. The callback gets the value back, it can use it for any purpose it likes. Could be an index in an array of objects for example. Could be a state variable. In the case of C++, it very commonly is the this pointer of the C++ object. Very handy, permits calling an instance function of the C++ object. Anything goes.

Do note that it is pretty unlikely that you need it. First hint that you don't is that you just don't know what to use it for. And that's pretty likely in C# because you already have context. The delegate you use for the callbackFunction argument already captures the this object reference. So any state you need in the callback method can already be provided by fields of the class. Just like C++, minus the extra call.

So don't worry about it, pass 0. Or really, fix the declaration, the parameter should be IntPtr, pass IntPtr.Zero. Probably also what you should do for the instance argument, it quacks like a "handle". A pointer under the hood.

And be careful with the callbackFunction argument, you need to make sure that the garbage collector does not destroy the delegate object. It does not know that the native code is using it. You have to store it in a static variable or call GCHandle.Alloc() so it is always referenced.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks for the tip, you were correct i was able to get access the corresponding callback (there are multiple callbacks) by other means. – Carlos Mar 06 '16 at 21:23