【项目亲历】JAVA JNI 获取任务管理器 所有信息

    由于之前第一次使用本地方法时详细记录过程,2次使用本地方法省了不少事,但是还有遇到一些麻烦,用了一天的时间才弄出来,记录下过程。
 

    步骤一:在“C:/c/com/swjtu/hardinfo”里建立文件SystemLoad.java,文件代码如下:

 

 

package com.swjtu.hardinfo;

public class SystemLoad {
	
	
	
	public static native void PerfDataRefresh();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	
	public static native boolean PerfDataInitialize();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	
	public static native long PerfDataGetProcessorUsage();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetCommitChargeTotalK();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetCommitChargeLimitK();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetCommitChargePeakK();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetKernelMemoryTotalK();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetKernelMemoryPagedK();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetKernelMemoryNonPagedK();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetPhysicalMemoryTotalK();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetPhysicalMemoryAvailableK();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetPhysicalMemorySystemCacheK();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetSystemHandleCount();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetTotalThreadCount();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	public static native long PerfDataGetProcessCount();
	static
	{
	System.loadLibrary("SystemLoad");
	}
	
	
}
步骤二:点击“开始”——>“运行”——>cmd 敲命令 Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
C:/Documents and Settings/Administrator>cd c:/c
C:/c>cd com/swjtu/hardinfo
C:/c/com/swjtu/hardinfo>javac SystemLoad.java
C:/c/com/swjtu/hardinfo>cd c:/c
C:/c>javah com.swjtu.hardinfo.SystemLoad
C:/c>
注:javah命令在c:/c文件夹下生成了com_swjtu_hardinfo_SystemLoad.h头文件,文件代码如下:
 
/* DO NOT EDIT THIS FILE - it is machine generated */
#include 
 
 
  
  
/* Header for class com_swjtu_hardinfo_SystemLoad */

#ifndef _Included_com_swjtu_hardinfo_SystemLoad
#define _Included_com_swjtu_hardinfo_SystemLoad
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataRefresh
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataRefresh
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataInitialize
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataInitialize
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetProcessorUsage
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetProcessorUsage
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetCommitChargeTotalK
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetCommitChargeTotalK
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetCommitChargeLimitK
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetCommitChargeLimitK
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetCommitChargePeakK
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetCommitChargePeakK
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetKernelMemoryTotalK
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetKernelMemoryTotalK
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetKernelMemoryPagedK
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetKernelMemoryPagedK
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetKernelMemoryNonPagedK
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetKernelMemoryNonPagedK
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetPhysicalMemoryTotalK
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetPhysicalMemoryTotalK
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetPhysicalMemoryAvailableK
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetPhysicalMemoryAvailableK
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetPhysicalMemorySystemCacheK
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetPhysicalMemorySystemCacheK
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetSystemHandleCount
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetSystemHandleCount
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetTotalThreadCount
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetTotalThreadCount
  (JNIEnv *, jclass);

/*
 * Class:     com_swjtu_hardinfo_SystemLoad
 * Method:    PerfDataGetProcessCount
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetProcessCount
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

 
 

步骤三:在文件夹“C:/c”建立com_swjtu_hardinfo_SystemLoad.cpp文件,文件内容如下:

 

#include "com_swjtu_hardinfo_SystemLoad.h"

#include "stdafx.h"
#include "taskmgr.h"
#include "TCHAR.H"
#include 
 
 
  
  
using namespace std;


CRITICAL_SECTION                 PerfDataCriticalSection;
PPERFDATA                        pPerfDataOld = NULL;    /* Older perf data (saved to establish delta values) */
PPERFDATA                        pPerfData = NULL;    /* Most recent copy of perf data */
ULONG                            ProcessCountOld = 0;
ULONG                            ProcessCount = 0;
double                            dbIdleTime;
double                            dbKernelTime;
double                            dbSystemTime;
LARGE_INTEGER                    liOldIdleTime = {{0,0}};
double                            OldKernelTime = 0;
LARGE_INTEGER                    liOldSystemTime = {{0,0}};
SYSTEM_PERFORMANCE_INFORMATION    SystemPerfInfo;
SYSTEM_BASIC_INFORMATION        SystemBasicInfo;
SYSTEM_CACHE_INFORMATION        SystemCacheInfo;
SYSTEM_HANDLE_INFORMATION        SystemHandleInfo;
PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SystemProcessorTimeInfo = NULL;
PSID                             SystemUserSid = NULL;
typedef LONG (WINAPI *PROCNTQSI)(DWORD,PVOID,ULONG,PULONG);//指向Native API NtQuerySystemInformation
PROCNTQSI        NtQuerySystemInformation = NULL; //取系统性能函数

 

JNIEXPORT jboolean JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataInitialize(JNIEnv *, jclass)
//BOOL PerfDataInitialize(void)
{
    SID_IDENTIFIER_AUTHORITY NtSidAuthority = {SECURITY_NT_AUTHORITY};
    NTSTATUS    status;

    InitializeCriticalSection(&PerfDataCriticalSection);
 NtQuerySystemInformation = (long(__stdcall*)(DWORD,PVOID,DWORD,DWORD*))
        GetProcAddress(
        GetModuleHandle( "ntdll.dll" ),
        "NtQuerySystemInformation"
        );
    /*
     * Get number of processors in the system
     */
    status = NtQuerySystemInformation(SystemBasicInformation, &SystemBasicInfo, sizeof(SystemBasicInfo), NULL);
    if (status != NO_ERROR)
        return FALSE;

    /*
     * Create the SYSTEM Sid
     */
    AllocateAndInitializeSid(&NtSidAuthority, 1, SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, &SystemUserSid);
    return TRUE;
}

void PerfDataUninitialize(void)
{
    DeleteCriticalSection(&PerfDataCriticalSection);

    if (SystemUserSid != NULL)
    {
        FreeSid(SystemUserSid);
        SystemUserSid = NULL;
    }
}

static void SidToUserName(PSID Sid, LPTSTR szBuffer, DWORD BufferSize)
{
    static TCHAR szDomainNameUnused[255]; 
    DWORD DomainNameLen = sizeof(szDomainNameUnused) / sizeof(szDomainNameUnused[0]);
    SID_NAME_USE Use;

    if (Sid != NULL)
        LookupAccountSid(NULL, Sid, szBuffer, &BufferSize, szDomainNameUnused, &DomainNameLen, &Use);
}
JNIEXPORT void JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataRefresh
  (JNIEnv *, jclass)
