C# 在Azure Web App中进行gdi32.dll函数调用-支持吗?
发布Azure Web应用程序后,我试图从C#对C# 在Azure Web App中进行gdi32.dll函数调用-支持吗?,c#,.net,azure,azure-web-app-service,gdi,C#,.net,Azure,Azure Web App Service,Gdi,发布Azure Web应用程序后,我试图从C#对gdi32.dll函数进行一些基本调用,但遇到了很多问题。它是否完全受支持,或者我是否可以进行变通/配置更改 在Visual Studio中以标准设置运行时,下面的指针都返回非零值,但在Azure中运行时返回0 创建了一个基本ASP.NET Web表单项目,并在Default.aspx的codebehind中添加了blow以测试: [DllImport("gdi32.dll")] private static extern IntPtr Creat
gdi32.dll
函数进行一些基本调用,但遇到了很多问题。它是否完全受支持,或者我是否可以进行变通/配置更改
在Visual Studio中以标准设置运行时,下面的指针都返回非零值,但在Azure中运行时返回0
创建了一个基本ASP.NET Web表单项目,并在Default.aspx的codebehind中添加了blow以测试:
[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));
}
}
}
[DllImport(“gdi32.dll”)]
私有静态外部IntPtr CreatePen(int-enPenStyle、int-nWidth、uint-crColor);
[DllImport(“gdi32.dll”)]
私有静态外部IntPtr SelectObject(IntPtr hdc、IntPtr hgdiobj);
[DllImport(“gdi32.dll”)]
私有静态外部bool MoveToEx(IntPtr hdc、intx、inty、IntPtr lpPoint);
[DllImport(“gdi32.dll”)]
专用静态外部布尔线到(IntPtr hdc、int nXEnd、int nYEnd);
[DllImport(“gdi32.dll”)]
私有静态外部bool DeleteObject([In]IntPtr hObject);
受保护的无效页面加载(对象发送方、事件参数e)
{
使用(位图位图=新位图(100100))
{
使用(Graphics=Graphics.FromImage(位图))
{
IntPtr hdc=graphics.GetHdc();
IntPtr pen=CreatePen(0,(int)2,(uint)0);
IntPtr hObject=SelectObject(hdc,笔);
DeleteObject(hObject);
删除对象(笔);
graphics.ReleaseHdc();
Write(string.Format(“HDC句柄:{0}”,HDC));
响应。写入(“
”);
Write(string.Format(“CreatePen:{0}”,hObject));
响应。写入(“
”);
Write(string.Format(“SelectObject返回:{0}”,hObject));
}
}
}
大多数GDI调用都被Azure应用程序服务沙箱明确阻止,因此您看到的错误行为是意料之中的。不幸的是,没有解决办法
您可以在此处找到有关沙箱和此限制背后的原因的更多信息:
为了彻底减少攻击表面积,沙箱阻止了几乎所有的Win32k.sys API被调用,这实际上意味着大部分User32/GDI32系统调用被阻止。对于大多数应用程序来说,这不是问题,因为大多数Azure Web应用程序不需要访问Windows UI功能(它们毕竟是Web应用程序)
一些例外情况是为了使流行的PDF生成库能够工作。有关更多详细信息,请参阅上面的链接 大多数GDI呼叫现在都可以在azure应用程序服务的windows容器中使用。但是,您需要将应用程序部署为容器化应用程序