delphi实现进程监控。

作者在 2014-05-11 18:49:55 发布以下内容
program Project1;
uses
Windows, Messages,  SysUtils,
Dialogs,  StdCtrls,Tlhelp32,shellapi; //DateUtils 用时间需要定义
{$R *.res}
function CheckTask(ExeFileName: string): Boolean;  //监视进程函数
const
PROCESS_TERMINATE=$0001; 
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle; 
FProcessEntry32: TProcessEntry32;
begin 
result := False; 
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop) <> 0 do begin
       if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =UpperCase(ExeFileName))
       or (UpperCase(FProcessEntry32.szExeFile) =UpperCase(ExeFileName))) then 
         result := True;
       ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32); 
end;
end;
function KillTask(ExeFileName:string):integer; 
const 
PROCESS_TERMINATE = $0001; 
var 
ContinueLoop: BOOLean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0; 
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
FProcessEntry32.dwSize := SizeOf(FProcessEntry32); 
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
//检测函数


function EndProcess(ExeFileName:string):integer;
const
  PROCESS_TERMINATE = $0001;
var
  ContinueLoop: BOOLean;
  FSnapshotHandle: THandle;
  FProcessEntry32:TProcessEntry32;
begin
  Result := 0;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);


while Integer(ContinueLoop) <> 0 do
begin
  if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
  UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
  UpperCase(ExeFileName))) then
  Result := Integer(
  TerminateProcess(OpenProcess(PROCESS_TERMINATE,
  BOOL(0),FProcessEntry32.th32ProcessID),0));
  ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;   
  CloseHandle(FSnapshotHandle);
end;


function DeleteDirectory(NowPath: string): Boolean; // 删除整个目录
var
  search: TSearchRec;
  ret: integer;
  key: string;
begin
  if NowPath[Length(NowPath)] <> '\' then
    NowPath := NowPath + '\';
  key := NowPath + '*.*';
  ret := findFirst(key, faanyfile, search);
  while ret = 0 do
  begin
    if ((search.Attr and fadirectory) = fadirectory) then
    begin
      if (search.Name <> '.') and (search.name <> '..') then
        DeleteDirectory(NowPath + search.name);
    end
    else
    begin
      if ((search.Attr and fadirectory) <> fadirectory) then
      begin
        deletefile(NowPath + search.name);
      end;
    end;
    ret := FindNext(search);
  end;
  findClose(search);
  //removedir(NowPath); 如果需要删除文件夹则添加
  result := True;
end;


begin


   if  CheckTask('qq.exe')=true then
   begin
    endprocess('qq.exe');
    DeleteDirectory('D:\QQMusicCache\WhirlCache');
    sleep(15000);
    ShellExecute(0, 'open', PChar('C:\Program Files\Tencent\QQ\QQProtect\Bin\QQProtect.exe'), nil, nil, SW_SHOW);
   end
   else
    showmessage('FUCK WXSUNNSY');
end.


编程点滴 | 阅读 2908 次
文章评论,共0条
游客请输入验证码
浏览2343950次