//void PerfDataRefresh(void)
{
    ULONG                            ulSize;
    NTSTATUS                          status;
    LPBYTE                            pBuffer;
    ULONG                            BufferSize;
    PSYSTEM_PROCESS_INFORMATION        pSPI;
    PPERFDATA                        pPDOld;
    ULONG                            Idx, Idx2;
    HANDLE                            hProcess;
    HANDLE                            hProcessToken;
    SYSTEM_PERFORMANCE_INFORMATION    SysPerfInfo;
    SYSTEM_TIMEOFDAY_INFORMATION      SysTimeInfo;
    //SYSTEM_CACHE_INFORMATION        SysCacheInfo;
    LPBYTE                            SysHandleInfoData;
    PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SysProcessorTimeInfo;
    double                            CurrentKernelTime;
    PSECURITY_DESCRIPTOR              ProcessSD;
    PSID                              ProcessUser;
    ULONG                             Buffer[64]; /* must be 4 bytes aligned! */

    /* Get new system time */
    status = NtQuerySystemInformation(SystemTimeOfDayInformation, &SysTimeInfo, sizeof(SysTimeInfo), 0);
    if (status != NO_ERROR)
        return;

    /* Get new CPU's idle time */
    status = NtQuerySystemInformation(SystemPerformanceInformation, &SysPerfInfo, sizeof(SysPerfInfo), NULL);
    if (status != NO_ERROR)
        return;

    /* Get system cache information */
    /* status = NtQuerySystemInformation(SystemFileCacheInformation, &SysCacheInfo, sizeof(SysCacheInfo), NULL);
        if (status != NO_ERROR)
            return;
    */

    /* Get processor time information */
    SysProcessorTimeInfo = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)HeapAlloc(GetProcessHeap(), 0, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberOfProcessors);
    status = NtQuerySystemInformation(SystemProcessorPerformanceInformation, SysProcessorTimeInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberOfProcessors, &ulSize);
    if (status != NO_ERROR)
        return;

    /* Get handle information
     * We don't know how much data there is so just keep
     * increasing the buffer size until the call succeeds
     */
    BufferSize = 0;
    do
    {
        BufferSize += 0x10000;
        SysHandleInfoData = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, BufferSize);

        status = NtQuerySystemInformation(SystemHandleInformation, SysHandleInfoData, BufferSize, &ulSize);

        if (status == STATUS_INFO_LENGTH_MISMATCH) {
            HeapFree(GetProcessHeap(), 0, SysHandleInfoData);
        }

    } while (status == STATUS_INFO_LENGTH_MISMATCH);

    /* Get process information
     * We don't know how much data there is so just keep
     * increasing the buffer size until the call succeeds
     */
    BufferSize = 0;
    do
    {
        BufferSize += 0x10000;
        pBuffer = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, BufferSize);

        status = NtQuerySystemInformation(SystemProcessInformation, pBuffer, BufferSize, &ulSize);

        if (status == STATUS_INFO_LENGTH_MISMATCH) {
            HeapFree(GetProcessHeap(), 0, pBuffer);
        }

    } while (status == STATUS_INFO_LENGTH_MISMATCH);

    EnterCriticalSection(&PerfDataCriticalSection);

    /*
     * Save system performance info
     */
    memcpy(&SystemPerfInfo, &SysPerfInfo, sizeof(SYSTEM_PERFORMANCE_INFORMATION));

    /*
     * Save system cache info
     */
   // memcpy(&SystemCacheInfo, &SysCacheInfo, sizeof(SYSTEM_CACHE_INFORMATION));

    /*
     * Save system processor time info
     */
    if (SystemProcessorTimeInfo) {
        HeapFree(GetProcessHeap(), 0, SystemProcessorTimeInfo);
    }
    SystemProcessorTimeInfo = SysProcessorTimeInfo;

    /*
     * Save system handle info
     */
    memcpy(&SystemHandleInfo, SysHandleInfoData, sizeof(SYSTEM_HANDLE_INFORMATION));
    HeapFree(GetProcessHeap(), 0, SysHandleInfoData);

    for (CurrentKernelTime=0, Idx=0; Idx<(ULONG)SystemBasicInfo.NumberOfProcessors; Idx++) {
        CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].KernelTime);
        CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].DpcTime);
        CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].InterruptTime);
    }

    /* If it&apos;s a first call - skip idle time calcs */
    if (liOldIdleTime.QuadPart != 0) {
        /*  CurrentValue = NewValue - OldValue */
        dbIdleTime = Li2Double(SysPerfInfo.IdleProcessTime) - Li2Double(liOldIdleTime);
        dbKernelTime = CurrentKernelTime - OldKernelTime;
        dbSystemTime = Li2Double(SysTimeInfo.CurrentTime) - Li2Double(liOldSystemTime);

        /*  CurrentCpuIdle = IdleTime / SystemTime */
        dbIdleTime = dbIdleTime / dbSystemTime;
        dbKernelTime = dbKernelTime / dbSystemTime;

        /*  CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors */
        dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
        dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
    }

    /* Store new CPU&apos;s idle and system time */
    liOldIdleTime = SysPerfInfo.IdleProcessTime;
    liOldSystemTime = SysTimeInfo.CurrentTime;
    OldKernelTime = CurrentKernelTime;

    /* Determine the process count
     * We loop through the data we got from NtQuerySystemInformation
     * and count how many structures there are (until RelativeOffset is 0)
     */
    ProcessCountOld = ProcessCount;
    ProcessCount = 0;
    pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
    while (pSPI) {
        ProcessCount++;
        if (pSPI->NextEntryOffset == 0)
            break;
        pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->NextEntryOffset);
    }

    /* Now alloc a new PERFDATA array and fill in the data */
    if (pPerfDataOld) {
        HeapFree(GetProcessHeap(), 0, pPerfDataOld);
    }
    pPerfDataOld = pPerfData;
    pPerfData = (PPERFDATA)HeapAlloc(GetProcessHeap(), 0, sizeof(PERFDATA) * ProcessCount);
    pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
    for (Idx=0; Idx
  
  
   
   UniqueProcessId) {
                pPDOld = &pPerfDataOld[Idx2];
                break;
            }
        }

        /* Clear out process perf data structure */
        memset(&pPerfData[Idx], 0, sizeof(PERFDATA));

        if (pSPI->ImageName.Buffer)
            wcscpy(pPerfData[Idx].ImageName, pSPI->ImageName.Buffer);
        else
  {
        //    LoadStringW(hInst, IDS_IDLE_PROCESS, pPerfData[Idx].ImageName,
        //                sizeof(pPerfData[Idx].ImageName) / sizeof(pPerfData[Idx].ImageName[0]));
  }

        pPerfData[Idx].ProcessId = pSPI->UniqueProcessId;

        if (pPDOld)    {
            double    CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
            double    OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
            double    CpuTime = (CurTime - OldTime) / dbSystemTime;
            CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
            pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
        }
        pPerfData[Idx].CPUTime.QuadPart = pSPI->UserTime.QuadPart + pSPI->KernelTime.QuadPart;
        pPerfData[Idx].WorkingSetSizeBytes = pSPI->WorkingSetSize;
        pPerfData[Idx].PeakWorkingSetSizeBytes = pSPI->PeakWorkingSetSize;
        if (pPDOld)
            pPerfData[Idx].WorkingSetSizeDelta = labs((LONG)pSPI->WorkingSetSize - (LONG)pPDOld->WorkingSetSizeBytes);
        else
            pPerfData[Idx].WorkingSetSizeDelta = 0;
        pPerfData[Idx].PageFaultCount = pSPI->PageFaultCount;
        if (pPDOld)
            pPerfData[Idx].PageFaultCountDelta = labs((LONG)pSPI->PageFaultCount - (LONG)pPDOld->PageFaultCount);
        else
            pPerfData[Idx].PageFaultCountDelta = 0;
        pPerfData[Idx].VirtualMemorySizeBytes = pSPI->VirtualSize;
        pPerfData[Idx].PagedPoolUsagePages = pSPI->QuotaPeakPagedPoolUsage;
        pPerfData[Idx].NonPagedPoolUsagePages = pSPI->QuotaPeakNonPagedPoolUsage;
        pPerfData[Idx].BasePriority = pSPI->BasePriority;
        pPerfData[Idx].HandleCount = pSPI->HandleCount;
        pPerfData[Idx].ThreadCount = pSPI->NumberOfThreads;
        pPerfData[Idx].SessionId = pSPI->SessionId;
        pPerfData[Idx].UserName[0] = _T(&apos;/0&apos;);
        pPerfData[Idx].USERObjectCount = 0;
        pPerfData[Idx].GDIObjectCount = 0;
        ProcessUser = SystemUserSid;
        ProcessSD = NULL;

        if (pSPI->UniqueProcessId != NULL) {
            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | READ_CONTROL, FALSE, PtrToUlong(pSPI->UniqueProcessId));
            if (hProcess) {
                /* don&apos;t query the information of the system process. It&apos;s possible but
                   returns Administrators as the owner of the process instead of SYSTEM */
                if (pSPI->UniqueProcessId != 0x4)
                {
                    if (OpenProcessToken(hProcess, TOKEN_QUERY, &hProcessToken))
                    {
                        DWORD RetLen = 0;
                        BOOL Ret;

                        Ret = GetTokenInformation(hProcessToken, TokenUser, (LPVOID)Buffer, sizeof(Buffer), &RetLen);
                        CloseHandle(hProcessToken);

                        if (Ret)
                            ProcessUser = ((PTOKEN_USER)Buffer)->User.Sid;
                        else
                            goto ReadProcOwner;
                    }
                    else
                    {
ReadProcOwner:
     ;
                     //   GetSecurityInfo(hProcess, SE_KERNEL_OBJECT, OWNER_SECURITY_INFORMATION, &ProcessUser, NULL, NULL, NULL, &ProcessSD);
                    }

                    pPerfData[Idx].USERObjectCount = GetGuiResources(hProcess, GR_USEROBJECTS);
                    pPerfData[Idx].GDIObjectCount = GetGuiResources(hProcess, GR_GDIOBJECTS);
                }

                GetProcessIoCounters(hProcess, &pPerfData[Idx].IOCounters);
                CloseHandle(hProcess);
            } else {
                goto ClearInfo;
            }
        } else {
ClearInfo:
            /* clear information we were unable to fetch */
            ZeroMemory(&pPerfData[Idx].IOCounters, sizeof(IO_COUNTERS));
        }

        //SidToUserName(ProcessUser, pPerfData[Idx].UserName, sizeof(pPerfData[0].UserName) / sizeof(pPerfData[0].UserName[0]));

        if (ProcessSD != NULL)
        {
            LocalFree((HLOCAL)ProcessSD);
        }

        pPerfData[Idx].UserTime.QuadPart = pSPI->UserTime.QuadPart;
        pPerfData[Idx].KernelTime.QuadPart = pSPI->KernelTime.QuadPart;
        pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->NextEntryOffset);
    }
    HeapFree(GetProcessHeap(), 0, pBuffer);
    LeaveCriticalSection(&PerfDataCriticalSection);
}

