home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kompon / d2345 / MSYSINFO.ZIP / Source / MSI_Processes.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-21  |  8KB  |  322 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       MiTeC System Information Component              }
  5. {               Process Detection Part                  }
  6. {           version 5.6 for Delphi 3,4,5                }
  7. {                                                       }
  8. {       Copyright ⌐ 1997,2001 Michal Mutl               }
  9. {                                                       }
  10. {*******************************************************}
  11.  
  12. {$INCLUDE MITEC_DEF.INC}
  13.  
  14.  
  15. unit MSI_Processes;
  16.  
  17. interface
  18.  
  19. uses
  20.   SysUtils, Windows, Classes;
  21.  
  22. type
  23.   TProcesses = class(TPersistent)
  24.   private
  25.     FProcessList: TStringlist;
  26.  
  27.     function GetProcessList(var List: TStringList; FullPath: Boolean {$IFDEF SUPPORTS_DEFAULTPARAMS} = True {$ENDIF}): Boolean;
  28.     function GetProcessCount: integer;
  29.     function GetProcessName(Index: integer): string;
  30.     procedure SetProcessCount(const Value: integer);
  31.  
  32.     procedure ClearList;
  33.   public
  34.     constructor Create;
  35.     destructor Destroy; override;
  36.     procedure GetInfo;
  37.     procedure Report(var sl :TStringList);
  38.   published
  39.     property ProcessCount: integer read GetProcessCount write SetProcessCount;
  40.   public
  41.     property ProcessNames[Index: integer]: string read GetProcessName;
  42.  
  43.     function GetPidFromProcessName(const ProcessName: string): DWORD;
  44.     function GetProcessNameFromWnd(Wnd: HWND): string;
  45.     function GetProcessNameFromPid(PID: DWORD): string;
  46.  
  47.     function TerminateProcess(PID: DWORD; Timeout: Integer): Boolean;
  48.  
  49.     function GetTasksList(var List: TStringList): Boolean;
  50.   end;
  51.  
  52. implementation
  53.  
  54. uses MiTeC_PSAPI, MiTeC_Routines, MiTeC_ToolHelp32, Messages;
  55.  
  56. { TProcesses }
  57.  
  58. constructor TProcesses.Create;
  59. begin
  60.   FProcessList:=TStringList.Create;
  61. end;
  62.  
  63. destructor TProcesses.Destroy;
  64. begin
  65.   ClearList;
  66.   FProcessList.Free;
  67.   inherited;
  68. end;
  69.  
  70. procedure TProcesses.GetInfo;
  71. begin
  72.   try
  73.     ClearList;
  74.     GetProcessList(FProcessList,True);
  75.   except
  76.     on e:Exception do begin
  77.       MessageBox(0,PChar(e.message),'TProcesses.GetInfo',MB_OK or MB_ICONERROR);
  78.     end;
  79.   end;
  80. end;
  81.  
  82. function TProcesses.GetPidFromProcessName(
  83.   const ProcessName: string): DWORD;
  84. var
  85.   i: Integer;
  86. begin
  87.   Result:=INVALID_HANDLE_VALUE;
  88.   i:=FProcessList.IndexOf(ProcessName);
  89.   if i>-1 then
  90.     Result:=DWORD(FProcessList.Objects[I]);
  91. end;
  92.  
  93. function TProcesses.GetProcessCount: integer;
  94. begin
  95.   Result:=FProcessList.Count;
  96. end;
  97.  
  98. function TProcesses.GetProcessName(Index: integer): string;
  99. begin
  100.   try
  101.     Result:=FProcessList[Index];
  102.   except
  103.     Result:='';
  104.   end;
  105. end;
  106.  
  107. function TProcesses.GetProcessNameFromPid(PID: DWORD): string;
  108. var
  109.   i: integer;
  110. begin
  111.   Result:='';
  112.   i:=FProcessList.IndexOfObject(Pointer(PID));
  113.   if i>-1 then
  114.     Result:=FProcessList[i];
  115. end;
  116.  
  117. function TProcesses.GetProcessNameFromWnd(Wnd: HWND): string;
  118. var
  119.   PID: DWORD;
  120.   i: Integer;
  121. begin
  122.   Result:='';
  123.   if IsWindow(Wnd) then begin
  124.     PID:=INVALID_HANDLE_VALUE;
  125.     GetWindowThreadProcessId(Wnd,@PID);
  126.     i:=FProcessList.IndexOfObject(Pointer(PID));
  127.     if i>-1 then
  128.       Result:=FProcessList[i];
  129.   end;
  130. end;
  131.  
  132. function TProcesses.GetTasksList;
  133.  
  134.   function EnumWindowsProc(Wnd: HWND; List: TStrings): Boolean; stdcall;
  135.   var
  136.     ParentWnd: HWND;
  137.     ExStyle: DWORD;
  138.     Caption: array [0..255] of Char;
  139.   begin
  140.     if IsWindowVisible(Wnd) then begin
  141.       ParentWnd:=GetWindowLong(Wnd,GWL_HWNDPARENT);
  142.       ExStyle:=GetWindowLong(Wnd,GWL_EXSTYLE);
  143.       if ((ParentWnd=0) or (ParentWnd=GetDesktopWindow)) and
  144.         ((ExStyle and WS_EX_TOOLWINDOW=0) or (ExStyle and WS_EX_APPWINDOW<>0)) and
  145.         (GetWindowText(Wnd,Caption,SizeOf(Caption))>0) then
  146.           List.AddObject(Caption,Pointer(Wnd));
  147.     end;
  148.     Result:=True;
  149.   end;
  150.  
  151. begin
  152.   Result:=EnumWindows(@EnumWindowsProc,Integer(List));
  153. end;
  154.  
  155. procedure TProcesses.Report(var sl: TStringList);
  156. var
  157.   i,n: integer;
  158. begin
  159.   with sl do begin
  160.     Add('[Processes]');
  161.     n:=ProcessCount;
  162.     Add(Format('Count=%d',[n]));
  163.     for i:=0 to n-1 do 
  164.       Add(Format('%d=%s',[GetPIDFromProcessName(ProcessNames[i]),ProcessNames[i]]));
  165.   end;
  166. end;
  167.  
  168. function TProcesses.GetProcessList;
  169.  
  170.   function ProcessFileName(PID: DWORD): string;
  171.   var
  172.     Handle: THandle;
  173.   begin
  174.     Result:='';
  175.     Handle:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,False,PID);
  176.     if Handle<>0 then
  177.       try
  178.         SetLength(Result,MAX_PATH);
  179.         if FullPath then begin
  180.           if GetModuleFileNameEx(Handle,0,PChar(Result),MAX_PATH)>0 then
  181.             SetLength(Result,StrLen(PChar(Result)))
  182.           else
  183.             Result:='';
  184.         end else begin
  185.           if GetModuleBaseName(Handle,0,PChar(Result),MAX_PATH)>0 then
  186.             SetLength(Result,StrLen(PChar(Result)))
  187.           else
  188.             Result:='';
  189.         end;
  190.       finally
  191.         CloseHandle(Handle);
  192.       end;
  193.   end;
  194.  
  195.   function BuildList_ToolHelp32: Boolean;
  196.   var
  197.     SnapProcHandle: THandle;
  198.     ProcEntry: TProcessEntry32;
  199.     NextProc: Boolean;
  200.     FileName: string;
  201.   begin
  202.     SnapProcHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  203.     Result:=(SnapProcHandle<>INVALID_HANDLE_VALUE);
  204.     if Result then
  205.       try
  206.         ProcEntry.dwSize:=SizeOf(ProcEntry);
  207.         NextProc:=Process32First(SnapProcHandle,ProcEntry);
  208.         while NextProc do begin
  209.           if ProcEntry.th32ProcessID=0 then begin
  210.             FileName:='System Idle Process';
  211.           end else begin
  212.             if GetOS=os2K then begin
  213.               FileName:=ProcessFileName(ProcEntry.th32ProcessID);
  214.               if FileName='' then
  215.                 FileName:=ProcEntry.szExeFile;
  216.             end else begin
  217.               FileName:=ProcEntry.szExeFile;
  218.               if not FullPath then
  219.                 FileName:=ExtractFileName(FileName);
  220.             end;
  221.           end;
  222.           List.AddObject(FileName,Pointer(ProcEntry.th32ProcessID));
  223.           NextProc:=Process32Next(SnapProcHandle,ProcEntry);
  224.         end;
  225.       finally
  226.         CloseHandle(SnapProcHandle);
  227.       end;
  228.   end;
  229.  
  230.   function BuildList_PSAPI: Boolean;
  231.   var
  232.     PIDs: array [0..1024] of DWORD;
  233.     Needed: DWORD;
  234.     i: Integer;
  235.     FileName: string;
  236.   begin
  237.     Result:=EnumProcesses(@PIDs,SizeOf(PIDs),Needed);
  238.     if Result then begin
  239.       for i:=0 to (Needed div SizeOf(DWORD))-1 do begin
  240.         case PIDs[I] of
  241.           0: FileName:='System Idle Process';
  242.           2: if GetOS=osNT4 then
  243.               FileName:='System Process'
  244.             else
  245.               FileName:=ProcessFileName(PIDs[i]);
  246.           8: if GetOS=os2K then
  247.               FileName:='System Process'
  248.             else
  249.               FileName:=ProcessFileName(PIDs[i]);
  250.         else
  251.           FileName:=ProcessFileName(PIDs[i]);
  252.         end;
  253.         if FileName<>'' then
  254.           List.AddObject(FileName,Pointer(PIDs[i]));
  255.       end;
  256.     end;
  257.   end;
  258.  
  259. begin
  260.   if GetOS=osNT4 then
  261.     Result:=BuildList_PSAPI
  262.   else
  263.     Result:=BuildList_ToolHelp32;
  264. end;
  265.  
  266. function TProcesses.TerminateProcess(PID: DWORD;
  267.   Timeout: Integer): Boolean;
  268. var
  269.   ProcessHandle: THandle;
  270.  
  271.   function EnumWindowsProc(Wnd: HWND; ProcessID: DWORD): Boolean; stdcall;
  272.   var
  273.     PID: DWORD;
  274.   begin
  275.     GetWindowThreadProcessId(Wnd,@PID);
  276.     if ProcessID=PID then
  277.       PostMessage(Wnd,WM_CLOSE,0,0);
  278.     Result:=True;
  279.   end;
  280.  
  281. begin
  282.   Result:=False;
  283.   if PID<>GetCurrentProcessId then begin
  284.     ProcessHandle:=OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE,False,PID);
  285.     try
  286.       if ProcessHandle<>0 then begin
  287.         EnumWindows(@EnumWindowsProc,PID);
  288.         if WaitForSingleObject(ProcessHandle,Timeout)=WAIT_OBJECT_0 then
  289.           Result:=True //Clean
  290.         else
  291.           if TerminateProcess(ProcessHandle,0) then
  292.             Result:=True; //Kill
  293.       end;
  294.     finally
  295.       CloseHandle(ProcessHandle);
  296.     end;
  297.   end;
  298. end;
  299.  
  300. procedure TProcesses.SetProcessCount(const Value: integer);
  301. begin
  302.  
  303. end;
  304.  
  305. procedure TProcesses.ClearList;
  306. var
  307.   p :PDWORD;
  308. begin
  309.   while FProcessList.count>0 do begin
  310.    p:=PDWORD(FProcessList.Objects[FProcessList.count-1]);
  311.    FProcessList.Delete(FProcessList.count-1);
  312.   end;
  313. end;
  314.  
  315. initialization
  316.   if GetOS=osNT4 then
  317.     InitPSAPI;
  318. finalization
  319.   if GetOS=osNT4 then
  320.     FreePSAPI;
  321. end.
  322.