Search code examples
c#pinvokenative-codecalling-convention

StackImbalance MDA exception using stdcall calling conversion on both sides


I got pInvokeStackImbalance MDA for some unclear reason

Native code:

extern "C" __declspec(dllexport) __declspec(noinline)  void __stdcall 
Ex(__int64 mask, unsigned long *index)
{
    *index = mask;
}

Managed:

[DllImport("Libr.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int Ex(long mask, out uint index);

...

        uint val;
        long mask = 12;
        NativeWrapper.Ex(mask, out val); // pInvokeStackImbalance MDA here

So as you can see CallingConvention StdCall is using.. what is the reason of that warning here?


Solution

  • The native function return type is void but your C# code returns int.