I am trying to use a P/Invoke method on Mono using Ubuntu 14.04:
C++ part:
#define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport)
EXTERN_DLL_EXPORT int SomeMethod(int num);
// and .cpp file with the actual implementation
C# part:
[DllImport(@"TestProj")]
extern static int SomeMethod(int n);
Console.WriteLine(SomeMethod(2));
However if I try to invoke the method, I always get NullReferenceException
, I am wondering how can I find out if the exception was raised because P/Invoke failed, maybe because it wasn't able to properly load the method, or the null reference actually occurred inside the SomeMethod
.
Thanks
If the shared (native) library is not found, you receive:
If you have a entry point mismatch you would receive a:
If the shared library was crashing you would never get the framework null ref.
So, the .so is being loaded and the 'c' function is being called, but something in the mono framework is throwing a fit. Marshaling interop is the first place I'd look. There is some mismatch between what you are passing from C# to Cpp or back... If the sample you give is true, just 'int', not pointers/structs/etc.. then it should just work.
The simplest Interop case HelloWorld I can create, give it a true and see what happens:
Should should be 1 and no errors...