命令行参数:此exe名 应用程序名(如:360se.exe)
//#include
#include
#include
#include
#include
#include
#include
#define MY_PROCESS_ERROR(Condition) do{ if(!(Condition)) goto Exit0; } while (false)
static DWORD g_sdwTickCountOld =0; // 上一次的tick计数
static LARGE_INTEGERg_slgProcessTimeOld; // 保存进程上一次的时间占用
static DWORD g_sdwProcessorCoreNum =0; // 处理器核心数
static HANDLE g_shExitEvent =NULL; // 线程退出控制
typedef struct _TARGET_PROCESS
{
DWORD dwProcessId; // 进程ID
}TARGET_PROCESS, *PTARGET_PROCESS;
HRESULT FindFirstProcessIdByName(const TCHAR* cpszExeFileName,DWORD &dwPID)
{
HRESULT hr =E_FAIL;
PROCESSENTRY32 pe = { 0 };
HANDLEhSnapshot = NULL;
if (NULL== cpszExeFileName)
{
hr = HRESULT_FROM_WIN32(ERROR_BAD_ARGUMENTS);
goto Exit0;
}
pe.dwSize= sizeof(PROCESSENTRY32);
hSnapshot =CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(INVALID_HANDLE_VALUE == hSnapshot)
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto Exit0;
}
if (FALSE== Process32First(hSnapshot, &pe))
{
hr = HRESULT_FROM_WIN32(ERROR_NO_MORE_FILES);
goto Exit0;
}
hr =S_FALSE;
do
{
if (0 == _tcsicmp(cpszExeFileName, pe.szExeFile))
{
dwPID = pe.th32ProcessID;
hr = S_OK;
break;
}
}while(Process32Next(hSnapshot, &pe));
Exit0:
if(hSnapshot)
{
CloseHandle(hSnapshot);
hSnapshot = NULL;
}
returnhr;
}
int GetProcessCpuPercent(const HANDLE hProcess, const DWORDdwElepsedTime)
{
intnProcCpuPercent = 0;
BOOLbRetCode = FALSE;
FILETIMECreateTime, 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)/ 10000;
nProcCpuPercent = (int)((lgCurTime.QuadPart -g_slgProcessTimeOld.QuadPart) * 100 / dwElepsedTime);
g_slgProcessTimeOld = lgCurTime;
nProcCpuPercent = nProcCpuPercent / g_sdwProcessorCoreNum;
}
else
{
nProcCpuPercent = -1;
}
returnnProcCpuPercent;
}
unsigned __stdcall WorkerThread(void *pArg)
{
HRESULT hr =E_FAIL;
HANDLEhProcess = NULL;
DWORDdwProcessId = 0;
DWORDdwRetVal = 0;
DWORDdwCurrentTickCount = 0;
DWORDdwElapsedTime = 0;
intnProcessCpuPercent = 0;
TARGET_PROCESS *pTargetProcess = (TARGET_PROCESS *)pArg;
dwProcessId = pTargetProcess->dwProcessId;
hProcess =OpenProcess(PROCESS_QUERY_INFORMATION , FALSE, dwProcessId);
MY_PROCESS_ERROR(hProcess);
//#############################
DWORD dwAverageCpuOwnerPercent = 0;
DWORD dwTotalCpuOwnerPercent = 0;
DWORD dwTimes = 0;
DWORD dwMaxCpuOwnerPercent = 0;
DWORD dwMinCpuOwnerPercent = 100;
//############################
do
{
dwRetVal = WaitForSingleObject(g_shExitEvent, 1000);
if (WAIT_OBJECT_0 == dwRetVal ||
WAIT_FAILED == dwRetVal
)
{
break;
}
dwCurrentTickCount = GetTickCount();
dwElapsedTime = dwCurrentTickCount - g_sdwTickCountOld;
g_sdwTickCountOld = dwCurrentTickCount;
nProcessCpuPercent = GetProcessCpuPercent(hProcess,dwElapsedTime);
if(dwMaxCpuOwnerPercent <nProcessCpuPercent)
{
dwMaxCpuOwnerPercent= nProcessCpuPercent;
}
if(dwMinCpuOwnerPercent >nProcessCpuPercent)
{
dwMinCpuOwnerPercent= nProcessCpuPercent;
}
dwTotalCpuOwnerPercent +=nProcessCpuPercent;
dwTimes++;
dwAverageCpuOwnerPercent =dwTotalCpuOwnerPercent / dwTimes;
wprintf(L"cpu:- avg- max-\n", nProcessCpuPercent,dwAverageCpuOwnerPercent,dwMaxCpuOwnerPercent);
//wprintf(L"cpu:%d\n",nProcessCpuPercent);
} while(1);
Exit0:
if(hProcess)
{
CloseHandle(hProcess);
hProcess = NULL;
}
return0;
}
int main(int argc, _TCHAR* argv[])
{
HRESULT hr =E_FAIL;
HANDLEhThread = NULL;
unsigned intuiTid = 0;
SYSTEM_INFOsysInfo = { 0 };
TARGET_PROCESS struTargetProcess;
if (argc> 1)
{
hr = FindFirstProcessIdByName(argv[1],struTargetProcess.dwProcessId);
if (S_OK != hr)
{
_tprintf(_T("Can't find process \'%s\' ret=0x%X\n"), argv[1],hr);
goto Exit0;
}
GetSystemInfo(&sysInfo);
g_sdwProcessorCoreNum = sysInfo.dwNumberOfProcessors;
g_shExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
MY_PROCESS_ERROR(g_shExitEvent);
hThread = (HANDLE)_beginthreadex(NULL, 0, WorkerThread,&struTargetProcess, 0, &uiTid);
MY_PROCESS_ERROR(hThread);
_getch();
SetEvent(g_shExitEvent);
WaitForSingleObject(hThread, INFINITE);
}
else
{
_tprintf(_T("Please input a process name.\n"));
}
Exit0:
if(hThread)
{
CloseHandle(hThread);
hThread = NULL;
}
if(g_shExitEvent)
{
CloseHandle(g_shExitEvent);
g_shExitEvent = NULL;
}
return0;
}