I updated my project from VS2005 (targeting .net 2) to VS2010 (targeting .net4). It seems the pInvokeStackImbalance MDA is enabled by default, then I get a bunch of "unbalanced the stack"
exceptions. Take this one for example:
[DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateSolidBrush(BrushStyles enBrushStyle, int crColor);
It was working in .net 2 but now it throws exception. I changed it to this and it works:
[DllImportAttribute("gdi32.dll", CallingConvention = CallingConvention.ThisCall)]
private static extern IntPtr CreateSolidBrush(BrushStyles enBrushStyle, int crColor);
To my surprise, pinvoke.net lists it as
[DllImport("gdi32.dll")]
static extern IntPtr CreateSolidBrush(uint crColor);
Why my old is not working? It seems the pinvoke.net is wrong but how do I find out which calling conversion is it given a win32 function?
EDIT
My project is using the code from C# Rubber Rectangle to do the XOR drawing. Apparently the code needs a fix to work in .Net 4.