반응형
// 작업 관리자의 'CPU 시간'
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, psapi;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetLocalTime(a: Tfiletime): String;
var
mtm: TSystemTime;
at: TFileTime;
ds,ts: ShortString;
begin
FileTimeToLocalFileTime(a, at);
FileTimeToSystemTime(at, mtm);
SetLength(ds, GetDateFormat(LOCALE_USER_DEFAULT, 0, @mtm, NIL, @ds[1], 255) - 1);
SetLength(ts, GetTimeFormat(LOCALE_USER_DEFAULT, time_noseconds, @mtm, NIL, @ts[1], 255) - 1);
Result:=ds+' '+ts;
end;
procedure TForm1.Button1Click(Sender: TObject);
type
integer = DWORD;
var
i, pidNeeded: Integer;
PIDList : array[0..1000] of Integer; // 배열 크기는 충분히 크게 잡는다
PIDName : array [0..MAX_PATH - 1] of char;
PH : THandle;
lpCreationTime, lpExitTime, lpKernelTime, lpUserTime: TFileTime;
KernelTime, UserTime: TDateTime;
KernelDay, UserDay: integer;
lpSystemTime: TSystemTime;
begin
ListBox1.Items.Clear;
// 모든 process id를 배열에 저장한다
if not EnumProcesses (@PIDList, 1000, pidNeeded) then
begin
ListBox1.Items.Add('Need psapi.dll');
exit;
end;
// 각각의 프로세스를 연다
for i := 0 to (pidNeeded div sizeof(Integer)-1) do
begin
// 프로세스의 핸들을 얻는다
PH := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PIDList[i]);
if PH <> 0 then
begin
if GetModuleBaseName(PH, 0, PIDName, sizeof (PIDName)) > 0 then
begin
GetProcessTimes(PH, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime);
FileTimeToSystemTime(lpKernelTime, lpSystemTime);
KernelDay := lpSystemTime.wDay;
KernelTime := SystemTimeToDateTime(lpSystemTime);
FileTimeToSystemTime(lpUserTime, lpSystemTime);
UserDay := lpSystemTime.wDay;
UserTime := SystemTimeToDateTime(lpSystemTime);
ListBox1.Items.Add(StrPas(PIDName)+'('+IntToStr(PIDList[i])+') '+
'['+GetLocalTime(lpCreationTime)+'] '+ // 시작 시간
'['+FormatDateTime('hh:mm:ss',UserTime + KernelTime)+']'); // CPU 시간
end;
end;
if PH > 0 then CloseHandle(PH);
end;
end;
end.
반응형