我尝试使用GetCurrentProcess函数从当前进程中获取 hWnd 以显示每个进程的路径。但是我在这一行遇到了一个错误:User32.INSTANCE.GetWindowThreadProcessId(hWnd, pid);
如何将其转换为所需的类型?
User32 类型中的 GetWindowThreadProcessId(WinDef.HWND, IntByReference) 方法不适用于参数 (WinNT.HANDLE, IntByReference)
有我的代码:
try {
while (kernel32.Process32Next(snapshot, processEntry)) {
kernel32.GetCurrentProcess();
HANDLE hWnd = kernel32.GetCurrentProcess();
User32.INSTANCE.GetWindowThreadProcessId(hWnd, pid);
HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010,
false, pid.getValue());
psapi.GetModuleFileNameExA(process, null, path, 1024);
System.out.println(Native.toString(path));
}
} finally {
kernel32.CloseHandle(snapshot);
}
UPD: 问题以这种方式解决:
try {
while (kernel32.Process32Next(snapshot, processEntry)) {
HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010,
false, processEntry.th32ProcessID.intValue());
if (process != null) {
int len = psapi.GetModuleFileNameExW(process, null, path,
1024);
if (len > 0) {
System.out.println(new String(path, 0, len));
} else {
System.out.println("GetModuleFileNameW failed");
}
} else {
System.out.println(kernel32.GetLastError());
}
System.out.println(process != null ? Native.toString(path) : "error");
}
} finally {
kernel32.CloseHandle(snapshot);
}
谢谢你的帮助!