2014-01-14 72 views
1

如何通過Java中的某個ProcessId獲取進程對象。通過進程Id獲取進程對象

我使用JNA來創建一個進程,因爲我想在後臺打開一個應用程序。

WinBase.STARTUPINFO startupInfo = new WinBase.STARTUPINFO(); 
startupInfo.dwFlags = 1; 

WinBase.PROCESS_INFORMATION.ByReference processInformation = new WinBase.PROCESS_INFORMATION.ByReference(); 

Kernel32.INSTANCE.CreateProcess(null, "C:\\Program Files\\...", null, null, true, new WinDef.DWORD(0), Pointer.NULL, System.getProperty("java.io.tmpdir"), startupInfo, processInformation); 

int prozessId = processInformation.dwProcessId.intValue(); 
+0

你爲什麼不使用Process?你如何*不*在Java的背景下打開一個進程? –

+0

我試過這個,但是我的應用程序總是在前臺開始。我爲此使用ProcessBuilder – Higune

+0

我想這個問題是關於Windows API編程而不是Java! –

回答

1

這是不可能的。 ProcessProcessBuilder的API不允許爲現有進程創建Process實例;您始終只能使用它們創建新的子進程。