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

Sti*_*nty 3 .net c# gdi azure azure-web-sites

gdi32.dll在发布Azure Web App之后,尝试对C#中的函数进行一些基本调用,而且我遇到了很多问题.是完全支持还是我可以进行变通/配置更改?

在标准设置的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));
        }
    }
}      
Run Code Online (Sandbox Code Playgroud)

Chr*_*lum 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生成库能够工作.有关详细信息,请参阅上面的链接.