I am trying to integrate with a legacy C++ DLL from C# using PInvoke in a console app (and eventually porting it to Web API). When I make the call to the DLL, it crashes with the following error:
--------------------------------------------------
Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'C:\projects\workspace-kn\PInvokeTest\PInvokeTest\bin\Debug\PInvokeTest.vshost.exe'.
Additional information: The runtime has encountered a fatal error. The address of the error was at 0x55dde385, on thread 0x2b34. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common
sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
--------------------------------------------------
For whatever reason, if I set "Output1" to a double (instead of double[]) the call works (somewhat). A single value is returned for Output1 but Output2 is null (should have a 5-element array populated). Values are populated for the other output params.
I've compiled the calling C# app as a 32-bit application. The call to the C++ library would fail when the C# caller was compiled as a 64-bit app.
Let me know if there is any other info I could provide to assist in debugging this. Thanks in advance!
Here is the signature for the .h file for the C++ DLL (sorry, lots of params):
#ifdef _VB_DEBUG
DllExport void __stdcall calculate
#else
DllExport void calculate
#endif
(double* input1, // 63-element array,
double* input2, // 63-element array,
double* input3, // 45-element array,
double* input4, // 45-element array,
short input5,
double* input6, // 45-element array,
double input7,
double input8,
double input9,
double input10,
short input11,
char* input12,
double* input13, // 63-element array,
double input14,
double input15,
double input16,
double input17,
double input18,
double input19,
double input20,
double input21,
double input22,
double input23,
double input24,
double input25,
double input26,
double input27,
double input28,
double input29,
double input30,
double * input31, // 4-element array,
double input32,
double input33,
double input34,
double input35,
char * input36,
double * output1,// OUTPUT - 10-element array
double * output2,// Output - 5-element array
double * output3,// Output
double * inputoutput1,// Input, Output
double * output4// Output
double * inputoutput2, // Input, Output
double * output5,// Output
double * output6,// Output
double * output7,// Output
double * output8,// Output
double * output9,// Output
double * output10,// Output
double * output11,// Output
double * output12,// Output
double * output13,// Output
double * output14,// Output
double * output15,// Output
short * output16,// Output
char * output17);// Output - limited
Here is the method stub in my C# class:
[DllImport("MyLibrary.dll", EntryPoint = "calculate", CallingConvention = CallingConvention.Cdecl, PreserveSig = true, CharSet=CharSet.Ansi)]private static extern void calculate([In]ref double[] input1, // 63-element array,
[In]ref double[] input2, // 63-element array,
[In]ref double[] input3, // 45-element array,
[In]ref double[] input4, // 45-element array,
[In]short input5,
[In]ref double[] input6, // 45-element array,
[In]double input7,
[In]double input8,
[In]double input9,
[In]double input10,
[In]short input11,
[In]ref char[] input12, // 6-character null-terminated string
[In]ref double[] input13, // 63-element array,
[In]double input14,
[In]double input15,
[In]double input16,
[In]double input17,
[In]double input18,
[In] double input19,
[In]double input20,
[In]double input21,
[In]double input22,
[In]double input23,
[In]double input24,
[In]double input25,
[In]double input26,
[In]double input27,
[In]double input28,
[In]double input29,
[In]double input30,
[In]ref double[] input31, // 4-element array.
[In]double input32,
[In]double input33,
[In]double input34,
[In]double input35,
[In]ref char input36,
[Out]out double[] output1,// Output - 10-element array
[Out]out double[] output2,// Output - 5-element array
[Out]out double output3,// Output,
[In, Out]ref double inputOutput1,// Input, Output,
[Out]out double output4,// Output,
[In, Out]ref double inputOutput2,// Input, Output,
[Out]out double output5,// Output,
[Out]out double output6,// Output,
[Out]out double output7,// Output,
[Out]out double output8,// Output,
[Out]out double output9,// Output,
[Out]out double output10,// Output,
[Out]out double output11,// Output,
[Out]out double output12,// Output,
[Out]out double output13,// Output,
[Out]out double output14,// Output,
[Out]out double output15,// Output,
[Out]out short output16,// Output
[Out]out char[] output17,// Output