vc获取当前进程CPU使用率

double GetCPUUserRate()
{
	HANDLE hProcess=::GetCurrentProcess();
	static DWORD s_dwTickCountOld = 0;                
	static LARGE_INTEGER s_lgProcessTimeOld={0};      
	static DWORD s_dwProcessorCoreNum = 0;   
	if (!s_dwProcessorCoreNum)
	{
		SYSTEM_INFO sysInfo = {0};
		GetSystemInfo(&sysInfo);
		s_dwProcessorCoreNum = sysInfo.dwNumberOfProcessors;
	}
	double dbProcCpuPercent = 0;
	BOOL bRetCode = FALSE;

	FILETIME CreateTime, ExitTime, KernelTime,UserTime;
	LARGE_INTEGER lgKernelTime;
	LARGE_INTEGER lgUserTime;
	LARGE_INTEGER lgCurTime;
	
	bRetCode = GetProcessTimes(hProcess, &CreateTime, &ExitTime, &KernelTime, &UserTime);
	if (bRetCode)
	{
		lgKernelTime.HighPart = KernelTime.dwHighDateTime;
		lgKernelTime.LowPart = KernelTime.dwLowDateTime;
		lgUserTime.HighPart = UserTime.dwHighDateTime;
		lgUserTime.LowPart = UserTime.dwLowDateTime;
		lgCurTime.QuadPart=(lgKernelTime.QuadPart+lgUserTime.QuadPart);
		if (s_lgProcessTimeOld.QuadPart)
		{
			DWORD dwElepsedTime=::GetTickCount()-s_dwTickCountOld;
			dbProcCpuPercent =(double)(((double)((lgCurTime.QuadPart - s_lgProcessTimeOld.QuadPart) * 100)) / dwElepsedTime)/10000;
			dbProcCpuPercent = dbProcCpuPercent / s_dwProcessorCoreNum;
		}
		s_lgProcessTimeOld = lgCurTime;
		s_dwTickCountOld=::GetTickCount();
	}

	return dbProcCpuPercent;
}



   

posted @ 2016-07-29 12:12  eATM  阅读(1440)  评论(0编辑  收藏  举报