2009-11-25 60 views

回答

2

有幾個或幾個Windows API調用名稱中的「鉤子」,允許您捕獲系統級事件。您必須將這些內容構建到DLL中,然後從單獨的應用程序調用「掛鉤」DLL。

那很簡單,就是這樣。希望能讓你開始!

3

您可以使用GetProcessTimes函數獲取特定進程的計時信息。

BOOL WINAPI GetProcessTimes(
    __in HANDLE hProcess, 
    __out LPFILETIME lpCreationTime, 
    __out LPFILETIME lpExitTime, 
    __out LPFILETIME lpKernelTime, 
    __out LPFILETIME lpUserTime 
); 

見這個例子

program GetProcessTime; 

{$APPTYPE CONSOLE} 

uses 
    DateUtils, 
    Windows, 
    tlhelp32, 
    SysUtils; 


Procedure GetAllProcessTime; 
var 
    HandleSnapShot : THandle; 
    EntryParentProc : TProcessEntry32; 
    DummyCreateFileTime : Windows.FILETIME; 
    DummyExitFileTime : Windows.FILETIME; 
    DummyKernelFileTime : Windows.FILETIME; 
    DummyUserFileTime : Windows.FILETIME; 
    aFileName   : String; 
    h     : THandle; 
    ActualTime   : TDateTime; 
    Dif     : TDateTime; 
    CreationTime  : TDateTime; 


function FileTime2DateTime(FileTime: TFileTime): TDateTime; //Convert then FileTime to TDatetime format 
var 
    LocalTime: TFileTime; 
    DOSTime : Integer; 
begin 
    FileTimeToLocalFileTime(FileTime, LocalTime); 
    FileTimeToDosDateTime(LocalTime, LongRec(DOSTime).Hi, LongRec(DOSTime).Lo); 
    Result := FileDateToDateTime(DOSTime); 
end; 

begin 
    HandleSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); //get the list of process 
    if HandleSnapShot <> INVALID_HANDLE_VALUE then 
    begin 
    EntryParentProc.dwSize := SizeOf(EntryParentProc); 
    if Process32First(HandleSnapShot, EntryParentProc) then //Get the first process in the list 
    begin 
     Writeln(Format('%-30s %-20s %-16s',['FileName','Start','Running Time']) ); 
     ActualTime:=Now; 
     repeat 
      h:=OpenProcess(PROCESS_QUERY_INFORMATION,false,EntryParentProc.th32ProcessID); //open a particular process 
      if GetProcessTimes(h, DummyCreateFileTime, DummyExitFileTime, DummyKernelFileTime, DummyUserFileTime) then //get the timing info 
      begin 
      aFileName:=ExtractFileName(EntryParentProc.szExeFile); 
      CreationTime:=FileTime2DateTime(DummyCreateFileTime); //get the initial time of the process 
      Dif := ActualTime-CreationTime; //calculate the elapsed time 
      Writeln(Format('%-30s %-20s %-16s',[aFileName,FormatDateTime('DD-MM-YYYY HH:NN:SS',CreationTime),FormatDateTime('HH:NN:SS',Dif)]) ); 
      end; 
      CloseHandle(h); 
     until not Process32Next(HandleSnapShot, EntryParentProc); 
    end; 
    CloseHandle(HandleSnapShot); 
    end; 

end; 



begin 
    try 
    GetAllProcessTime(); 
    Readln; 
    except 
    on E: Exception do 
    begin 
    Writeln(E.ClassName, ': ', E.Message); 
    Readln; 
    end; 
    end; 
end. 
1

WH_CBT hook是最有可能的一個你是後。它允許您在創建或銷燬窗口時通知操作系統。您將希望使用此掛鉤來抓取手柄,並從手柄獲取使用GetWindowThreadProcessId的進程ID。然後,您可以將此句柄傳遞給函數GetProcessTimes(由RRUZ建議)以獲得時間。一個示例(儘管過時,概念仍然相同)可在GpSysHook源文件中找到。

1

Windows Performance Analyzer具有專門用於計時應用程序的功能,即使它們處於啓動或登錄順序,也可以從啓動時開始計時。

1

Windows Management Instrumentation提供事件訂閱。 WMI的好處在於它使用DCOM和SOAP也可以工作在遠程。

WMI offers the capability to notify a subscriber for any event it is interested in.

WMI使用WMI查詢語言(WQL) 提交WQL事件查詢和 定義的事件類型是 返回。所有相關回調的事件機制 WMI COM/DCOM和自動化接口 的一部分。

德爾福的免費WMI客戶端實現可在網上(如果它支持事件回調不知道):

Magenta Systems WMI and SMART Component