在Azure Web App中进行gdi32.dll函数调用 - 是否支持?

时间:2016-01-29 16:06:39

标签: c# .net azure azure-web-sites gdi

尝试在发布Azure Web App后从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));
        }
    }
}      

2 个答案:

答案 0 :(得分:6)

Azure App Service沙箱明确阻止了大多数GDI调用,因此您可能会看到错误的行为。不幸的是,没有解决方法。

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

  

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

为了使流行的PDF生成库能够工作,我们做了一些例外。有关详细信息,请参阅上面的链接。

答案 1 :(得分:0)

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