AUFS 发表于 2009-11-5 02:28:57

如何获取某进程CPU占用率?(有VC源码)

本帖最后由 AUFS 于 2009-11-22 03:31 编辑

这里是搜索过来的VC源码,不知道可不可以参考修改回AU3用?

typedef struct _THREAD_INFO
{
LARGE_INTEGER CreateTime;
DWORD dwUnknown1;
DWORD dwStartAddress;
DWORD StartEIP;
DWORD dwOwnerPID;
DWORD dwThreadId;
DWORD dwCurrentPriority;
DWORD dwBasePriority;
DWORD dwContextSwitches;
DWORD Unknown;
DWORD WaitReason;

}THREADINFO, *PTHREADINFO;

typedef struct _UNICODE_STRING
{
USHORT Length;
USHORT MaxLength;
PWSTR Buffer;
} UNICODE_STRING;

typedef struct _PROCESS_INFO
{
DWORD dwOffset;
DWORD dwThreadsCount;
DWORD dwUnused1;
LARGE_INTEGER CreateTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER KernelTime;
UNICODE_STRING ProcessName;

DWORD dwBasePriority;
DWORD dwProcessID;
DWORD dwParentProcessId;
DWORD dwHandleCount;
DWORD dwUnused3;

DWORD dwVirtualBytesPeak;
DWORD dwVirtualBytes;
ULONG dwPageFaults;
DWORD dwWorkingSetPeak;
DWORD dwWorkingSet;
DWORD dwQuotaPeakPagedPoolUsage;
DWORD dwQuotaPagedPoolUsage;
DWORD dwQuotaPeakNonPagedPoolUsage;
DWORD dwQuotaNonPagedPoolUsage;
DWORD dwPageFileUsage;
DWORD dwPageFileUsagePeak;

DWORD dCommitCharge;
THREADINFO ThreadSysInfo;

} PROCESSINFO, *PPROCESSINFO;

函数定义:

int Button2Click(int id);//参数是进程ID

函数实体:
int Button2Click(int id)
{
int cpuusage;
PVOID pProcInfo = NULL;
DWORD dwInfoSize = 0x20000;
PPROCESSINFO pProcessInfo;
DWORD dwWorkingSet;
long ( __stdcall *NtQuerySystemInformation )( DWORD, PVOID, DWORD, DWORD );


static __int64 LastTotalProcessCPUUsage = 0;
static __int64 LastCurrentProcessCPUUsage = 0;

int CurrentDelta;
int TotalDelta;

__int64 TotalProcessCPUUsage = 0;
__int64 CurrentProcessCPUUsage = 0;

/////////////////////////////////

pProcInfo = (PVOID)(new byte);

NtQuerySystemInformation = (long(__stdcall*)(DWORD,PVOID,DWORD,DWORD))
GetProcAddress( GetModuleHandle( "ntdll.dll" ),"NtQuerySystemInformation" );

NtQuerySystemInformation(5,pProcInfo,dwInfoSize,0);

pProcessInfo = (PPROCESSINFO)pProcInfo;

do
{
TotalProcessCPUUsage += (__int64)pProcessInfo->KernelTime.QuadPart + (__int64)pProcessInfo->UserTime.QuadPart;

if(pProcessInfo->dwProcessID == id)
{
dwWorkingSet = pProcessInfo->dwWorkingSet;
CurrentProcessCPUUsage += (__int64)pProcessInfo->KernelTime.QuadPart + (__int64)pProcessInfo->UserTime.QuadPart;
}

/////////
if(pProcessInfo->dwOffset == 0)
{
break;
}

pProcessInfo = (PPROCESSINFO)((byte*)pProcessInfo + pProcessInfo->dwOffset);
}
while(true);

TotalDelta = TotalProcessCPUUsage - LastTotalProcessCPUUsage;
CurrentDelta = CurrentProcessCPUUsage - LastCurrentProcessCPUUsage;

if(TotalDelta != 0)
cpuusage = 100 * CurrentDelta / TotalDelta;

//this->Caption = "CPU = " + IntToStr(100 * CurrentDelta / TotalDelta) +
//"Memory = "+ IntToStr(dwWorkingSet / 1024) " K";

LastTotalProcessCPUUsage = TotalProcessCPUUsage;
LastCurrentProcessCPUUsage = CurrentProcessCPUUsage;

delete[] pProcInfo;
return cpuusage;

}