ULONG PerfDataGetProcessCount(void)//获得进程数目
{
    return ProcessCount;
}
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetProcessorUsage
  (JNIEnv *, jclass)
//ULONG PerfDataGetProcessorUsage(void)//获得CPU使用率
{
    return (ULONG)dbIdleTime;
}

ULONG PerfDataGetProcessorSystemUsage(void)
{
    return (ULONG)dbKernelTime;
}

BOOL PerfDataGetImageName(ULONG Index, LPTSTR lpImageName, int nMaxCount)
{
    BOOL    bSuccessful;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount) {
        #ifdef _UNICODE
            wcsncpy(lpImageName, pPerfData[Index].ImageName, nMaxCount);
        #else
            WideCharToMultiByte(CP_ACP, 0, pPerfData[Index].ImageName, -1, lpImageName, nMaxCount, NULL, NULL);
        #endif

        bSuccessful = TRUE;
    } else {
        bSuccessful = FALSE;
    }
    LeaveCriticalSection(&PerfDataCriticalSection);
    return bSuccessful;
}

int PerfGetIndexByProcessId(DWORD dwProcessId)
{
    int Index, FoundIndex = -1;

    EnterCriticalSection(&PerfDataCriticalSection);

    for (Index = 0; Index < (int)ProcessCount; Index++)
    {
        if ((DWORD)pPerfData[Index].ProcessId == dwProcessId)
        {
            FoundIndex = Index;
            break;
        }
    }

    LeaveCriticalSection(&PerfDataCriticalSection);

    return FoundIndex;
}

ULONG PerfDataGetProcessId(ULONG Index)
{
    ULONG    ProcessId;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        ProcessId = (ULONG)pPerfData[Index].ProcessId;
    else
        ProcessId = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return ProcessId;
}

BOOL PerfDataGetUserName(ULONG Index, LPTSTR lpUserName, int nMaxCount)
{
    BOOL    bSuccessful;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount) {
        #ifdef _UNICODE
            wcsncpy(lpUserName, pPerfData[Index].UserName, nMaxCount);
        #else
            WideCharToMultiByte(CP_ACP, 0, pPerfData[Index].UserName, -1, lpUserName, nMaxCount, NULL, NULL);
        #endif

        bSuccessful = TRUE;
    } else {
        bSuccessful = FALSE;
    }

    LeaveCriticalSection(&PerfDataCriticalSection);

    return bSuccessful;
}

ULONG PerfDataGetSessionId(ULONG Index)
{
    ULONG    SessionId;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        SessionId = pPerfData[Index].SessionId;
    else
        SessionId = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return SessionId;
}

ULONG PerfDataGetCPUUsage(ULONG Index)
{
    ULONG    CpuUsage;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        CpuUsage = pPerfData[Index].CPUUsage;
    else
        CpuUsage = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return CpuUsage;
}

LARGE_INTEGER PerfDataGetCPUTime(ULONG Index)
{
    LARGE_INTEGER    CpuTime = {{0,0}};

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        CpuTime = pPerfData[Index].CPUTime;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return CpuTime;
}

ULONG PerfDataGetWorkingSetSizeBytes(ULONG Index)
{
    ULONG    WorkingSetSizeBytes;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        WorkingSetSizeBytes = pPerfData[Index].WorkingSetSizeBytes;
    else
        WorkingSetSizeBytes = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return WorkingSetSizeBytes;
}

ULONG PerfDataGetPeakWorkingSetSizeBytes(ULONG Index)
{
    ULONG    PeakWorkingSetSizeBytes;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        PeakWorkingSetSizeBytes = pPerfData[Index].PeakWorkingSetSizeBytes;
    else
        PeakWorkingSetSizeBytes = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return PeakWorkingSetSizeBytes;
}

ULONG PerfDataGetWorkingSetSizeDelta(ULONG Index)
{
    ULONG    WorkingSetSizeDelta;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        WorkingSetSizeDelta = pPerfData[Index].WorkingSetSizeDelta;
    else
        WorkingSetSizeDelta = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return WorkingSetSizeDelta;
}

ULONG PerfDataGetPageFaultCount(ULONG Index)
{
    ULONG    PageFaultCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        PageFaultCount = pPerfData[Index].PageFaultCount;
    else
        PageFaultCount = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return PageFaultCount;
}

ULONG PerfDataGetPageFaultCountDelta(ULONG Index)
{
    ULONG    PageFaultCountDelta;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        PageFaultCountDelta = pPerfData[Index].PageFaultCountDelta;
    else
        PageFaultCountDelta = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return PageFaultCountDelta;
}

ULONG PerfDataGetVirtualMemorySizeBytes(ULONG Index)
{
    ULONG    VirtualMemorySizeBytes;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        VirtualMemorySizeBytes = pPerfData[Index].VirtualMemorySizeBytes;
    else
        VirtualMemorySizeBytes = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return VirtualMemorySizeBytes;
}

ULONG PerfDataGetPagedPoolUsagePages(ULONG Index)
{
    ULONG    PagedPoolUsage;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        PagedPoolUsage = pPerfData[Index].PagedPoolUsagePages;
    else
        PagedPoolUsage = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return PagedPoolUsage;
}

ULONG PerfDataGetNonPagedPoolUsagePages(ULONG Index)
{
    ULONG    NonPagedPoolUsage;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        NonPagedPoolUsage = pPerfData[Index].NonPagedPoolUsagePages;
    else
        NonPagedPoolUsage = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return NonPagedPoolUsage;
}

ULONG PerfDataGetBasePriority(ULONG Index)
{
    ULONG    BasePriority;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        BasePriority = pPerfData[Index].BasePriority;
    else
        BasePriority = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return BasePriority;
}
//进程的句柄数
ULONG PerfDataGetHandleCount(ULONG Index)
{
    ULONG    HandleCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        HandleCount = pPerfData[Index].HandleCount;
    else
        HandleCount = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return HandleCount;
}
//  进程的线程数
ULONG PerfDataGetThreadCount(ULONG Index)
{
    ULONG    ThreadCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        ThreadCount = pPerfData[Index].ThreadCount;
    else
        ThreadCount = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return ThreadCount;
}

ULONG PerfDataGetUSERObjectCount(ULONG Index)
{
    ULONG    USERObjectCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        USERObjectCount = pPerfData[Index].USERObjectCount;
    else
        USERObjectCount = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return USERObjectCount;
}

ULONG PerfDataGetGDIObjectCount(ULONG Index)
{
    ULONG    GDIObjectCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        GDIObjectCount = pPerfData[Index].GDIObjectCount;
    else
        GDIObjectCount = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return GDIObjectCount;
}

BOOL PerfDataGetIOCounters(ULONG Index, PIO_COUNTERS pIoCounters)
{
    BOOL    bSuccessful;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
    {
        memcpy(pIoCounters, &pPerfData[Index].IOCounters, sizeof(IO_COUNTERS));
        bSuccessful = TRUE;
    }
    else
        bSuccessful = FALSE;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return bSuccessful;
}
//提交总量
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetCommitChargeTotalK
  (JNIEnv *, jclass)
//ULONG PerfDataGetCommitChargeTotalK(void)
{
    ULONG    Total;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Total = SystemPerfInfo.CommittedPages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Total = Total * (PageSize / 1024);

    return Total;
}
//提交限制
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetCommitChargeLimitK
  (JNIEnv *, jclass)
//ULONG PerfDataGetCommitChargeLimitK(void)
{
    ULONG    Limit;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Limit = SystemPerfInfo.CommitLimit;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Limit = Limit * (PageSize / 1024);

    return Limit;
}
//提交峰值
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetCommitChargePeakK
  (JNIEnv *, jclass)
