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

cFi*_*ish 3 .net c# cookies internet-explorer internet-explorer-8

根据" http://msdn.microsoft.com/en-us/library/cc196998%28v=VS.85%29.aspx "中的功能描述,我编写了以下代码以尝试获取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);
Run Code Online (Sandbox Code Playgroud)

测试这个功能:

var cookies = GetProtectedModeCookie("http://bbs.pcbeta.com/", null, 0);
Run Code Online (Sandbox Code Playgroud)

但是api IEGetProtectedModeCookie总是返回0x80070057,这表示一个或多个参数无效.我很困惑,经过大量的尝试终于失败了,只得到了这个结果.有谁能够帮我?

Eri*_*Law 5

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

另请注意,IEGetProtectedModeCookie API无法在高完整性(例如Admin)进程中运行; 它将返回ERROR_INVALID_ACCESS(0x8000000C).

这是我使用的代码:

[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");
    }
}
Run Code Online (Sandbox Code Playgroud)