A call to PInvoke function ‘[…]’ has unbalanced the stack

I had a _cdecl c++ dll that I called without any trouble from Visual Studio 2008, and then the identical code in Visual Studio 2010 would not work. I got the same PInvoke … has unbalanced the stack error as well.

The solution for me was to specify the calling convention in the DllImport(…) attribute:
From:

[DllImport(CudaLibDir)] 

To:

[DllImport(CudaLibDir, CallingConvention = CallingConvention.Cdecl)]

I guess they changed the default calling convention for DLLImport between .NET 3.5 and .NET 4.0?

Leave a Comment