//ULONG PerfDataGetCommitChargePeakK(void)
{
    ULONG    Peak;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Peak = SystemPerfInfo.PeakCommitment;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Peak = Peak * (PageSize / 1024);

    return Peak;
}
//核心内存总量
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetKernelMemoryTotalK
  (JNIEnv *, jclass)
//ULONG PerfDataGetKernelMemoryTotalK(void)
{
    ULONG    Total;
    ULONG    Paged;
    ULONG    NonPaged;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Paged = SystemPerfInfo.PagedPoolPages;
    NonPaged = SystemPerfInfo.NonPagedPoolPages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Paged = Paged * (PageSize / 1024);
    NonPaged = NonPaged * (PageSize / 1024);

    Total = Paged + NonPaged;

    return Total;
}
//内核分页内存
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetKernelMemoryPagedK
  (JNIEnv *, jclass)
//ULONG PerfDataGetKernelMemoryPagedK(void)
{
    ULONG    Paged;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Paged = SystemPerfInfo.PagedPoolPages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Paged = Paged * (PageSize / 1024);

    return Paged;
}
//内核非分页内存
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetKernelMemoryNonPagedK
  (JNIEnv *, jclass)
//ULONG PerfDataGetKernelMemoryNonPagedK(void)
{
    ULONG    NonPaged;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    NonPaged = SystemPerfInfo.NonPagedPoolPages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    NonPaged = NonPaged * (PageSize / 1024);

    return NonPaged;
}
//总物理内存
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetPhysicalMemoryTotalK
  (JNIEnv *, jclass)
//ULONG PerfDataGetPhysicalMemoryTotalK(void)
{
    ULONG    Total;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Total = SystemBasicInfo.NumberOfPhysicalPages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Total = Total * (PageSize / 1024);

    return Total;
}
//可用物理内存
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetPhysicalMemoryAvailableK
  (JNIEnv *, jclass)
//ULONG PerfDataGetPhysicalMemoryAvailableK(void)
{
    ULONG    Available;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Available = SystemPerfInfo.AvailablePages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Available = Available * (PageSize / 1024);

    return Available;
}
//物理内存系统缓存
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetPhysicalMemorySystemCacheK
  (JNIEnv *, jclass)
//ULONG PerfDataGetPhysicalMemorySystemCacheK(void)
{
    ULONG    SystemCache;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    PageSize = SystemBasicInfo.PageSize;
    SystemCache = SystemCacheInfo.CurrentSize * PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return SystemCache / 1024;
}
//句柄数
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetSystemHandleCount
  (JNIEnv *, jclass)
//ULONG PerfDataGetSystemHandleCount(void)
{
    ULONG    HandleCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    HandleCount = SystemHandleInfo.Count;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return HandleCount;
}
//线程数
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetTotalThreadCount
  (JNIEnv *, jclass)
//ULONG PerfDataGetTotalThreadCount(void)
{
    ULONG    ThreadCount = 0;
    ULONG    i;

    EnterCriticalSection(&PerfDataCriticalSection);

    for (i=0; i< ProcessCount; i++)
    {
        ThreadCount += pPerfData[i].ThreadCount;
    }

    LeaveCriticalSection(&PerfDataCriticalSection);

    return ThreadCount;
}

/*
int main()
{
  PerfDataInitialize();
  while (1)
  {
   //...get data
   //for eg.
   PerfDataRefresh();
   
  
   cout <<"CPU使用率: "<< PerfDataGetProcessorUsage()<< endl;
   //cout <<"信息: "<< PerfDataGetProcessorSystemUsage()<< endl;
   cout <<"认可用量(K),总数:"<< PerfDataGetCommitChargeTotalK()<< endl;
   cout <<"认可用量(K),限制:"<< PerfDataGetCommitChargeLimitK()<< endl;
   cout <<"认可用量(K),峰值:"<< PerfDataGetCommitChargePeakK()<< endl;
   cout <<"核心内存(K),总数:"<< PerfDataGetKernelMemoryTotalK()<< endl;
   cout <<"核心内存(K),分页数:"<< PerfDataGetKernelMemoryPagedK()<< endl;
   cout <<"核心内存(K),未分页:"<< PerfDataGetKernelMemoryNonPagedK()<< endl;
   cout <<"物理内存(K),总数:"<< PerfDataGetPhysicalMemoryTotalK()<< endl;
   cout <<"物理内存(K),可用数:"<< PerfDataGetPhysicalMemoryAvailableK()<< endl;
   cout <<"物理内存(K),系统缓存:"<< PerfDataGetPhysicalMemorySystemCacheK()<< endl;
   cout <<"总数,句柄数:"<< PerfDataGetSystemHandleCount()<< endl;
   cout <<"总数,线程数:"<< PerfDataGetTotalThreadCount()<< endl;
    cout <<"总数,进程数目: "<< PerfDataGetProcessCount()<< endl;
   cout <<"CPU: PID:0 ,Usage: "<< PerfDataGetCPUUsage(PerfGetIndexByProcessId(0))<< endl;
   cout <<"CPU: PID:0 ,Usage: "<< PerfDataGetCPUUsage(PerfGetIndexByProcessId(4))<< endl;
   Sleep(1000);
  }
  return 0;
}

*/
  
  
 
 

步骤四:生成DLL文件 使用命令:cl -IC:/include -LD com_swjtu_hardinfo_SystemLoad.cpp -FeSystemLoad.dll如果执行过程中出现找不到头文件错误,则将该头文件所在目录加入到命令中(如-IC:/include,目录名不要有空格),如果执行过程出现打不开lib文件错误,则将相应的文件拷贝到C:/c文件夹下。成功后生成SystemLoad.dll,将这个文件拷贝到com文件同目录。

步骤五:编写个JAVA文件调用这个方法。如下代码:

 

package com.swjtu.hardinfo;

