Ahh, delegates! Those are even better than thunks, because I don't have to
pass the MonoObject in too.
Unfortunately, I can't get those to work at all. To my method called by C#
to register the managed object, I added a parameter that takes a delegate.
In C#, I call it like this:
var mc = new MainClass();
native_RegisterObject(mc, mc.Add);
On the C++ side, I have this:
typedef float (*MonoAddFunc)(float, float);
void native_RegisterObject(MonoObject *mobj, MonoAddFunc delegate) {
g_mobj = mobj;
g_delegate = delegate;
}
In the debugger, delegate is a non-zero number. I was using
native_RegisterObject as an internal call, but I noticed in your code here
you had "DllImport(...)" inside your C# for calling the native method. I
read more in the documentation that the difference between P/Invoke and
internal calls is that P/Invoke does more marshaling, which is seems like I
need in this case to marshal the delegate into a C++ function pointer.
However, when I run that, I get an EntryPointNotFound exception. I put an
extern "C" block around my native_RegisterObject, but that didn't help.
In C#, this is how I declare the native function:
private delegate float AddDelegate(float a, float b);
[DllImport("__Internal", EntryPoint="native_registerObject")]
private static extern void native_registerObject(MainClass instance,
AddDelegate add);
And then I call that as above.
Can I use internal calls for registering a delegate? If not, how do I
expose my native functions such that the runtime can find them in the image
that's running?
Thanks for you help, Robert!
-Tom
--
View this message in context:
http://mono.1490590.n4.nabble.com/NullReferenceException-when-calling-thunk-tp4658230p4658247.html
Sent from the Mono - General mailing list archive at Nabble.com._______________________________________________
Mono-list maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list