简体   繁体   English

带有正则表达式的JNA .FindWindow

[英]JNA .FindWindow with regex

Hello I'm working on a program which uses JNA 4.5.1. 您好我正在开发一个使用JNA 4.5.1的程序。 I need to know whether a specific program is running or not. 我需要知道特定程序是否正在运行。 Here is my problem: 这是我的问题:

hwnd = User32.INSTANCE.FindWindow
       (null, "Session Dev/Prod - [32 x 80]");
if (hwnd == null) {
    System.out.println("Session Dev/Prod is not running");
    Process p = null;
    try {
        p = Runtime.getRuntime()
                .exec("rundll32 
                 url.dll,FileProtocolHandler C: 
                 /ProgramData/Microsoft/Windows/Start 
                 Menu/Programs/IBM Personal 
                 Communications/TNHost");
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        p.waitFor();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
else{

    System.out.println("Host Already open");
    User32.INSTANCE.ShowWindow(hwnd, User32.SW_MAXIMIZE );        
    User32.INSTANCE.SetForegroundWindow(hwnd);
}

The Problem is that the Window-Title changes depending on the monitor size. 问题是窗口标题会根据监视器大小而变化。

hwnd = User32.INSTANCE.FindWindow(null, "Session Dev/Prod - [32 x 80]");

The title is always "Session Dev/Prod" + the size which changes. 标题始终为“会话开发/产品” +更改的大小。

I need to find the window which starts with "Session Dev/Prod". 我需要找到以“ Session Dev / Prod”开头的窗口。 Does anyone know how to do this. 有谁知道如何做到这一点。 Or is there an other way to find out whether a program is running or not? 还是有其他方法可以确定程序是否正在运行? I've tried to do it with Regex as parameter but the function accepts just a String. 我尝试使用Regex作为参数来执行此操作,但该函数仅接受字符串。

Thank you 谢谢

I had the task once to check whether a program was running or not (and if so kill it) and solved it like this: 我曾经执行一次任务来检查程序是否正在运行(如果杀死,则将其杀死)并按以下方式解决它:

public static void isProcessRunning(String processName) throws IOException, InterruptedException {
 ProcessBuilder processBuilder = new ProcessBuilder("tasklist.exe");
 Process process = processBuilder.start();
 // handle errors: process.getErrorStream();

 BufferedReader reader = new BufferedReader(new  InputStreamReader(process.getInputStream()));
 String line;
 while ((line = reader.readLine()) != null) {
  if (line.startsWith(processName)) {
    System.out.println("Yes program is running");
    break;
  }
  // else {
  // System.out.println("No program is not running");
  // }
}

} }

To find out the name of your task call tasklist in the commandline and look for the name. 要在命令行中找到任务的名称,请调用任务tasklist并查找名称。 If it is really 'Session Dev/Prod - [32 x 80]' then you can use 'Session Dev/Prod' as a string... But note that this is a windows solution. 如果确实是'Session Dev / Prod-[32 x 80]',则可以将'​​Session Dev / Prod'用作字符串。但是请注意,这是Windows解决方案。 For linux you have to use something like ps -ef 对于Linux,您必须使用ps -ef东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM