在 Azure Web 应用程序中进行 gdi32.dll 函数调用 - 支持吗?

Making gdi32.dll function calls in Azure Web App - supported?

在发布 Azure Web 应用程序后尝试从 C# 对 gdi32.dll 函数进行一些基本调用,但我遇到了很多问题。它是否完全受支持,或者是否有我可以做出的解决方法/配置更改?

在标准设置中 Visual Studio 中 运行 时,所有 return 非零值下方的指针,但在 运行ning 时它们 return 0在 Azure 中。

创建了一个基本的 ASP.NET Web Forms 项目并将打击添加到 Default.aspx 的代码隐藏以进行测试:

[DllImport("gdi32.dll")]
private static extern IntPtr CreatePen(int enPenStyle, int nWidth, uint crColor);

[DllImport("gdi32.dll")]
private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

[DllImport("gdi32.dll")]
private static extern bool MoveToEx(IntPtr hdc, int X, int Y, IntPtr lpPoint);

[DllImport("gdi32.dll")]
private static extern bool LineTo(IntPtr hdc, int nXEnd, int nYEnd);

[DllImport("gdi32.dll")]
private static extern bool DeleteObject([In] IntPtr hObject);


protected void Page_Load(object sender, EventArgs e)
{
    using (Bitmap bitmap = new Bitmap(100, 100))
    {
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            IntPtr hdc = graphics.GetHdc();
            IntPtr pen = CreatePen(0, (int)2, (uint)0);
            IntPtr hObject = SelectObject(hdc, pen);

            DeleteObject(hObject);
            DeleteObject(pen);
            graphics.ReleaseHdc();

            Response.Write(string.Format("HDC handle: {0}", hdc));
            Response.Write("<br/>");
            Response.Write(string.Format("CreatePen pen: {0}", hObject));
            Response.Write("<br/>");
            Response.Write(string.Format("SelectObject returned: {0}", hObject));
        }
    }
}      

大多数 GDI 调用都被 Azure 应用服务沙箱明确阻止,因此您看到的错误行为是预料之中的。不幸的是,没有解决方法。

您可以在此处找到有关沙盒的更多信息以及此限制背后的原因:https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox

For the sake of radical attack surface area reduction, the sandbox prevents almost all of the Win32k.sys APIs from being called, which practically means that most of User32/GDI32 system calls are blocked. For most applications this is not an issue since most Azure Web Apps do not require access to Windows UI functionality (they are web applications after all).

一些例外情况是为了使流行的 PDF 生成库能够正常工作。有关详细信息,请参阅上面的 link。

大部分 GDI 调用现在都可以在 Azure 应用服务的 windows 容器中使用。 https://azure.microsoft.com/en-us/updates/app-service-announces-general-availability-of-windows-container-support/。但是,您需要将应用程序部署为容器化应用程序。