我知道这已被问过很多次,但不幸的是我没有找到解决问题的方法,我正在使用urlmon.dll
从字节数组中查找 MIME 类型,但我收到 IIS 进程崩溃w3wp.exe
尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
编码:
public static int MimeSampleSize = 256;
public static string DefaultMimeType = "application/octet-stream";
[DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
private extern static uint FindMimeFromData(
uint pBC,
[MarshalAs(UnmanagedType.LPStr)] string pwzUrl,
[MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
uint cbSize,
[MarshalAs(UnmanagedType.LPStr)] string pwzMimeProposed,
uint dwMimeFlags,
out uint ppwzMimeOut,
uint dwReserverd
);
public static string GetMimeFromBytes(byte[] data)
{
try
{
uint mimeType;
FindMimeFromData(0, null, data, (uint)MimeSampleSize, null, 0, out mimeType, 0);
var mimePointer = new IntPtr(mimeType);
var mime = Marshal.PtrToStringUni(mimePointer); // <-- Crash happens here
Marshal.FreeCoTaskMem(mimePointer);
return mime ?? DefaultMimeType;
}
catch
{
return DefaultMimeType;
}
}
崩溃发生在以下行:
var mime = Marshal.PtrToStringUni(mimePointer);
我试图取消选中“ Suppress JIT optimization on module load
”选项,但没有运气。还尝试将构建更改为 x86 而不是 Any CPU,但没有任何运气。
注意:我使用的是 4.5 框架,IIS 8