如何使用Java从Windows上的前台窗口获取进程名称?

时间:2016-08-22 15:19:56

标签: java windows process window

我正试图从前台窗口获取进程的名称,但是我遇到了一些问题。

例如,如果我打开Mozilla firefox,它会注册类似“www.google.com - Mozilla Firefox”的内容。在photoshop中,它会注册“filename.psd”+您正在处理的缩放或图层名称。

我想从前台窗口获取进程名称,我不希望它检测到您正在处理的每个层等,我只是想让它获取进程名称(photoshop.exe或类似的东西) )

现在,我正在使用它从该窗口获取前台窗口名称和进程ID:

public String getActiveProcess(){                   
    char[] buffer;
    HWND hwnd;
    String processName = ".... // ";
    IntByReference IdByRef = new IntByReference();

    //get window information
    buffer = new char[MAX_TITLE_LENGTH * 2];
    hwnd = User32.INSTANCE.GetForegroundWindow();

    //get process ID from window in foreground
    int processid = User32.INSTANCE.GetWindowThreadProcessId(hwnd, IdByRef);

    HANDLE processHandle = Kernel32.INSTANCE.OpenProcess(processid, true, MAX_TITLE_LENGTH);

    return processName;     
}

1 个答案:

答案 0 :(得分:0)

假设您可以调用任意Win32函数,您可以使用GetWindowThreadProcessId()获取进程ID,使用OpenProcess()打开句柄,并使用GetModuleBaseName()获取进程的可执行文件名。