Why do I get “PInvokeStackImbalance was detected” for this simple example?

后端 未结 2 2042
刺人心
刺人心 2020-12-30 00:22

I\'m creating a very simple PInvoke sample:

extern \"C\" __declspec(dllexport) int Add(int a, int b)
{
    return a + b;
}

[DllImport(\"CommonNativeLib.dll\         


        
相关标签:
2条回答
  • 2020-12-30 00:45

    The Stack Imbalance reasons are either the signature is not matching else Calling Convention by default calling convention is stdcall. When your calling convention is stdcall callee cleans the stack if you want caller to clean the stack us cdecl calling convention. you can find more Here

    But if you are facing because of signature, just go through above link Solve Signature based Stack Imbalance issues using PInvoke extension

    0 讨论(0)
  • 2020-12-30 00:53

    You need to instead use either

    [DllImport("CommonNativeLib.dll", CallingConvention = CallingConvention.Cdecl)]
    

    or

    extern "C" __declspec(dllexport) int __stdcall Add(int a, int b) ...
    

    because regular C functions work differently than the Windows API functions; their "calling conventions" are different, meaning how they pass around parameters is different. (This was hinted at in the error.)

    0 讨论(0)
提交回复
热议问题