找回密码
 加入
搜索
查看: 7258|回复: 8

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

[复制链接]
发表于 2009-11-5 02:28:57 | 显示全部楼层 |阅读模式
本帖最后由 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[6];
LARGE_INTEGER CreateTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER KernelTime;
UNICODE_STRING ProcessName;

DWORD dwBasePriority;
DWORD dwProcessID;
DWORD dwParentProcessId;
DWORD dwHandleCount;
DWORD dwUnused3[2];

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[1];

} 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[dwInfoSize]);

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;
}

评分

参与人数 1金钱 +20 收起 理由
kn007 + 20 感谢主动将修改帖子分类为[已解决],请继续 ...

查看全部评分

 楼主| 发表于 2009-11-6 00:17:00 | 显示全部楼层
这个应该对大家有点作用。。。有人在论坛里问过但没有好的解决办法。。。现在收藏到VC的源码。。望高手来转换一下了.....造福大家..
发表于 2009-11-6 02:14:56 | 显示全部楼层
源码区好有这类源码。
 楼主| 发表于 2009-11-7 10:51:33 | 显示全部楼层
看过了,只是CPU的整体占用率不是指定的某个程序占用率。
 楼主| 发表于 2009-11-16 01:05:20 | 显示全部楼层
看来是暂时还没有解决的办法
发表于 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] / 2, @DesktopHeight / 2 - $pos[3] / 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[7]
        $Array=MemGetStats()
        Return $Array[0]
EndFunc

评分

参与人数 1金钱 +30 贡献 +10 收起 理由
kn007 + 30 + 10 我很赞同

查看全部评分

 楼主| 发表于 2009-11-22 03:30:55 | 显示全部楼层
需然没有达到想要的哪种效果。。。变一种方式来用吧~~谢谢楼上的大哥。。热心帮助
发表于 2011-11-1 16:51:54 | 显示全部楼层
学习ing!谢谢
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-6 03:11 , Processed in 0.078558 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表