class gethardinfo
{
public static void main(String[] args)
{

long total=0,free=0;
int percent=0;
percent=Memo.percent();
total=Memo.total();
free=Memo.free();
SystemLoad.PerfDataInitialize();
while(true){
SystemLoad.PerfDataRefresh();



System.out.println("CommitChargeLimit:"+SystemLoad.PerfDataGetCommitChargeLimitK());
System.out.println("CommitChargePeak:"+SystemLoad.PerfDataGetCommitChargePeakK());
System.out.println("CommitChargeTotal:"+SystemLoad.PerfDataGetCommitChargeTotalK());
System.out.println("KernelMemoryNonPaged:"+SystemLoad.PerfDataGetKernelMemoryNonPagedK());
System.out.println("KernelMemoryPaged:"+SystemLoad.PerfDataGetKernelMemoryPagedK());
System.out.println("KernelMemoryTotal:"+SystemLoad.PerfDataGetKernelMemoryTotalK());
System.out.println("PhysicalMemoryAvailable:"+SystemLoad.PerfDataGetPhysicalMemoryAvailableK());
System.out.println("PhysicalMemorySystemCache:"+SystemLoad.PerfDataGetPhysicalMemorySystemCacheK());
System.out.println("PhysicalMemoryTotal"+SystemLoad.PerfDataGetPhysicalMemoryTotalK());
System.out.println("ProcessCount:"+SystemLoad.PerfDataGetProcessCount());
System.out.println("ProcessorUsage:"+SystemLoad.PerfDataGetProcessorUsage());
System.out.println("SystemHandleCount:"+SystemLoad.PerfDataGetSystemHandleCount());
System.out.println("TotalThreadCount:"+SystemLoad.PerfDataGetTotalThreadCount());



System.out.println(percent);
System.out.println(total);
System.out.println(free);
try {
	Thread.sleep(1000);
} catch (InterruptedException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
}
}

}

运行这个文件,报如下错误:

CommitChargeLimit:1917912
CommitChargePeak:1404836
CommitChargeTotal:1074556
KernelMemoryNonPaged:34812
KernelMemoryPaged:83968
KernelMemoryTotal:118780
PhysicalMemoryAvailable:285704
PhysicalMemorySystemCache:0
PhysicalMemoryTotal784492
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.swjtu.hardinfo.SystemLoad.PerfDataGetProcessCount()J
 at com.swjtu.hardinfo.SystemLoad.PerfDataGetProcessCount(Native Method)
 at com.swjtu.hardinfo.gethardinfo.main(gethardinfo.java:28)
。。。。。。。晕了。。。回头查看com_swjtu_hardinfo_SystemLoad.cpp文件。。发现341行的代码。。。ULONG PerfDataGetProcessCount(void)//获得进程数目 没有改好。。。啊~啊~ 傻了。。。。。

将这一行用JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetProcessCount
  (JNIEnv *, jclass)替换,再用CL命令:

C:/c>cl -IC:/Progra~1/Java/jdk1.6.0_03/include -IC:/Progra~1/Java/jdk1.6.0_03/in
clude/win32 -IC:/include -IC:/include/Include -IC:/include/in2 -IC:/include/lib
-LD com_swjtu_hardinfo_SystemLoad.cpp -FeSystemLoad.dll
用于 80x86 的 Microsoft (R) 32 位 C/C++ 优化编译器 15.00.21022.08 版
版权所有(C) Microsoft Corporation。保留所有权利。

com_swjtu_hardinfo_SystemLoad.cpp
C:/include/in2/afxwin1.inl(105) : warning C4530: 使用了 C++ 异常处理程序,但未启
用展开语义。请指定 /EHsc
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

/dll
/implib:SystemLoad.lib
/out:SystemLoad.dll
com_swjtu_hardinfo_SystemLoad.obj
   正在创建库 SystemLoad.lib 和对象 SystemLoad.exp

C:/c>

再次运行gethardinfo.java 得下面结果:

CommitChargeLimit:1917912
CommitChargePeak:1404836
CommitChargeTotal:1010604
KernelMemoryNonPaged:34648
KernelMemoryPaged:80340
KernelMemoryTotal:114988
PhysicalMemoryAvailable:277972
PhysicalMemorySystemCache:0
PhysicalMemoryTotal784492
ProcessCount:49
ProcessorUsage:0
SystemHandleCount:55823
TotalThreadCount:544
64
784492
278460
CommitChargeLimit:1917912
CommitChargePeak:1404836
CommitChargeTotal:1010736
KernelMemoryNonPaged:34648
KernelMemoryPaged:80340
KernelMemoryTotal:114988
PhysicalMemoryAvailable:278128
PhysicalMemorySystemCache:0
PhysicalMemoryTotal784492
ProcessCount:49
ProcessorUsage:8
SystemHandleCount:55824
TotalThreadCount:544
64
784492
278460
CommitChargeLimit:1917912
CommitChargePeak:1404836
CommitChargeTotal:1010788
KernelMemoryNonPaged:34648
KernelMemoryPaged:80340
KernelMemoryTotal:114988
PhysicalMemoryAvailable:278708
PhysicalMemorySystemCache:0
PhysicalMemoryTotal784492
ProcessCount:49
ProcessorUsage:8
SystemHandleCount:55824
TotalThreadCount:544
64
784492
278460
CommitChargeLimit:1917912
CommitChargePeak:1404836
CommitChargeTotal:1010792
KernelMemoryNonPaged:34648
KernelMemoryPaged:80340
KernelMemoryTotal:114988
PhysicalMemoryAvailable:278036
PhysicalMemorySystemCache:0
PhysicalMemoryTotal784492
ProcessCount:49
ProcessorUsage:10
SystemHandleCount:55827
TotalThreadCount:544
64
784492
278460

 

最后:至此 所有步骤都完成了。主要容易出问题的地方是编译生成DLL文件步骤,一般出现缺少头文件 或打不开LIB文件,解决方法就是把缺少的加进去—_—!! 最后还想说。。。我搞以上所有东西花了一天。。写这篇东西花了一下午。。。。。。啊~啊~~~CSDN的文章编辑器真是太强大了~~~~~~~ 代码里出现< p 和 < e 的时候都出错。。必须在<和p/e间加空格。。。。困扰啊~~。。。。最后 写个文章太不容易了,要不是为了以后方便查阅。记录下来还是有用的,以后需要进行类似开发,就不用从零开始了。

 

==============================================================

以为搞完了吧,哈哈第二天一看,又出问题了。

CPU dominant frequency:1662
CommitChargeLimit:1917912
CommitChargePeak:951712
CommitChargeTotal:949424
KernelMemoryNonPaged:28200
KernelMemoryPaged:55212
KernelMemoryTotal:83412
PhysicalMemoryAvailable:136272
PhysicalMemorySystemCache:0
PhysicalMemoryTotal784492
ProcessCount:47
ProcessorUsage:6
SystemHandleCount:55310
TotalThreadCount:515

把代码中的关于计算系统缓存的注释去掉,编译 算出来的系统缓存还是不对。经过查阅资料,得出解决方案:

将其中的获取物理内存系统缓存的函数如下修改:

 

//物理内存系统缓存
ULONG PerfDataGetPhysicalMemorySystemCacheK(void)
{
    ULONG    SystemCache;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    PageSize = SystemBasicInfo.PageSize;
   SystemCache = SystemCacheInfo.unused[0] * PageSize;
    LeaveCriticalSection(&PerfDataCriticalSection);
    return SystemCache /1024;
}

问题解决。运行时的结果与windows任务管理器的对比如下:

 

 

经过修改和测试,其最终com_swjtu_hardinfo_SystemLoad.cpp文件代码 如下:

#include "com_swjtu_hardinfo_SystemLoad.h"

#include "stdafx.h"
#include "taskmgr.h"
#include "TCHAR.H"
#include < iostream>
using namespace std;


CRITICAL_SECTION                 PerfDataCriticalSection;
PPERFDATA                        pPerfDataOld = NULL;    /* Older perf data (saved to establish delta values) */
PPERFDATA                        pPerfData = NULL;    /* Most recent copy of perf data */
ULONG                            ProcessCountOld = 0;
ULONG                            ProcessCount = 0;
double                            dbIdleTime;
double                            dbKernelTime;
double                            dbSystemTime;
LARGE_INTEGER                    liOldIdleTime = {{0,0}};
double                            OldKernelTime = 0;
LARGE_INTEGER                    liOldSystemTime = {{0,0}};
SYSTEM_PERFORMANCE_INFORMATION    SystemPerfInfo;
SYSTEM_BASIC_INFORMATION        SystemBasicInfo;
SYSTEM_CACHE_INFORMATION        SystemCacheInfo;
SYSTEM_HANDLE_INFORMATION        SystemHandleInfo;
PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SystemProcessorTimeInfo = NULL;
PSID                             SystemUserSid = NULL;
typedef LONG (WINAPI *PROCNTQSI)(DWORD,PVOID,ULONG,PULONG);//指向Native API NtQuerySystemInformation
PROCNTQSI        NtQuerySystemInformation = NULL; //取系统性能函数

 

JNIEXPORT jboolean JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataInitialize(JNIEnv *, jclass)
//BOOL PerfDataInitialize(void)
{
    SID_IDENTIFIER_AUTHORITY NtSidAuthority = {SECURITY_NT_AUTHORITY};
    NTSTATUS    status;

    InitializeCriticalSection(&PerfDataCriticalSection);
 NtQuerySystemInformation = (long(__stdcall*)(DWORD,PVOID,DWORD,DWORD*))
        GetProcAddress(
        GetModuleHandle( "ntdll.dll" ),
        "NtQuerySystemInformation"
        );
    /*
     * Get number of processors in the system
     */
    status = NtQuerySystemInformation(SystemBasicInformation, &SystemBasicInfo, sizeof(SystemBasicInfo), NULL);
    if (status != NO_ERROR)
        return FALSE;

    /*
     * Create the SYSTEM Sid
     */
    AllocateAndInitializeSid(&NtSidAuthority, 1, SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0, &SystemUserSid);
    return TRUE;
}

void PerfDataUninitialize(void)
{
    DeleteCriticalSection(&PerfDataCriticalSection);

    if (SystemUserSid != NULL)
    {
        FreeSid(SystemUserSid);
        SystemUserSid = NULL;
    }
}

static void SidToUserName(PSID Sid, LPTSTR szBuffer, DWORD BufferSize)
{
    static TCHAR szDomainNameUnused[255]; 
    DWORD DomainNameLen = sizeof(szDomainNameUnused) / sizeof(szDomainNameUnused[0]);
    SID_NAME_USE Use;

    if (Sid != NULL)
        LookupAccountSid(NULL, Sid, szBuffer, &BufferSize, szDomainNameUnused, &DomainNameLen, &Use);
}
JNIEXPORT void JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataRefresh
  (JNIEnv *, jclass)
//void PerfDataRefresh(void)
{
    ULONG                            ulSize;
    NTSTATUS                          status;
    LPBYTE                            pBuffer;
    ULONG                            BufferSize;
    PSYSTEM_PROCESS_INFORMATION        pSPI;
    PPERFDATA                        pPDOld;
    ULONG                            Idx, Idx2;
    HANDLE                            hProcess;
    HANDLE                            hProcessToken;
    SYSTEM_PERFORMANCE_INFORMATION    SysPerfInfo;
    SYSTEM_TIMEOFDAY_INFORMATION      SysTimeInfo;
    SYSTEM_CACHE_INFORMATION        SysCacheInfo;
    LPBYTE                            SysHandleInfoData;
    PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION SysProcessorTimeInfo;
    double                            CurrentKernelTime;
    PSECURITY_DESCRIPTOR              ProcessSD;
    PSID                              ProcessUser;
    ULONG                             Buffer[64]; /* must be 4 bytes aligned! */

    /* Get new system time */
    status = NtQuerySystemInformation(SystemTimeOfDayInformation, &SysTimeInfo, sizeof(SysTimeInfo), 0);
    if (status != NO_ERROR)
        return;

    /* Get new CPU's idle time */
    status = NtQuerySystemInformation(SystemPerformanceInformation, &SysPerfInfo, sizeof(SysPerfInfo), NULL);
    if (status != NO_ERROR)
        return;

    /* Get system cache information */
     status = NtQuerySystemInformation(SystemCacheInformation, &SysCacheInfo, sizeof(SysCacheInfo), NULL);
        if (status != NO_ERROR)
            return;
    

    /* Get processor time information */
    SysProcessorTimeInfo = (PSYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)HeapAlloc(GetProcessHeap(), 0, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberOfProcessors);
    status = NtQuerySystemInformation(SystemProcessorPerformanceInformation, SysProcessorTimeInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * SystemBasicInfo.NumberOfProcessors, &ulSize);
    if (status != NO_ERROR)
        return;

    /* Get handle information
     * We don't know how much data there is so just keep
     * increasing the buffer size until the call succeeds
     */
    BufferSize = 0;
    do
    {
        BufferSize += 0x10000;
        SysHandleInfoData = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, BufferSize);

        status = NtQuerySystemInformation(SystemHandleInformation, SysHandleInfoData, BufferSize, &ulSize);

        if (status == STATUS_INFO_LENGTH_MISMATCH) {
            HeapFree(GetProcessHeap(), 0, SysHandleInfoData);
        }

    } while (status == STATUS_INFO_LENGTH_MISMATCH);

