错误提示:
检测到 PInvokeStackImbalance
Message:对 PInvoke 函数“Smai_WindowsFormsApplication!Smai_WindowsFormsApplication.Form2::InternetGetCookieEx”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。
#region 获取cookie 包括带httpOnly的cookies
[DllImport("wininet.dll", CharSet = CharSet.Ansi, EntryPoint = "InternetGetCookieEx", CallingConvention = CallingConvention.Cdecl)]
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref int pcchCookieData, int dwFlags, object lpReserved);
private static string GetCookieString(string url)
{
// Determine the size of the cookie
int datasize = 256;
StringBuilder cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
{
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
return null;
}
return cookieData.ToString();
}
#endregion
在网上搜索答案,说是只要加上CallingConvention = CallingConvention.Cdecl这个就可以了,然而却还是出现问题,是因为我的.net framework版本是4.0的,是在vs2010上建立的项目。转到2.0就木问题啦。