Einzelnen Beitrag anzeigen

af99

Registriert seit: 22. Apr 2004
84 Beiträge
 
Delphi XE5 Ultimate
 
#8

AW: CPU und Memory Nutzung eines Prozesses

  Alt 20. Mai 2015, 15:10
der Fehler in ShowMemoryUsage kommt hier
Delphi-Quellcode:
hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ProcessId);
     begin
       if ( hProcess = 0 ) then
       begin
         WriteLn(SysErrorMessage(GetLastError));
         writeln('fehler');
         Exit;
       end;
das "SysErrorMessage(GetLastError));" bringt "Zugriff verweigert" in der Funktion GetCpuUsage benutze ich "h:=OpenProcess(PROCESS_QUERY_INFORMATION,false,PI D);" und ich bekomme keinen Fehler.
Delphi-Quellcode:
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;
der Fehler in "GetProcessMemorySize" kommt hier
Delphi-Quellcode:
  l_nWndHandle := FindWindow(nil, PChar(sProcessName));
  if l_nWndHandle = 0 then
  begin
    WriteLn(SysErrorMessage(GetLastError));
    writeln('fehler');
    Result := False;
    Exit;
  end;
  Mit Zitat antworten Zitat