    /* Get process information
     * We don't know how much data there is so just keep
     * increasing the buffer size until the call succeeds
     */
    BufferSize = 0;
    do
    {
        BufferSize += 0x10000;
        pBuffer = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, BufferSize);

        status = NtQuerySystemInformation(SystemProcessInformation, pBuffer, BufferSize, &ulSize);

        if (status == STATUS_INFO_LENGTH_MISMATCH) {
            HeapFree(GetProcessHeap(), 0, pBuffer);
        }

    } while (status == STATUS_INFO_LENGTH_MISMATCH);

    EnterCriticalSection(&PerfDataCriticalSection);

    /*
     * Save system performance info
     */
    memcpy(&SystemPerfInfo, &SysPerfInfo, sizeof(SYSTEM_PERFORMANCE_INFORMATION));

    /*
     * Save system cache info
     */
    memcpy(&SystemCacheInfo, &SysCacheInfo, sizeof(SYSTEM_CACHE_INFORMATION));

    /*
     * Save system processor time info
     */
    if (SystemProcessorTimeInfo) {
        HeapFree(GetProcessHeap(), 0, SystemProcessorTimeInfo);
    }
    SystemProcessorTimeInfo = SysProcessorTimeInfo;

    /*
     * Save system handle info
     */
    memcpy(&SystemHandleInfo, SysHandleInfoData, sizeof(SYSTEM_HANDLE_INFORMATION));
    HeapFree(GetProcessHeap(), 0, SysHandleInfoData);

    for (CurrentKernelTime=0, Idx=0; Idx<(ULONG)SystemBasicInfo.NumberOfProcessors; Idx++) {
        CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].KernelTime);
        CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].DpcTime);
        CurrentKernelTime += Li2Double(SystemProcessorTimeInfo[Idx].InterruptTime);
    }

    /* If it's a first call - skip idle time calcs */
    if (liOldIdleTime.QuadPart != 0) {
        /*  CurrentValue = NewValue - OldValue */
        dbIdleTime = Li2Double(SysPerfInfo.IdleProcessTime) - Li2Double(liOldIdleTime);
        dbKernelTime = CurrentKernelTime - OldKernelTime;
        dbSystemTime = Li2Double(SysTimeInfo.CurrentTime) - Li2Double(liOldSystemTime);

        /*  CurrentCpuIdle = IdleTime / SystemTime */
        dbIdleTime = dbIdleTime / dbSystemTime;
        dbKernelTime = dbKernelTime / dbSystemTime;

        /*  CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors */
        dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
        dbKernelTime = 100.0 - dbKernelTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
    }

    /* Store new CPU's idle and system time */
    liOldIdleTime = SysPerfInfo.IdleProcessTime;
    liOldSystemTime = SysTimeInfo.CurrentTime;
    OldKernelTime = CurrentKernelTime;

    /* Determine the process count
     * We loop through the data we got from NtQuerySystemInformation
     * and count how many structures there are (until RelativeOffset is 0)
     */
    ProcessCountOld = ProcessCount;
    ProcessCount = 0;
    pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
    while (pSPI) {
        ProcessCount++;
        if (pSPI->NextEntryOffset == 0)
            break;
        pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->NextEntryOffset);
    }

    /* Now alloc a new PERFDATA array and fill in the data */
    if (pPerfDataOld) {
        HeapFree(GetProcessHeap(), 0, pPerfDataOld);
    }
    pPerfDataOld = pPerfData;
    pPerfData = (PPERFDATA)HeapAlloc(GetProcessHeap(), 0, sizeof(PERFDATA) * ProcessCount);
    pSPI = (PSYSTEM_PROCESS_INFORMATION)pBuffer;
    for (Idx=0; Idx< ProcessCount; Idx++) {
        /* Get the old perf data for this process (if any) */
        /* so that we can establish delta values */
        pPDOld = NULL;
        for (Idx2=0; Idx2< ProcessCountOld; Idx2++)
  {
            if (pPerfDataOld[Idx2].ProcessId == pSPI->UniqueProcessId) {
                pPDOld = &pPerfDataOld[Idx2];
                break;
            }
        }

        /* Clear out process perf data structure */
        memset(&pPerfData[Idx], 0, sizeof(PERFDATA));

        if (pSPI->ImageName.Buffer)
            wcscpy(pPerfData[Idx].ImageName, pSPI->ImageName.Buffer);
        else
  {
        //    LoadStringW(hInst, IDS_IDLE_PROCESS, pPerfData[Idx].ImageName,
        //                sizeof(pPerfData[Idx].ImageName) / sizeof(pPerfData[Idx].ImageName[0]));
  }

        pPerfData[Idx].ProcessId = pSPI->UniqueProcessId;

        if (pPDOld)    {
            double    CurTime = Li2Double(pSPI->KernelTime) + Li2Double(pSPI->UserTime);
            double    OldTime = Li2Double(pPDOld->KernelTime) + Li2Double(pPDOld->UserTime);
            double    CpuTime = (CurTime - OldTime) / dbSystemTime;
            CpuTime = CpuTime * 100.0 / (double)SystemBasicInfo.NumberOfProcessors; /* + 0.5; */
            pPerfData[Idx].CPUUsage = (ULONG)CpuTime;
        }
        pPerfData[Idx].CPUTime.QuadPart = pSPI->UserTime.QuadPart + pSPI->KernelTime.QuadPart;
        pPerfData[Idx].WorkingSetSizeBytes = pSPI->WorkingSetSize;
        pPerfData[Idx].PeakWorkingSetSizeBytes = pSPI->PeakWorkingSetSize;
        if (pPDOld)
            pPerfData[Idx].WorkingSetSizeDelta = labs((LONG)pSPI->WorkingSetSize - (LONG)pPDOld->WorkingSetSizeBytes);
        else
            pPerfData[Idx].WorkingSetSizeDelta = 0;
        pPerfData[Idx].PageFaultCount = pSPI->PageFaultCount;
        if (pPDOld)
            pPerfData[Idx].PageFaultCountDelta = labs((LONG)pSPI->PageFaultCount - (LONG)pPDOld->PageFaultCount);
        else
            pPerfData[Idx].PageFaultCountDelta = 0;
        pPerfData[Idx].VirtualMemorySizeBytes = pSPI->VirtualSize;
        pPerfData[Idx].PagedPoolUsagePages = pSPI->QuotaPeakPagedPoolUsage;
        pPerfData[Idx].NonPagedPoolUsagePages = pSPI->QuotaPeakNonPagedPoolUsage;
        pPerfData[Idx].BasePriority = pSPI->BasePriority;
        pPerfData[Idx].HandleCount = pSPI->HandleCount;
        pPerfData[Idx].ThreadCount = pSPI->NumberOfThreads;
        pPerfData[Idx].SessionId = pSPI->SessionId;
        pPerfData[Idx].UserName[0] = _T('/0');
        pPerfData[Idx].USERObjectCount = 0;
        pPerfData[Idx].GDIObjectCount = 0;
        ProcessUser = SystemUserSid;
        ProcessSD = NULL;

        if (pSPI->UniqueProcessId != NULL) {
            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | READ_CONTROL, FALSE, PtrToUlong(pSPI->UniqueProcessId));
            if (hProcess) {
                /* don't query the information of the system process. It's possible but
                   returns Administrators as the owner of the process instead of SYSTEM */
                if (pSPI->UniqueProcessId != 0x4)
                {
                    if (OpenProcessToken(hProcess, TOKEN_QUERY, &hProcessToken))
                    {
                        DWORD RetLen = 0;
                        BOOL Ret;

                        Ret = GetTokenInformation(hProcessToken, TokenUser, (LPVOID)Buffer, sizeof(Buffer), &RetLen);
                        CloseHandle(hProcessToken);

                        if (Ret)
                            ProcessUser = ((PTOKEN_USER)Buffer)->User.Sid;
                        else
                            goto ReadProcOwner;
                    }
                    else
                    {
ReadProcOwner:
     ;
                     //   GetSecurityInfo(hProcess, SE_KERNEL_OBJECT, OWNER_SECURITY_INFORMATION, &ProcessUser, NULL, NULL, NULL, &ProcessSD);
                    }

                    pPerfData[Idx].USERObjectCount = GetGuiResources(hProcess, GR_USEROBJECTS);
                    pPerfData[Idx].GDIObjectCount = GetGuiResources(hProcess, GR_GDIOBJECTS);
                }

                GetProcessIoCounters(hProcess, &pPerfData[Idx].IOCounters);
                CloseHandle(hProcess);
            } else {
                goto ClearInfo;
            }
        } else {
ClearInfo:
            /* clear information we were unable to fetch */
            ZeroMemory(&pPerfData[Idx].IOCounters, sizeof(IO_COUNTERS));
        }

        //SidToUserName(ProcessUser, pPerfData[Idx].UserName, sizeof(pPerfData[0].UserName) / sizeof(pPerfData[0].UserName[0]));

        if (ProcessSD != NULL)
        {
            LocalFree((HLOCAL)ProcessSD);
        }

        pPerfData[Idx].UserTime.QuadPart = pSPI->UserTime.QuadPart;
        pPerfData[Idx].KernelTime.QuadPart = pSPI->KernelTime.QuadPart;
        pSPI = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pSPI + pSPI->NextEntryOffset);
    }
    HeapFree(GetProcessHeap(), 0, pBuffer);
    LeaveCriticalSection(&PerfDataCriticalSection);
}
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetProcessCount
  (JNIEnv *, jclass)
