2013-06-12 41 views

回答

1

考慮使用Java Native Access。從here下載jna.jar和platform.jar。

您可能已經注意到我們可以使用GetWindowThreadProcessId中的user32.dll從窗口句柄中獲取pid。然後我們可以在kernel32.dll中使用OpenProcess來獲得指向該進程的指針。然後在the bunch of APIsGetProcessMemoryInfoGetModuleBaseNamePSAPI.DLL可以幫助你從這個過程目標的過程信息和GetProcessTimesKERNEL32.DLL可以返回CPU使用率。 GetProcessMemoryInfo包含一個名爲PROCESS_MEMORY_COUNTERS的結構,它需要Structure in JNA來處理。

static class Kernel32 { 
    static { Native.register("kernel32"); } 
    public static int PROCESS_QUERY_INFORMATION = 0x0400; 
    public static int PROCESS_VM_READ = 0x0010; 
    public static native int GetLastError(); 
    public static native Pointer OpenProcess(int dwDesiredAccess, boolean bInheritHandle, Pointer pointer); 
    public static native boolean GetProcessTimes(Pointer hProcess, int lpCreationTime,int LPFILETIME lpExitTime, int lpKernelTime, int lpUserTime 
} 

static class Psapi { 
    static { Native.register("psapi"); } 
    public static native int GetModuleBaseNameW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size); 
    ... 
} 

static class User32DLL { 
    static { Native.register("user32"); } 
    public static native int GetWindowThreadProcessId(HWND hWnd, PointerByReference pref); 
    public static native int GetWindowTextW(HWND hWnd, char[] lpString, int nMaxCount); 
} 

PointerByReference pointer = new PointerByReference(); 
GetWindowThreadProcessId(yourHandle, pointer); 
Pointer process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pointer.getValue()); 
GetModuleBaseNameW(process, null, buffer, MAX_TITLE_LENGTH); 
System.out.println("Active window process: " + Native.toString(buffer)); 
+0

只是想說,經過2個小時的搜索,這是唯一的答案,實際上我的作品能夠一次性列出標題和過程!謝謝!!! – misaka