I wrote a C++ DLL and now I need to call a native function from a managed app.
The exported native function appears like this:
extern "C" __declspec(dllexport)
bool NativeMethod(char *param1, char *param2, char *result);
So, from C# I’ll call that function passing 2 input params, 1 output param and obviously I’ll read the return bool value.
I tried to wrap all this in many ways, but always I get a PInvokeStackImbalance exception.
The only way I know to call native function is by applying CallingConvention = CallingConvention.Cdecl) on .NET function declaration. However in this way I’m not able to read the output param (it’s empty string always) and also the return value is always true.
First, I’d adjust the prototype of your native function.
Since this function has a C interface, you should use a C type for booleans, not a C++ type like
bool. You may want to use Win32’sBOOLtype.Moreover, as it currently is, your function is prone to buffer overruns: it’s better to add another parameter to specify the maximum size of the destination
resultstring buffer.Note also that a widespread calling convention for DLLs exporting pure C interface functions (like lots of Win32 API functions) is
__stdcall(not__cdecl). I’d use that as well.Last, since the first two parameters are input strings, you may want to use
constto make it clear and enforce const-correctness.So, I’d make the prototype of the exported native function like this:
Then, on the C# side, you can use the following P/Invoke:
Note that for the output string, a
StringBuilderis used.Note also that
CharSet = CharSet.Ansiis used to marshal C#’s Unicode UTF-16 strings to ANSI (pay attention to the fact that the conversion is lossy – if you want a non-lossy conversion, just usewchar_t*strings on the C++ side as well).I did a test with a simple C++ native DLL:
And it is called successfully by the following C# console app code: