JNA-查询Windows进程 [英] JNA - Query Windows Processes

查看:151
本文介绍了JNA-查询Windows进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JNA返回有关特定Windows进程的详细信息.不确定如何执行此操作.在互助网上找不到太多帮助.我想返回的一些信息包括CPU和内存使用情况.下面只是我发现的一个例子.

I'm trying to use JNA to return details on a specific Windows Process. Not exactly sure how to do this. Couldn't find much on the interwebs for help. Some information I would like returned include CPU and memory usage. The below is just an example I found.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import com.sun.jna.*;
import com.sun.jna.Library.Handler;
import com.sun.jna.platform.win32.*;
import com.sun.jna.platform.win32.Advapi32Util.*;
import com.sun.jna.platform.win32.WinNT.*;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.*;

import com.sun.jna.Native; 
import com.sun.jna.platform.win32.*; 
import com.sun.jna.win32.W32APIOptions;


public class WindowsProcess {

    public static void main(String[] args) {

        WinNT winNT = (WinNT) Native.loadLibrary(WinNT.class, W32APIOptions.UNICODE_OPTIONS);          
        WinNT.HANDLE snapshot = winNT.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));  
        Thelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();          

        while (winNT.Process32Next(snapshot, processEntry)) {             
        System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile));         

        }          
        winNT.CloseHandle(snapshot);     
        } 

    }

推荐答案

JNA 3.5兼容.0.我认为您所使用的示例与该库的最新版本不兼容.

This works with JNA 3.5.0. The example you have isn't compatible with more recent versions of the library, I think.

import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.Tlhelp32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.win32.W32APIOptions;
import com.sun.jna.Native; 

public class ListProcesses {
    public static void main(String[] args) {
        Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
        Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();          

        WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
        try  {
            while (kernel32.Process32Next(snapshot, processEntry)) {             
                System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile));
            }
        }
        finally {
            kernel32.CloseHandle(snapshot);
        }
    } 
}

另请参阅我的答案在其他地方.

这篇关于JNA-查询Windows进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