NtQuerySystemInformation 例子:bool CanGetCPUUsage() { //os version information structure OSVERSIONINFO OsVersionInfo; OsVersionInfo.dwOSVersionInfoSize= sizeof(OSVERSIONINFO); //get os version GetVersionEx(&OsVersionInfo); //only run in win2000/nt OS if(OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) { // MessageBox("Only run in Win2K/NT,\\nYour OS is not Win2000/NT!","prompt:"); return false; }
//Get NtQuerySystemInformation Address from ntdll.dll NtQuerySystemInformation =(NTQSI)GetProcAddress( GetModuleHandle("ntdll.dll"), "NtQuerySystemInformation" );
if (!NtQuerySystemInformation) { // MessageBox("no this function"); return false; }
// get num of processors status = NtQuerySystemInformation(0,&SysBaseInfo,sizeof(SysBaseInfo),NULL); if (status != NO_ERROR) { // MessageBox("failed to query basic info(num of processors)"); return false; } return true; } double GetCPUUsage() { // get system time status = NtQuerySystemInformation(3,&SysTimeInfo,sizeof(SysTimeInfo),0); if (status!=NO_ERROR) { // MessageBox("failed to get system time!"); return ret_error; }
// get cpu idle time status = NtQuerySystemInformation(2,&SysPerfInfo,sizeof(SysPerfInfo),NULL); if (status != NO_ERROR) { // MessageBox("failed to get cpu time"); return ret_error; } if (liOldIdleTime.QuadPart != 0) { // new cpu Time = NewTime - OldTime dbCpuIdleTime =LI64ToDouble(SysPerfInfo.liIdleTime)-LI64ToDouble(liOldIdleTime); dbSysTime =LI64ToDouble(SysTimeInfo.liKeSystemTime)-LI64ToDouble(liOldSysTime);
//get cpu usage unCpuUsage=100 - 100*(dbCpuIdleTime/dbSysTime)/(double)SysBaseInfo.bKeNumberProcessors+0.5; }
// store new cpu idle and system time liOldIdleTime = SysPerfInfo.liIdleTime; liOldSysTime = SysTimeInfo.liKeSystemTime;
例子:bool CanGetCPUUsage()
{
//os version information structure
OSVERSIONINFO OsVersionInfo;
OsVersionInfo.dwOSVersionInfoSize= sizeof(OSVERSIONINFO);
//get os version
GetVersionEx(&OsVersionInfo);
//only run in win2000/nt OS
if(OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
{
// MessageBox("Only run in Win2K/NT,\\nYour OS is not Win2000/NT!","prompt:");
return false;
}
//Get NtQuerySystemInformation Address from ntdll.dll
NtQuerySystemInformation =(NTQSI)GetProcAddress(
GetModuleHandle("ntdll.dll"),
"NtQuerySystemInformation" );
if (!NtQuerySystemInformation)
{
// MessageBox("no this function");
return false;
}
// get num of processors
status = NtQuerySystemInformation(0,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
if (status != NO_ERROR)
{
// MessageBox("failed to query basic info(num of processors)");
return false;
}
return true;
}
double GetCPUUsage()
{
// get system time
status = NtQuerySystemInformation(3,&SysTimeInfo,sizeof(SysTimeInfo),0);
if (status!=NO_ERROR)
{
// MessageBox("failed to get system time!");
return ret_error;
}
// get cpu idle time
status = NtQuerySystemInformation(2,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
if (status != NO_ERROR)
{
// MessageBox("failed to get cpu time");
return ret_error;
}
if (liOldIdleTime.QuadPart != 0)
{
// new cpu Time = NewTime - OldTime
dbCpuIdleTime =LI64ToDouble(SysPerfInfo.liIdleTime)-LI64ToDouble(liOldIdleTime);
dbSysTime =LI64ToDouble(SysTimeInfo.liKeSystemTime)-LI64ToDouble(liOldSysTime);
//get cpu usage
unCpuUsage=100 - 100*(dbCpuIdleTime/dbSysTime)/(double)SysBaseInfo.bKeNumberProcessors+0.5;
}
// store new cpu idle and system time
liOldIdleTime = SysPerfInfo.liIdleTime;
liOldSysTime = SysTimeInfo.liKeSystemTime;
return unCpuUsage;
}
---------------------------------------------------------------
GetProcessUse(PROCESS_INFORMATION proc,_JOBOBJECT_BASIC_ACCOUNTING_INFORMAT Info)
{
Handle hJob=CreateJobOjbect(NULL,NULL);
AssignProcessToJobObject(hjob,proc.hProcess,NULL,0,0);
QueryInformationJobOjbect(hJob,Info)
closeHandle(hjob);
}
typedef struct _JOBOBJECT_BASIC_ACCOUNTING_INFORMATION
{
LARGE_INTEGER TotalUserTime;
LARGE_INTEGER TotalKernelTime;
LARGE_INTEGER ThisPeriodTotalUerTime
LARGE_INTEGER thisPeriodTotalKernelTime;
DWORD TotalPageFaultCount
DWORD TotalProcess
DWORD ActiveProcesses
DWORD TotalTerminatedProcesses;
}_JOBOBJECT_BASIC_ACCOUNTING_INFORMATION,*_JOBOBJECT_BASIC_ACCOUNTING_INFORMATION