Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java从Windows上的前台窗口获取进程名?_Java_Windows_Process_Window - Fatal编程技术网

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

如何使用Java从Windows上的前台窗口获取进程名?,java,windows,process,window,Java,Windows,Process,Window,我正在尝试从前台窗口获取进程的名称,但我遇到了一些问题 例如,如果我打开Mozilla firefox,它会注册类似“www.google.com-Mozilla firefox”的内容。在photoshop中,它会注册“filename.psd”+正在处理的缩放或图层名称 我想从前台窗口获取进程名,我不想让它检测到你正在处理的每一层,等等,我只想让它获取进程名(photoshop.exe或类似的东西) 现在,我使用它从该窗口获取前景窗口名和进程ID: public String getActi

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

例如,如果我打开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;     
}

假设可以调用任意Win32函数,则可以使用获取进程ID,使用打开句柄,并使用获取进程的可执行名称。

假设可以调用任意Win32函数,则可以使用获取进程ID,使用打开句柄,并使用获取进程的可执行名称。

进程没有名称。为什么不问问你想要解决的真正问题,而不是你的解决方案呢?流程没有名字。为什么不问一下您试图解决的真正问题,而不是您的解决方案呢。其余的我都做了,但我被绊住了。您能写一些简单的使用示例吗?请改为尝试GetModuleBaseName()。如果您仍然无法使其工作,请在问题的末尾添加新代码,我将看看是否能找出问题所在。代码已更新。我不知道如何继续。我不知道如何在带有“GetProcessImageFileName()”的Java中使用GetProcessImageFileName()或GetModuleBasename()。其余的我都做了,但我被绊住了。您能写一些简单的使用示例吗?请改为尝试GetModuleBaseName()。如果您仍然无法使其工作,请在问题的末尾添加新代码,我将看看是否能找出问题所在。代码已更新。我不知道如何继续。我不知道如何在java中使用GetProcessImageFileName()或GetModuleBasename()