0

这是我的PSAPI.java接口:

import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;

    public interface PsApi extends StdCallLibrary {

        public static abstract class PPROCESS_MEMORY_COUNTERS extends Structure {

            public int cb;
            public int PageFaultCount;
            public int PeakWorkingSetSize;
            public int WorkingSetSize;
            public int QuotaPeakPagedPoolUsage;
            public int QuotaPagedPoolUsage;
            public int QuotaPeakNonPagedPoolUsage;
            public int QuotaNonPagedPoolUsage;
            public int PagefileUsage;
            public int PeakPagefileUsage;
        }

        boolean GetProcessMemoryInfo(HANDLE process, PsApi pmem, int cb);
    }

另一个版本(没有摘要):

public static class PPROCESS_MEMORY_COUNTERS extends Structure {

    public int cb;
    public int PageFaultCount;
    public int PeakWorkingSetSize;
    public int WorkingSetSize;
    public int QuotaPeakPagedPoolUsage;
    public int QuotaPagedPoolUsage;
    public int QuotaPeakNonPagedPoolUsage;
    public int QuotaNonPagedPoolUsage;
    public int PagefileUsage;
    public int PeakPagefileUsage;
    @Override
    protected List getFieldOrder() {
        // TODO Auto-generated method stub
        return null;
    }
}

要使用GetProcessMemoryInfo()函数,我应该声明变量:

PPROCESS_MEMORY_COUNTERS pmem;

但它返回一个错误:

PPROCESS_MEMORY_COUNTERS 无法解析为类型

如何正确包含 PPROCESS_MEMORY_COUNTERS 结构?谢谢。

4

1 回答 1

0

应评论员的要求,我发布了一个代码片段,其中显示了正在运行的进程列表(id、进程名称、线程数):

public class windows {
    public static void main(String[] args) {
        Kernel32 kernel32 = Kernel32.INSTANCE;
        WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
        Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();

        try {
            while (kernel32.Process32Next(snapshot, processEntry)) {
                HANDLE process = Kernel32.INSTANCE.OpenProcess(0x0400 | 0x0010,
                        false, 0);
                if (process != null) {
                    int len = psapi.GetModuleFileNameExW(process, null, path,
                            1024);
                    if (len > 0)
                        System.out.println(new String(path, 0, len));
                    else
                        System.out.println("GetModuleFileNameW failed");
                } else
                    System.out.println("OpenProcess failed");

                System.out.println(processEntry.th32ProcessID + "\t"
                        + Native.toString(processEntry.szExeFile) + "\t"
                        + processEntry.cntThreads);
            }
        } finally {
            kernel32.CloseHandle(snapshot);
        }
}

使用进程 ID(请参阅JNA API 文档),您可以获得每个进程的内存使用情况。

于 2014-08-01T16:13:28.030 回答