//ULONG PerfDataGetProcessCount(void)//获得进程数目
{
    return ProcessCount;
}
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetProcessorUsage
  (JNIEnv *, jclass)
//ULONG PerfDataGetProcessorUsage(void)//获得CPU使用率
{
    return (ULONG)dbIdleTime;
}

ULONG PerfDataGetProcessorSystemUsage(void)
{
    return (ULONG)dbKernelTime;
}

BOOL PerfDataGetImageName(ULONG Index, LPTSTR lpImageName, int nMaxCount)
{
    BOOL    bSuccessful;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount) {
        #ifdef _UNICODE
            wcsncpy(lpImageName, pPerfData[Index].ImageName, nMaxCount);
        #else
            WideCharToMultiByte(CP_ACP, 0, pPerfData[Index].ImageName, -1, lpImageName, nMaxCount, NULL, NULL);
        #endif

        bSuccessful = TRUE;
    } else {
        bSuccessful = FALSE;
    }
    LeaveCriticalSection(&PerfDataCriticalSection);
    return bSuccessful;
}

int PerfGetIndexByProcessId(DWORD dwProcessId)
{
    int Index, FoundIndex = -1;

    EnterCriticalSection(&PerfDataCriticalSection);

    for (Index = 0; Index < (int)ProcessCount; Index++)
    {
        if ((DWORD)pPerfData[Index].ProcessId == dwProcessId)
        {
            FoundIndex = Index;
            break;
        }
    }

    LeaveCriticalSection(&PerfDataCriticalSection);

    return FoundIndex;
}

ULONG PerfDataGetProcessId(ULONG Index)
{
    ULONG    ProcessId;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        ProcessId = (ULONG)pPerfData[Index].ProcessId;
    else
        ProcessId = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return ProcessId;
}

BOOL PerfDataGetUserName(ULONG Index, LPTSTR lpUserName, int nMaxCount)
{
    BOOL    bSuccessful;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount) {
        #ifdef _UNICODE
            wcsncpy(lpUserName, pPerfData[Index].UserName, nMaxCount);
        #else
            WideCharToMultiByte(CP_ACP, 0, pPerfData[Index].UserName, -1, lpUserName, nMaxCount, NULL, NULL);
        #endif

        bSuccessful = TRUE;
    } else {
        bSuccessful = FALSE;
    }

    LeaveCriticalSection(&PerfDataCriticalSection);

    return bSuccessful;
}

ULONG PerfDataGetSessionId(ULONG Index)
{
    ULONG    SessionId;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        SessionId = pPerfData[Index].SessionId;
    else
        SessionId = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return SessionId;
}

ULONG PerfDataGetCPUUsage(ULONG Index)
{
    ULONG    CpuUsage;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        CpuUsage = pPerfData[Index].CPUUsage;
    else
        CpuUsage = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return CpuUsage;
}

LARGE_INTEGER PerfDataGetCPUTime(ULONG Index)
{
    LARGE_INTEGER    CpuTime = {{0,0}};

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        CpuTime = pPerfData[Index].CPUTime;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return CpuTime;
}

ULONG PerfDataGetWorkingSetSizeBytes(ULONG Index)
{
    ULONG    WorkingSetSizeBytes;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        WorkingSetSizeBytes = pPerfData[Index].WorkingSetSizeBytes;
    else
        WorkingSetSizeBytes = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return WorkingSetSizeBytes;
}

ULONG PerfDataGetPeakWorkingSetSizeBytes(ULONG Index)
{
    ULONG    PeakWorkingSetSizeBytes;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        PeakWorkingSetSizeBytes = pPerfData[Index].PeakWorkingSetSizeBytes;
    else
        PeakWorkingSetSizeBytes = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return PeakWorkingSetSizeBytes;
}

ULONG PerfDataGetWorkingSetSizeDelta(ULONG Index)
{
    ULONG    WorkingSetSizeDelta;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        WorkingSetSizeDelta = pPerfData[Index].WorkingSetSizeDelta;
    else
        WorkingSetSizeDelta = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return WorkingSetSizeDelta;
}

ULONG PerfDataGetPageFaultCount(ULONG Index)
{
    ULONG    PageFaultCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        PageFaultCount = pPerfData[Index].PageFaultCount;
    else
        PageFaultCount = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return PageFaultCount;
}

ULONG PerfDataGetPageFaultCountDelta(ULONG Index)
{
    ULONG    PageFaultCountDelta;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        PageFaultCountDelta = pPerfData[Index].PageFaultCountDelta;
    else
        PageFaultCountDelta = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return PageFaultCountDelta;
}

ULONG PerfDataGetVirtualMemorySizeBytes(ULONG Index)
{
    ULONG    VirtualMemorySizeBytes;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        VirtualMemorySizeBytes = pPerfData[Index].VirtualMemorySizeBytes;
    else
        VirtualMemorySizeBytes = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return VirtualMemorySizeBytes;
}

