urlmon.dll FindMimeFromData() works perfectly on 64bit desktop/console but generates errors on ASP.NET

后端 未结 1 1799
走了就别回头了
走了就别回头了 2020-12-16 02:09

I am creating a library of utilities to be used both in desktop environment in a web environment.

It contains several features that I believe are often repeated in m

相关标签:
1条回答
  • 2020-12-16 02:37

    If you used the pinvoke signature from the answer your linked, it's defined there like this:

    [DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
    private extern static System.UInt32 FindMimeFromData(
        System.UInt32 pBC,
        [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
        [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
        System.UInt32 cbSize,
        [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
        System.UInt32 dwMimeFlags,
        out System.UInt32 ppwzMimeOut,
        System.UInt32 dwReserverd
    );
    

    I would rather use the defintion from pinvoke.net:

    [DllImport("urlmon.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false)]
    static extern int FindMimeFromData(IntPtr pBC,
        [MarshalAs(UnmanagedType.LPWStr)] string pwzUrl,
        [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1, SizeParamIndex=3)] 
        byte[] pBuffer,
        int cbSize,
        [MarshalAs(UnmanagedType.LPWStr)] string pwzMimeProposed,
        int dwMimeFlags,
        out IntPtr ppwzMimeOut,
        int dwReserved);
    

    Note the difference in types for ppwzMimeOut and pBC parameter. In the former case, System.UInt32 is not a correct type for a 64-bit pointer under a 64-bit platform. For pBC, this is probably not an issue (as long as it is NULL), but it matters for ppwzMimeOut.

    Refer to this implementation which appears to be correct.

    0 讨论(0)
提交回复
热议问题