I\'m creating a very simple PInvoke sample:
extern \"C\" __declspec(dllexport) int Add(int a, int b)
{
return a + b;
}
[DllImport(\"CommonNativeLib.dll\
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
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.)