ULONG PerfDataGetPagedPoolUsagePages(ULONG Index)
{
    ULONG    PagedPoolUsage;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        PagedPoolUsage = pPerfData[Index].PagedPoolUsagePages;
    else
        PagedPoolUsage = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return PagedPoolUsage;
}

ULONG PerfDataGetNonPagedPoolUsagePages(ULONG Index)
{
    ULONG    NonPagedPoolUsage;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        NonPagedPoolUsage = pPerfData[Index].NonPagedPoolUsagePages;
    else
        NonPagedPoolUsage = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return NonPagedPoolUsage;
}

ULONG PerfDataGetBasePriority(ULONG Index)
{
    ULONG    BasePriority;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        BasePriority = pPerfData[Index].BasePriority;
    else
        BasePriority = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return BasePriority;
}
//进程的句柄数
ULONG PerfDataGetHandleCount(ULONG Index)
{
    ULONG    HandleCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        HandleCount = pPerfData[Index].HandleCount;
    else
        HandleCount = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return HandleCount;
}
//  进程的线程数
ULONG PerfDataGetThreadCount(ULONG Index)
{
    ULONG    ThreadCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        ThreadCount = pPerfData[Index].ThreadCount;
    else
        ThreadCount = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return ThreadCount;
}

ULONG PerfDataGetUSERObjectCount(ULONG Index)
{
    ULONG    USERObjectCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        USERObjectCount = pPerfData[Index].USERObjectCount;
    else
        USERObjectCount = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return USERObjectCount;
}

ULONG PerfDataGetGDIObjectCount(ULONG Index)
{
    ULONG    GDIObjectCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
        GDIObjectCount = pPerfData[Index].GDIObjectCount;
    else
        GDIObjectCount = 0;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return GDIObjectCount;
}

BOOL PerfDataGetIOCounters(ULONG Index, PIO_COUNTERS pIoCounters)
{
    BOOL    bSuccessful;

    EnterCriticalSection(&PerfDataCriticalSection);

    if (Index < ProcessCount)
    {
        memcpy(pIoCounters, &pPerfData[Index].IOCounters, sizeof(IO_COUNTERS));
        bSuccessful = TRUE;
    }
    else
        bSuccessful = FALSE;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return bSuccessful;
}
//提交总量
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetCommitChargeTotalK
  (JNIEnv *, jclass)
//ULONG PerfDataGetCommitChargeTotalK(void)
{
    ULONG    Total;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Total = SystemPerfInfo.CommittedPages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Total = Total * (PageSize / 1024);

    return Total;
}
//提交限制
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetCommitChargeLimitK
  (JNIEnv *, jclass)
//ULONG PerfDataGetCommitChargeLimitK(void)
{
    ULONG    Limit;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Limit = SystemPerfInfo.CommitLimit;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Limit = Limit * (PageSize / 1024);

    return Limit;
}
//提交峰值
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetCommitChargePeakK
  (JNIEnv *, jclass)
//ULONG PerfDataGetCommitChargePeakK(void)
{
    ULONG    Peak;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Peak = SystemPerfInfo.PeakCommitment;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Peak = Peak * (PageSize / 1024);

    return Peak;
}
//核心内存总量
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetKernelMemoryTotalK
  (JNIEnv *, jclass)
//ULONG PerfDataGetKernelMemoryTotalK(void)
{
    ULONG    Total;
    ULONG    Paged;
    ULONG    NonPaged;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Paged = SystemPerfInfo.PagedPoolPages;
    NonPaged = SystemPerfInfo.NonPagedPoolPages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Paged = Paged * (PageSize / 1024);
    NonPaged = NonPaged * (PageSize / 1024);

    Total = Paged + NonPaged;

    return Total;
}
//内核分页内存
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetKernelMemoryPagedK
  (JNIEnv *, jclass)
//ULONG PerfDataGetKernelMemoryPagedK(void)
{
    ULONG    Paged;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Paged = SystemPerfInfo.PagedPoolPages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Paged = Paged * (PageSize / 1024);

    return Paged;
}
//内核非分页内存
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetKernelMemoryNonPagedK
  (JNIEnv *, jclass)
//ULONG PerfDataGetKernelMemoryNonPagedK(void)
{
    ULONG    NonPaged;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    NonPaged = SystemPerfInfo.NonPagedPoolPages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    NonPaged = NonPaged * (PageSize / 1024);

    return NonPaged;
}
//总物理内存
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetPhysicalMemoryTotalK
  (JNIEnv *, jclass)
//ULONG PerfDataGetPhysicalMemoryTotalK(void)
{
    ULONG    Total;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Total = SystemBasicInfo.NumberOfPhysicalPages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Total = Total * (PageSize / 1024);

    return Total;
}
//可用物理内存
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetPhysicalMemoryAvailableK
  (JNIEnv *, jclass)
//ULONG PerfDataGetPhysicalMemoryAvailableK(void)
{
    ULONG    Available;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    Available = SystemPerfInfo.AvailablePages;
    PageSize = SystemBasicInfo.PageSize;

    LeaveCriticalSection(&PerfDataCriticalSection);

    Available = Available * (PageSize / 1024);

    return Available;
}
//物理内存系统缓存
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetPhysicalMemorySystemCacheK
  (JNIEnv *, jclass)
//ULONG PerfDataGetPhysicalMemorySystemCacheK(void)
{
   ULONG    SystemCache;
    ULONG    PageSize;

    EnterCriticalSection(&PerfDataCriticalSection);

    PageSize = SystemBasicInfo.PageSize;
   
    SystemCache = SystemCacheInfo.unused[0] * PageSize;
    LeaveCriticalSection(&PerfDataCriticalSection);

    return SystemCache /1024;
}
//句柄数
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetSystemHandleCount
  (JNIEnv *, jclass)
//ULONG PerfDataGetSystemHandleCount(void)
{
    ULONG    HandleCount;

    EnterCriticalSection(&PerfDataCriticalSection);

    HandleCount = SystemHandleInfo.Count;

    LeaveCriticalSection(&PerfDataCriticalSection);

    return HandleCount;
}
//线程数
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_SystemLoad_PerfDataGetTotalThreadCount
  (JNIEnv *, jclass)
//ULONG PerfDataGetTotalThreadCount(void)
{
    ULONG    ThreadCount = 0;
    ULONG    i;

    EnterCriticalSection(&PerfDataCriticalSection);

    for (i=0; i< ProcessCount; i++)
    {
        ThreadCount += pPerfData[i].ThreadCount;
    }

    LeaveCriticalSection(&PerfDataCriticalSection);

    return ThreadCount;
}

/*
int main()
{
  PerfDataInitialize();
  while (1)
  {
   //...get data
   //for eg.
   PerfDataRefresh();
   
  
   cout <<"CPU使用率: "<< PerfDataGetProcessorUsage()<< endl;
   //cout <<"信息: "<< PerfDataGetProcessorSystemUsage()<< endl;
   cout <<"认可用量(K),总数:"<< PerfDataGetCommitChargeTotalK()<< endl;
   cout <<"认可用量(K),限制:"<< PerfDataGetCommitChargeLimitK()<< endl;
   cout <<"认可用量(K),峰值:"<< PerfDataGetCommitChargePeakK()<< endl;
   cout <<"核心内存(K),总数:"<< PerfDataGetKernelMemoryTotalK()<< endl;
   cout <<"核心内存(K),分页数:"<< PerfDataGetKernelMemoryPagedK()<< endl;
   cout <<"核心内存(K),未分页:"<< PerfDataGetKernelMemoryNonPagedK()<< endl;
   cout <<"物理内存(K),总数:"<< PerfDataGetPhysicalMemoryTotalK()<< endl;
   cout <<"物理内存(K),可用数:"<< PerfDataGetPhysicalMemoryAvailableK()<< endl;
   cout <<"物理内存(K),系统缓存:"<< PerfDataGetPhysicalMemorySystemCacheK()<< endl;
   cout <<"总数,句柄数:"<< PerfDataGetSystemHandleCount()<< endl;
   cout <<"总数,线程数:"<< PerfDataGetTotalThreadCount()<< endl;
    cout <<"总数,进程数目: "<< PerfDataGetProcessCount()<< endl;
   cout <<"CPU: PID:0 ,Usage: "<< PerfDataGetCPUUsage(PerfGetIndexByProcessId(0))<< endl;
   cout <<"CPU: PID:0 ,Usage: "<< PerfDataGetCPUUsage(PerfGetIndexByProcessId(4))<< endl;
   Sleep(1000);
  }
  return 0;
}

*/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值