C# 在Windows Server 2016中读取MIME类型时发生AccessViolationException异常
我的一些产品代码在从Windows 2008R2服务器移动到Windows 2016服务器时被破坏。代码检查文件扩展名与文件中嵌入的MIME类型不匹配的文件 为了获得嵌入的MIME类型,我们使用。(缓冲区是从正在扫描的文件中读取的数据。) 我已经验证了mimeType被设置为非零数。 周末有两件事改变了密码被破解了:C# 在Windows Server 2016中读取MIME类型时发生AccessViolationException异常,c#,mime-types,dllimport,access-violation,windows-server-2016,C#,Mime Types,Dllimport,Access Violation,Windows Server 2016,我的一些产品代码在从Windows 2008R2服务器移动到Windows 2016服务器时被破坏。代码检查文件扩展名与文件中嵌入的MIME类型不匹配的文件 为了获得嵌入的MIME类型,我们使用。(缓冲区是从正在扫描的文件中读取的数据。) 我已经验证了mimeType被设置为非零数。 周末有两件事改变了密码被破解了: 应用程序已从Windows Server 2008R2计算机移动到Windows Server 2016 我们从中下载文件的(外部控制的)web服务已更新 Windows 2016
public static string ScanBufferForMimeType(byte[] buffer)
{
uint mimeType = default(uint);
FindMimeFromData(0, null, buffer, 256, null, 0, ref mimeType, 0);
IntPtr mimeTypePtr = new IntPtr(mimeType);
string mime = Marshal.PtrToStringUni(mimeTypePtr); //AccessViolationException is thrown here.
Marshal.FreeCoTaskMem(mimeTypePtr);
if (string.IsNullOrEmpty(mime))
mime = "application/octet-stream";
return mime;
}