Azure WebJob 运行时无法使用 MapVirtualKey 映射虚拟键 (user32.dll)

Azure WebJob runtime unable to map virtual key using MapVirtualKey (user32.dll)

.Net 控制台应用程序工作正常,直到它作为 Azure WebJob 上传到 Azure 网站。要复制的代码:

using System;
using System.Runtime.InteropServices;
using System.Windows.Input;
namespace ConsoleApplication2
{

class Program
{
    [DllImport("user32.dll")]
    private static extern uint MapVirtualKey(uint uCode, uint uMapType);

    static void Main()
    {
        foreach (uint c in  new uint[] {18, 31})
        {
            var mapped = (int) MapVirtualKey(c, 1);
            Console.WriteLine($"{mapped} : {KeyInterop.KeyFromVirtualKey(mapped)}");
        }
    }
}

}

预期输出:

69 : E

83 : S

实际输出:

-1073741790 : None

-1073741790 : None

不确定从这里去哪里。这是否意味着此 api 调用的访问被拒绝?

这不起作用的原因是 MapVirtualKey API 被 Azure Web App 沙箱阻止。您可以在 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).