2009-11-25 61 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