JNA - 如何通过.exe获取processID

时间:2015-11-26 07:46:34

标签: java c++ jna

所以我正在使用" findWindow" atm来获取processID,但是让我们说,而不是使用查找窗口来获取"使命召唤黑色行动",我想直接获取processID与进程名称本身,即" BlackOps.exe&#34 ;.我该怎么做?

2 个答案:

答案 0 :(得分:0)

这是我用java"

查找proccess id的代码
    public static void main(String[] args) {

    String taskListCommand = System.getenv("windir") + "\\system32\\" + "tasklist.exe";
    try {
        final Process p = Runtime.getRuntime().exec(taskListCommand);
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        try {
            while ((line = input.readLine()) != null) {
                if (line.contains("BlackOps.exe")) {
                    System.out.println(line);
                    PID = (line.split("\\s+"))[1];
                    System.out.println("PID = " + PID);
                    break;
                }
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        } catch (IOException e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

#using<System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
int main()
{   
  // Get the current process.    
  Process^ currentProcess = Process::GetCurrentProcess();

  // Get all processes running on the local computer.
  array<Process^>^localAll = Process::GetProcesses();

  // Get all instances of Notepad running on the local computer.
  // This will return an empty array if notepad isn't running.
  array<Process^>^localByName = Process::GetProcessesByName("notepad");

  // Get a process on the local computer, using the process id.
  // This will throw an exception if there is no such process.
  Process^ localById = Process::GetProcessById(1234);


  // Get processes running on a remote computer. Note that this
  // and all the following calls will timeout and throw an exception
  // if "myComputer" and 169.0.0.0 do not exist on your local network.

  // Get all processes on a remote computer.
  array<Process^>^remoteAll = Process::GetProcesses("myComputer");

  // Get all instances of Notepad running on the specific computer, using machine name.
  array<Process^>^remoteByName = Process::GetProcessesByName( "notepad", "myComputer" );

  // Get all instances of Notepad running on the specific computer, using IP address.
  array<Process^>^ipByName = Process::GetProcessesByName( "notepad", "169.0.0.0" );

  // Get a process on a remote computer, using the process id and machine name.
  Process^ remoteById = Process::GetProcessById( 2345, "myComputer" );
}

上面的代码是由微软自己编写的,你可以在这里看到完整的答案:

https://msdn.microsoft.com/en-us/library/z3w4xdc9(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-2