我試圖使用GetCurrentProcess函數來顯示路徑每個進程當前進程中取得的hWnd。但我在這一行中得到一個錯誤:User32.INSTANCE.GetWindowThreadProcessId(hWnd, pid);
如何將其轉換爲所需的類型?獲取的hWnd從當前進程
在類型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);
}
謝謝你的幫助!
GetCurrentProcess返回一個PID,而不是一個HWND 。並且在while循環中調用GetCurrentProcess沒有意義,API將始終返回相同的值:** current **進程的PID。解釋你在做什麼。 – manuell
我正在嘗試顯示正在運行的進程的路徑列表。 –
你可以建議如何實現它嗎? –