0

我想在 Windows 10 Pro 64 位中检索指定进程 ID 的有效 DPI 感知值。我需要的值是我可以使用 WinAPI GetProcessDpiAwareness函数获得的PROCESS_DPI_AWARENESS之一。

为了实现我所需要的,我在 VS 2015 中编写了一个简单的单窗口 C# WPF 应用程序。我在 TextBox txtProcessID 中输入我感兴趣的进程 ID,当我按下 txtProcessID 按钮时,结果会显示在 TextBlock txtResult 中:

private const int S_OK = 0;
private enum PROCESS_DPI_AWARENESS
{
    PROCESS_DPI_UNAWARE = 0,
    PROCESS_SYSTEM_DPI_AWARE = 1,
    PROCESS_PER_MONITOR_DPI_AWARE = 2
}
[DllImport("Shcore.dll")]
private static extern int GetProcessDpiAwareness(IntPtr hprocess, out PROCESS_DPI_AWARENESS value);

private void btnGetDPIAwareness_Click(object sender, RoutedEventArgs e)
{
    int procIDint = int.Parse(txtProcessID.Text);
    IntPtr procID = new IntPtr(procIDint);
    PROCESS_DPI_AWARENESS value;
    int res = GetProcessDpiAwareness(procID, out value);
    if (res == S_OK)
        txtResult.Text = value.ToString();
    else
        txtResult.Text = "Error: " + res.ToString("X");
}

但是为任何进程调用 GetProcessDpiAwareness 总是会给我一个错误 E_INVALIDARG。我究竟做错了什么?

4

2 回答 2

5

从文档中GetProcessDpiAwareness第一个参数是进程句柄,而不是进程 ID。你需要OpenProcess,然后得到你想要的信息CloseHandleh(在定义 ap/Invoke 签名时,任何以或lp通常是IntPtr托管代码中的开头的变量名称。)

如:

private const int PROCESS_QUERY_INFORMATION = 0x0400;
private const int PROCESS_VM_READ = 0x0010;

[DllImport("Kernel32.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId);

[DllImport("Kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr handle);

private const int S_OK = 0;
private enum PROCESS_DPI_AWARENESS
{
    PROCESS_DPI_UNAWARE = 0,
    PROCESS_SYSTEM_DPI_AWARE = 1,
    PROCESS_PER_MONITOR_DPI_AWARE = 2
}
[DllImport("Shcore.dll")]
private static extern int GetProcessDpiAwareness(IntPtr hprocess, out PROCESS_DPI_AWARENESS value);

private PROCESS_DPI_AWARENESS GetDpiState(uint processId)
{
    IntPtr handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processId);
    if (handle != IntPtr.Zero)
    {
        PROCESS_DPI_AWARENESS value;
        int result = GetProcessDpiAwareness(handle, out value);
        if (result == S_OK)
        {
            System.Diagnostics.Debug.Print(value.ToString());
        }
        CloseHandle(handle);
        if (result != S_OK)
        {
            throw new Win32Exception(result);
        }
        return value;
    }
    throw new Win32Exception(Marshal.GetLastWin32Error());
}

虽然更简单的方法是使用System.Diagnostics.Process如下:

System.Diagnostics.Process proc = Process.GetProcessById(processId);
PROCESS_DPI_AWARENESS value;
int res = GetProcessDpiAwareness(proc.Handle, out value);
于 2016-06-24T11:44:35.970 回答
0

是的,这是我的疏忽。我需要将进程句柄传递给GetProcessDpiAwareness. 我刚刚根据我的问题下的前 2 条评论编写了自己的工作代码:

int procID = int.Parse(txtProcessID.Text);
Process process = Process.GetProcessById(procID, ".");
PROCESS_DPI_AWARENESS value;
int res = GetProcessDpiAwareness(process.Handle, out value);
if (res == S_OK)
    txtResult.Text = value.ToString();
else
    txtResult.Text = "Error: " + res.ToString("X");
process.Close();

附加评论:最好以管理员权限执行此代码,以避免访问其他进程时出现问题。

于 2016-06-24T11:51:25.857 回答