VB怎么监测指定进程的CPU占用率?
看了很多网页,都没完全的答案,各位有会的吗?菜鸟不懂呀. --------------------编程问答-------------------- http:\\hi.csdn.net\Veron_04 --------------------编程问答-------------------- 楼上的,你那连接啥也看不到 --------------------编程问答-------------------- bs在技术贴灌水的顶顶……
If 本楼给分 Then--------------------编程问答-------------------- 楼猪我就是没有小JJ,你想什么样泥~~~~~~~~
楼主木有小JJ
End If
--------------------编程问答-------------------- 楼上的,如果我木有JJ,那你也一样,因为我和你同类 --------------------编程问答-------------------- --------------------编程问答-------------------- 有谁知道的? 请回答了,谢谢. --------------------编程问答--------------------
Public Function GetCPUTemp() As Double--------------------编程问答-------------------- 收藏了,MARK
Dim i As Integer
Dim mCPU As Variant
Dim u As Variant
Dim s As String
Set mCPU = GetObject("WINMGMTS:{impersonationLevel=impersonate}!root\wmi").ExecQuery("SELECT CurrentTemperature From MSAcpi_ThermalZoneTemperature")
For Each u In mCPU
s = s & u.CurrentTemperature
Next
Set mCPU = Nothing
GetCPUTemp = (s - 2732) / 10
End Function
If 本楼给分 Then--------------------编程问答-------------------- Veron_04
楼主木有小JJ
End If
有详细点的代码吗? 怎么是报错误了? --------------------编程问答--------------------
他那是获取CPU温度的,不是CPU占用率。 --------------------编程问答-------------------- 获取总的CPU占用率,网上有现成的VB例子,就不说了。
如何实时获取系统每个进程占用的CPU使用率(这是C++的,有谁把翻译成VB6就好了):
http://tech.ddvip.com/2006-04/11439183602461.html --------------------编程问答-------------------- chenjl1031
哎....怎么是C++的,VB就没办法了吗? --------------------编程问答--------------------
每个进程的时间和CPU总时间可以获得,然后计算一下比例.如果动态的话在Timer中执行.
--------------------编程问答--------------------
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;
}
补充:VB , API