控制台调用:
int main(void)
{
while(true)
{
int s = Button2Click(0);//在此把进程ID作为参数传入
printf("%d\n",s);
Sleep(1000);
}
return 0;
}

AUFS 发表于 2009-11-6 00:17:00

这个应该对大家有点作用。。。有人在论坛里问过但没有好的解决办法。。。现在收藏到VC的源码。。望高手来转换一下了.....造福大家..

kodin 发表于 2009-11-6 02:14:56

源码区好有这类源码。

AUFS 发表于 2009-11-7 10:51:33

看过了,只是CPU的整体占用率不是指定的某个程序占用率。

AUFS 发表于 2009-11-16 01:05:20

看来是暂时还没有解决的办法

karlpopper 发表于 2009-11-16 04:58:08

WMI能实现吗

水木子 发表于 2009-11-16 08:39:39

本帖最后由 水木子 于 2009-11-16 08:40 编辑

这是天空老大skyfree写的,看看吧!
检测CUP和内存的使用率。#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=D:\Personal\Desktop\CPU_RAM.kxf
$FormName="System"
$Form1 = GUICreate($FormName, 306, 70, 193, 125, BitOR($WS_MINIMIZEBOX,$WS_SYSMENU,$WS_POPUP,$WS_GROUP,$WS_CLIPSIBLINGS))
$Label1 = GUICtrlCreateLabel("CPU", 8, 8, 36, 17)
GUICtrlSetFont(-1, 11, 400, 0, "宋体")
$Label2 = GUICtrlCreateLabel("RAM", 8, 40, 36, 17)
GUICtrlSetFont(-1, 11, 400, 0, "宋体")
$Progress1 = GUICtrlCreateProgress(56, 8, 193, 17)
$Progress2 = GUICtrlCreateProgress(56, 40, 193, 17)
$Label3 = GUICtrlCreateLabel("", 264, 8, 36, 20)
GUICtrlSetFont(-1, 11, 400, 0, "宋体")
$Label4 = GUICtrlCreateLabel("", 264, 40, 36, 17)
GUICtrlSetFont(-1, 11, 400, 0, "宋体")
$pos = WinGetPos($FormName)
WinMove($FormName, "", @DesktopWidth / 2 - $pos / 2, @DesktopHeight / 2 - $pos / 2)
WinSetOnTop($FormName,"",1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

AdlibEnable("CpuRamRate",500)

While 1
      
WEnd

Func CpuRamRate()
      Local $rCPU,$rRAM
      $rCPU=CurrentCPURate()
      $rRAM=CurrentRAMRate()
      GUICtrlSetData($Progress1,$rCPU)
      GUICtrlSetData($Label3,$rCPU&"%")
      GUICtrlSetData($Progress2,$rRAM)
      GUICtrlSetData($Label4,$rRAM&"%")
EndFunc

Func CurrentCPURate()
      $wbemServices = ObjGet("winmgmts:\\.\root\cimv2")
      $wbemObjectSet= $wbemServices.ExecQuery("Select * from Win32_Processor")
      For $wbemObject In $wbemObjectSet
                $dd=$wbemObject.LoadPercentage
      Next
      Return $dd
EndFunc

Func CurrentRAMRate()
      Local $Array
      $Array=MemGetStats()
      Return $Array
EndFunc

AUFS 发表于 2009-11-22 03:30:55

需然没有达到想要的哪种效果。。。变一种方式来用吧~~谢谢楼上的大哥。。热心帮助

lgs0540 发表于 2011-11-1 16:51:54

学习ing!谢谢
页: [1]
查看完整版本: 如何获取某进程CPU占用率?(有VC源码)