我想在Windows 10 Pro 64位中检索指定进程ID的有效DPI感知值。我需要的值是我可以使用WinAPI PROCESS_DPI_AWARENESS函数获得的GetProcessDpiAwareness之一。
为了实现我的需要,我在VS 2015中编写了一个简单的单窗口C#WPF应用程序。我将我感兴趣的进程ID输入到TextBox txtProcessID中,当我按下时,结果显示在TextBlock txtResult中txtProcessID按钮:
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。我做错了什么?
答案 0 :(得分:4)
从GetProcessDpiAwareness
的文档中,第一个参数是流程句柄,而不是流程ID。您需要OpenProcess
,然后获取所需信息CloseHandle
。 (定义p / Invoke签名时,以h
或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);
答案 1 :(得分:0)
是的,这是我的遗漏。我需要将进程句柄传递给GetProcessDpiAwareness
。我根据我的问题中的前两条评论编写了自己的工作代码:
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();
另外一条评论:最好使用管理员权限执行此代码,以避免访问其他进程时出现问题。