function GetCpuUsage(PID:cardinal):single;
const
cWaitTime=750;
var
h : Cardinal;
mCreationTime,mExitTime,mKernelTime, mUserTime:_FILETIME;
TotalTime1,TotalTime2:int64;
begin
{We need to get a handle of the process with PROCESS_QUERY_INFORMATION privileges.}
h:=OpenProcess(PROCESS_QUERY_INFORMATION,false,PID);
{We can use the GetProcessTimes() function to get the amount of time the process has spent in kernel mode and user mode.}
GetProcessTimes(h,mCreationTime,mExitTime,mKernelTime,mUserTime);
TotalTime1:=int64(mKernelTime.dwLowDateTime
or (mKernelTime.dwHighDateTime
shr 32)) + int64(mUserTime.dwLowDateTime
or (mUserTime.dwHighDateTime
shr 32));
{Wait a little}
Sleep(cWaitTime);
GetProcessTimes(h,mCreationTime,mExitTime,mKernelTime,mUserTime);
TotalTime2:=int64(mKernelTime.dwLowDateTime
or (mKernelTime.dwHighDateTime
shr 32)) + int64(mUserTime.dwLowDateTime
or (mUserTime.dwHighDateTime
shr 32));
{This should work out nicely, as there were approx. 250 ms between the calls
and the result will be a percentage between 0 and 100}
Result:=((TotalTime2-TotalTime1)/cWaitTime)/100;
CloseHandle(h);
end;