c# - 在 Azure Web App 中调用 gdi32.dll 函数 - 支持吗?

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

在发布 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));
        }
    }
}      

最佳答案

大多数 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 生成库能够正常工作,我们做了一些异常(exception)处理。请参阅上面的链接了解更多详细信息。

关于c# - 在 Azure Web App 中调用 gdi32.dll 函数 - 支持吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35088902/

相关文章:

c# - 将 PDF 流式传输到网页失败

c# - 使用 SSH.NET 库从 SFTP 下载文件

c# - ADO.Net 中的异步编程

azure - 如何为 AKS 和 Helm 设置 ASP.NET CORE Web 和 WebAPI 容器之间的通信

azure - 是否可以将 Azure 虚拟机 move 到另一个区域?

c# - 这对静态类有用吗?

c# - 测试一个方法是否被覆盖?

c# - 如何将流中的图像解码为现有图像

c# - Linq 查询首选项

azure - 具有不同参数的多个相同的 azure WebJobs