Reputation: 7593
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.
Upvotes: 0
Views: 1425
Reputation: 613592
CreateSolidBrush
uses stdcall
. Almost all Win32 APIs do so. They never use thiscall
. The function is declared as:
HBRUSH CreateSolidBrush(
__in COLORREF crColor
);
so your mistake is simply that your version has too many parameters. The pinvoke.net declaration that you have found is correct.
The stdcall
convention pushes params right to left which explains how your code worked even with an extra spurious parameter.
Upvotes: 3