C# 为什么IEGetProtectedModeCookie()总是返回0x80070057?

C# 为什么IEGetProtectedModeCookie()总是返回0x80070057?,c#,.net,internet-explorer,cookies,internet-explorer-8,C#,.net,Internet Explorer,Cookies,Internet Explorer 8,根据“”中的函数描述,我编写了以下代码以尝试获取受IE保护的Cookie: public static string GetProtectedModeCookie(string lpszURL, string lpszCookieName, uint dwFlags) { var size = 255; var sb = new System.Text.StringBuilder(size); var acturalSize = sb.Capacity; var

根据“”中的函数描述,我编写了以下代码以尝试获取受IE保护的Cookie:

public static string GetProtectedModeCookie(string lpszURL, string lpszCookieName, uint dwFlags)
{
    var size = 255;
    var sb = new System.Text.StringBuilder(size);
    var acturalSize = sb.Capacity;
    var code = IEGetProtectedModeCookie(lpszURL, lpszCookieName, sb, ref acturalSize, dwFlags);
    if ((code & 0x80000000) > 0) return string.Empty;
    if (acturalSize > size)
    {
        sb.EnsureCapacity(acturalSize);
        IEGetProtectedModeCookie(lpszURL, lpszCookieName, sb, ref acturalSize, dwFlags);
    }
    return sb.ToString();
}

[DllImport("ieframe.dll", SetLastError = true)]
public static extern uint IEGetProtectedModeCookie(string lpszURL, string lpszCookieName, System.Text.StringBuilder pszCookieData, ref int pcchCookieData, int dwFlags);
要测试此功能,请执行以下操作:

var cookies = GetProtectedModeCookie("http://bbs.pcbeta.com/", null, 0);
但是apiIEGetProtectedModeCookie始终返回0x80070057,这表示一个或多个参数无效。
我很困惑,经过多次尝试终于失败了,只得到了这个结果。有人能帮我吗?

如果IEGetProtectedModelCookie认为URL不打算在保护模式下打开,它将返回E_INVALIDARG。它使用API确定这一点。因此,如果您将该URL放在受信任区域或诸如此类的地方,那么您将遇到此错误。如果无法传递URL或无法传递指向缓冲区大小整数的指针,则基础InternetGetCookie API将返回E_INVALIDARG

还请注意,IEGetProtectedModeCookie API在高完整性(例如管理)进程中无法工作;它将返回错误\u无效\u访问(0x800000C)

以下是我使用的代码:

[DllImport("ieframe.dll", CharSet = CharSet.Unicode, EntryPoint = "IEGetProtectedModeCookie", SetLastError = true)]
public static extern int IEGetProtectedModeCookie(String url, String cookieName, StringBuilder cookieData, ref int size, uint flag);


private void GetCookie_Click(object sender, EventArgs e)
{
    int iSize = 4096;
    StringBuilder sbValue = new StringBuilder(iSize);

    int hResult = IEAPI.IEGetProtectedModeCookie("http://www.google.com", "PREF", sbValue, ref iSize, 0);

    if (hResult == 0)
    {
        MessageBox.Show(sbValue.ToString());
    }
    else
    {
        MessageBox.Show("Failed to get cookie. HRESULT=0x" + hResult.ToString("x") + "\nLast Win32Error=" + Marshal.GetLastWin32Error().ToString(), "Failed");
    }
}

字符集参数必须存在于DllImport属性中。将API声明更改为以下将很好地工作:

[DllImport("ieframe.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint IEGetProtectedModeCookie(string lpszURL, string lpszCookieName, System.Text.StringBuilder pszCookieData, ref int pcchCookieData, uint dwFlags);

如果Charset参数丢失,此API将始终返回0x80070057,这表明一个或多个参数无效。

感谢您的帮助。我已经打开IE进程模式,并导航到目标网站。在“AppData\Roaming\Microsoft\Windows\cookies\Low”下可以找到处于保护模式的Cookie,但此api仍返回0x80070057。我还尝试使用IEisProtectedModelUrl:[DllImport(“ieframe.dll”)]公共静态外部HRESULT IEisProtectedModelUrl(字符串pszUrl);但它也会通过以下调用返回0x80070057:isProtectedModelUrl(“);我想知道可能的原因是什么?非常感谢。我已经找到了原因……我错过了DllImport属性中的字符集。在我将“Charset=Charset.Unicode”添加到DllImport属性中后,此api运行良好。感谢您的帮助,我从您的代码中得到了提示:-)