I have a problem with a P/Invoke call I’m trying to do. I have to call a C++ class from a C# program. I have the source of this class so what I did is put it in a DLL project and create export functions to access it’s main method. That should be enough to do what I need and keep things simple.
My export method looks like this :
extern "C" _declspec(dllexport) void Inference(double *c1, double *c2, double *c3, double *result)
{
/* somecode */
}
This compiles, and I can see the export in a dumpbin output.
Now the problem is, I can’t call this method from my C# code because I always get a PInvokeStackInbalance exception, telling me that
This is likely because the managed
PInvoke signature does not match the
unmanaged target signature.
I tried calling the method with this :
[DllImport("InferenceEngine.dll")]
extern static unsafe void Inference(double *c1, double *c2, double *c3, double *result);
I also tried this :
[DllImport("InferenceEngine.dll")]
extern static void Inference(ref double c1, ref double c2, ref double c3, ref double result);
… which were both possible ways documented on MSDN but with no luck. Does anyone have any clue about what the problem is ?
Thanks !
You should declare your C++ function as
__stdcall, which is the P/Invoke default:It’s also possible to leave the C++ prototype alone and change the P/Invoke declaration:
cdeclisn’t used often with P/Invoke, probably because the Windows API isstdcall.