2013-10-23 58 views
0

我試圖使用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); 
} 

謝謝你的幫助!

+1

GetCurrentProcess返回一個PID,而不是一個HWND 。並且在while循環中調用GetCurrentProcess沒有意義,API將始終返回相同的值:** current **進程的PID。解釋你在做什麼。 – manuell

+0

我正在嘗試顯示正在運行的進程的路徑列表。 –

+0

你可以建議如何實現它嗎? –

回答

1

A)使用Win32 API EnumProcesses或Win32 API的CreateToolhelp32Snapshot/Process32First/Process32Next/CloseHandle

B)與每個PID得到(進程標識符的列表PID),使用Win32 API OpenProcess獲取進程的HANDLE(請求PROCESS_QUERY_INFORMATION爲dwDesiredAccess)。與手柄,使用Win32 API GetProcessImageFileName(不要忘記關閉與CloseHandle的手柄)

希望這有助於(因爲這不是JAVA代碼,遺憾的是)

+0

謝謝!我會在決定問題後更新帖子。 –