AccessViolationException from WebBrowser control with custom IDownloadManager

My .NET application hosts a WebBrowser control, and I added a custom download manager. Everything is working fine, except for requests with the following two HTTP headers:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename=blahblahblah

Change or omit the content type header and everything works fine; it's just an application / octet stream that has a problem. Remove the content placement header and the download manager will not be used.

Interestingly, this problem only occurs on 32-bit machines (I tested XP and Win 7 32 bits. Win 7/8 64 bits did not crash).

What is so special about application/octet-stream?

In case this helps, here is a ton of code that the user download manager registers. This was inspired by this question and answer: Windows Forms Webbrowswer control with IDownloadManager

In my class, which comes from WebBrowser:

protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
{
    var site = new DownloadWebBrowserSite(this);
    return site;
}

In DownloadWebBrowserSite:

    DownloadManager _manager = new DownloadManager();

    public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
    {
        if (guidService.CompareTo(ComInteropConstants.IID_IDownloadManager) == 0 && riid.CompareTo(ComInteropConstants.IID_IDownloadManager) == 0)
        {
            IntPtr punk = Marshal.GetIUnknownForObject(_manager);
            return Marshal.QueryInterface(punk, ref riid, out ppvObject);
        }
        ppvObject = IntPtr.Zero;
        return ComInteropConstants.E_NOINTERFACE;
    }

DownloadManager- my user download manager. Its implementation is Downloadbelow.

public int Download(IMoniker pmk, IBindCtx pbc, uint dwBindVerb, int grfBINDF, IntPtr pBindInfo, string pszHeaders, string pszRedir, uint uiCP)
{
    return ComInteropConstants.S_OK;
}

It implements IDownloadManager, announced below.

[ComVisible(false), ComImport]
[Guid("988934A4-064B-11D3-BB80-00104B35E7F9")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDownloadManager
{
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int Download(
        [In, MarshalAs(UnmanagedType.Interface)] IMoniker pmk,
        [In, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc,
        [In, MarshalAs(UnmanagedType.U4)] uint dwBindVerb,
        [In] int grfBINDF,
        [In] IntPtr pBindInfo,
        [In, MarshalAs(UnmanagedType.LPWStr)] string pszHeaders,
        [In, MarshalAs(UnmanagedType.LPWStr)] string pszRedir,
        [In, MarshalAs(UnmanagedType.U4)] uint uiCP);
}

To reproduce the failure, I do not need to do anything inside this method.

+1
source share
1 answer

I still don’t quite understand why my application crashes, and I don’t understand why the following code fixed it, but here is what prevented the crash:

[DllImport("urlmon.dll"), PreserveSig]
[return: MarshalAs(UnmanagedType.Error)]
static extern int CoInternetSetFeatureEnabled(int FeatureEntry, [In, MarshalAs(UnmanagedType.U4)]uint dwFlags, bool fEnable);

CoInternetSetFeatureEnabled(ComInteropConstants.FEATURE_MIME_HANDLING, (uint)ComInteropConstants.SET_FEATURE_ON_PROCESS, true);
CoInternetSetFeatureEnabled(ComInteropConstants.FEATURE_MIME_SNIFFING, (uint)ComInteropConstants.SET_FEATURE_ON_PROCESS, true);

, , ( MIME MIME-) HTTP- ( /- ) .

+1

Source: https://habr.com/ru/post/1525221/


All Articles