home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d56 / MSYSINFO.ZIP / Source / MSI_Processes.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-07-24  |  8.2 KB  |  317 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       MiTeC System Information Component              }
  5. {               Process Detection Part                  }
  6. {           version 6.0 for Delphi 5,6                  }
  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 {$IFNDEF D6PLUS} write SetProcessCount {$ENDIF} stored False;
  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.   ClearList;
  73.   GetProcessList(FProcessList,True);
  74. end;
  75.  
  76. function TProcesses.GetPidFromProcessName(
  77.   const ProcessName: string): DWORD;
  78. var
  79.   i: Integer;
  80. begin
  81.   Result:=INVALID_HANDLE_VALUE;
  82.   i:=FProcessList.IndexOf(ProcessName);
  83.   if i>-1 then
  84.     Result:=DWORD(FProcessList.Objects[I]);
  85. end;
  86.  
  87. function TProcesses.GetProcessCount: integer;
  88. begin
  89.   Result:=FProcessList.Count;
  90. end;
  91.  
  92. function TProcesses.GetProcessName(Index: integer): string;
  93. begin
  94.   try
  95.     Result:=FProcessList[Index];
  96.   except
  97.     Result:='';
  98.   end;
  99. end;
  100.  
  101. function TProcesses.GetProcessNameFromPid(PID: DWORD): string;
  102. var
  103.   i: integer;
  104. begin
  105.   Result:='';
  106.   i:=FProcessList.IndexOfObject(Pointer(PID));
  107.   if i>-1 then
  108.     Result:=FProcessList[i];
  109. end;
  110.  
  111. function TProcesses.GetProcessNameFromWnd(Wnd: HWND): string;
  112. var
  113.   PID: DWORD;
  114.   i: Integer;
  115. begin
  116.   Result:='';
  117.   if IsWindow(Wnd) then begin
  118.     PID:=INVALID_HANDLE_VALUE;
  119.     GetWindowThreadProcessId(Wnd,@PID);
  120.     i:=FProcessList.IndexOfObject(Pointer(PID));
  121.     if i>-1 then
  122.       Result:=FProcessList[i];
  123.   end;
  124. end;
  125.  
  126. function TProcesses.GetTasksList;
  127.  
  128.   function EnumWindowsProc(Wnd: HWND; List: TStrings): Boolean; stdcall;
  129.   var
  130.     ParentWnd: HWND;
  131.     ExStyle: DWORD;
  132.     Caption: array [0..255] of Char;
  133.   begin
  134.     if IsWindowVisible(Wnd) then begin
  135.       ParentWnd:=GetWindowLong(Wnd,GWL_HWNDPARENT);
  136.       ExStyle:=GetWindowLong(Wnd,GWL_EXSTYLE);
  137.       if ((ParentWnd=0) or (ParentWnd=GetDesktopWindow)) and
  138.         ((ExStyle and WS_EX_TOOLWINDOW=0) or (ExStyle and WS_EX_APPWINDOW<>0)) and
  139.         (GetWindowText(Wnd,Caption,SizeOf(Caption))>0) then
  140.           List.AddObject(Caption,Pointer(Wnd));
  141.     end;
  142.     Result:=True;
  143.   end;
  144.  
  145. begin
  146.   Result:=EnumWindows(@EnumWindowsProc,Integer(List));
  147. end;
  148.  
  149. procedure TProcesses.Report(var sl: TStringList);
  150. var
  151.   i,n: integer;
  152. begin
  153.   with sl do begin
  154.     Add('[Processes]');
  155.     n:=ProcessCount;
  156.     Add(Format('Count=%d',[n]));
  157.     for i:=0 to n-1 do 
  158.       Add(Format('%d=%s',[GetPIDFromProcessName(ProcessNames[i]),ProcessNames[i]]));
  159.   end;
  160. end;
  161.  
  162. function TProcesses.GetProcessList;
  163.  
  164.   function ProcessFileName(PID: DWORD): string;
  165.   var
  166.     Handle: THandle;
  167.   begin
  168.     Result:='';
  169.     Handle:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,False,PID);
  170.     if Handle<>0 then
  171.       try
  172.         SetLength(Result,MAX_PATH);
  173.         if FullPath then begin
  174.           if GetModuleFileNameEx(Handle,0,PChar(Result),MAX_PATH)>0 then
  175.             SetLength(Result,StrLen(PChar(Result)))
  176.           else
  177.             Result:='';
  178.         end else begin
  179.           if GetModuleBaseName(Handle,0,PChar(Result),MAX_PATH)>0 then
  180.             SetLength(Result,StrLen(PChar(Result)))
  181.           else
  182.             Result:='';
  183.         end;
  184.       finally
  185.         CloseHandle(Handle);
  186.       end;
  187.   end;
  188.  
  189.   function BuildList_ToolHelp32: Boolean;
  190.   var
  191.     SnapProcHandle: THandle;
  192.     ProcEntry: TProcessEntry32;
  193.     NextProc: Boolean;
  194.     FileName: string;
  195.   begin
  196.     SnapProcHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
  197.     Result:=(SnapProcHandle<>INVALID_HANDLE_VALUE);
  198.     if Result then
  199.       try
  200.         ProcEntry.dwSize:=SizeOf(ProcEntry);
  201.         NextProc:=Process32First(SnapProcHandle,ProcEntry);
  202.         while NextProc do begin
  203.           if ProcEntry.th32ProcessID=0 then begin
  204.             FileName:='System Idle Process';
  205.           end else begin
  206.             if GetOS=os2K then begin
  207.               FileName:=ProcessFileName(ProcEntry.th32ProcessID);
  208.               if FileName='' then
  209.                 FileName:=ProcEntry.szExeFile;
  210.             end else begin
  211.               FileName:=ProcEntry.szExeFile;
  212.               if not FullPath then
  213.                 FileName:=ExtractFileName(FileName);
  214.             end;
  215.           end;
  216.           List.AddObject(FileName,Pointer(ProcEntry.th32ProcessID));
  217.           NextProc:=Process32Next(SnapProcHandle,ProcEntry);
  218.         end;
  219.       finally
  220.         CloseHandle(SnapProcHandle);
  221.       end;
  222.   end;
  223.  
  224.   function BuildList_PSAPI: Boolean;
  225.   var
  226.     PIDs: array [0..1024] of DWORD;
  227.     Needed: DWORD;
  228.     i: Integer;
  229.     FileName: string;
  230.   begin
  231.     Result:=EnumProcesses(@PIDs,SizeOf(PIDs),Needed);
  232.     if Result then begin
  233.       for i:=0 to (Needed div SizeOf(DWORD))-1 do begin
  234.         case PIDs[I] of
  235.           0: FileName:='System Idle Process';
  236.           2: if GetOS=osNT4 then
  237.               FileName:='System Process'
  238.             else
  239.               FileName:=ProcessFileName(PIDs[i]);
  240.           8: if GetOS=os2K then
  241.               FileName:='System Process'
  242.             else
  243.               FileName:=ProcessFileName(PIDs[i]);
  244.         else
  245.           FileName:=ProcessFileName(PIDs[i]);
  246.         end;
  247.         if FileName<>'' then
  248.           List.AddObject(FileName,Pointer(PIDs[i]));
  249.       end;
  250.     end;
  251.   end;
  252.  
  253. begin
  254.   if GetOS=osNT4 then
  255.     Result:=BuildList_PSAPI
  256.   else
  257.     Result:=BuildList_ToolHelp32;
  258. end;
  259.  
  260. function TProcesses.TerminateProcess(PID: DWORD;
  261.   Timeout: Integer): Boolean;
  262. var
  263.   ProcessHandle: THandle;
  264.  
  265.   function EnumWindowsProc(Wnd: HWND; ProcessID: DWORD): Boolean; stdcall;
  266.   var
  267.     PID: DWORD;
  268.   begin
  269.     GetWindowThreadProcessId(Wnd,@PID);
  270.     if ProcessID=PID then
  271.       PostMessage(Wnd,WM_CLOSE,0,0);
  272.     Result:=True;
  273.   end;
  274.  
  275. begin
  276.   Result:=False;
  277.   if PID<>GetCurrentProcessId then begin
  278.     ProcessHandle:=OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE,False,PID);
  279.     try
  280.       if ProcessHandle<>0 then begin
  281.         EnumWindows(@EnumWindowsProc,PID);
  282.         if WaitForSingleObject(ProcessHandle,Timeout)=WAIT_OBJECT_0 then
  283.           Result:=True //Clean
  284.         else
  285.           if TerminateProcess(ProcessHandle,0) then
  286.             Result:=True; //Kill
  287.       end;
  288.     finally
  289.       CloseHandle(ProcessHandle);
  290.     end;
  291.   end;
  292. end;
  293.  
  294. procedure TProcesses.SetProcessCount(const Value: integer);
  295. begin
  296.  
  297. end;
  298.  
  299. procedure TProcesses.ClearList;
  300. {var
  301.   p: PDWORD;}
  302. begin
  303.   while FProcessList.count>0 do begin
  304. //   p:=PDWORD(FProcessList.Objects[FProcessList.count-1]);
  305.    FProcessList.Delete(FProcessList.count-1);
  306. //   dispose(p);
  307.   end;
  308. end;
  309.  
  310. initialization
  311.   if GetOS=osNT4 then
  312.     InitPSAPI;
  313. finalization
  314.   if GetOS=osNT4 then
  315.     FreePSAPI;
  316. end.
  317.