【问题标题】:Making gdi32.dll function calls in Azure Web App - supported?在 Azure Web App 中进行 gdi32.dll 函数调用 - 支持吗?
【发布时间】:2016-05-07 10:43:28
【问题描述】:

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

在标准设置下在 Visual Studio 中运行时,下面的指针都返回非零值,但在 Azure 中运行时它们返回 0。

创建了一个基本的 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));
        }
    }
}      

【问题讨论】:

    标签: c# .net azure azure-web-app-service gdi


    【解决方案1】:

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

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

    为了彻底减少攻击面,沙盒阻止了几乎所有的 Win32k.sys API 被调用,这实际上意味着大部分 User32/GDI32 系统调用都被阻止了。对于大多数应用程序而言,这不是问题,因为大多数 Azure Web 应用程序不需要访问 Windows UI 功能(它们毕竟是 Web 应用程序)。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-05
        • 2018-10-22
        • 1970-01-01
        • 2021-02-09
        • 2010-09-14
        • 2018-03-28
        • 1970-01-01
        相关资源
        最近更新 更多