CSDN博客

img ly_liuyang

查找一个特定的EXE是否在内存中运行

发表于2001/10/16 12:16:00  1593人阅读

分类: 技术

unit Find_Unit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, TLHelp32, ComCtrls;

type
  TFindForm = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    SB: TStatusBar;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function FindProc(ProcName: string): boolean;
  end;

var
  FindForm: TFindForm;

implementation

{$R *.DFM}

procedure TFindForm.Button1Click(Sender: TObject);
var
  hFile: THandle;
  r: Boolean;
  tf: string;
begin
  r := False;
  hFile := CreateFile(PChar(Edit1.Text), Generic_Read or
         Generic_Write, File_Share_Read or File_Share_Write,
         nil, Open_Existing, File_Attribute_Normal, 0);
  if hFile <> Invalid_Handle_Value then
     begin
       CloseHandle(hFile);
       r := True;
     end;
  if r
     then sb.Panels[0].Text:='Space Find!'
     else sb.Panels[0].Text:='Space Not Found!';
  tf := Edit2.text;
  if Pos('.', tf) = 0 then tf := tf + '.exe';
  if FindProc(tf)
     then sb.Panels[1].Text:='Process Find!'
     else sb.Panels[1].Text:='Process Not Found!';
end;

function TFindForm.FindProc(ProcName: string): boolean;
var
  OK: Bool;
  hPL, hML: THandle;
  ProcessStruct: TProcessEntry32;
  ModuleStruct: TModuleEntry32;
begin
  Result := False;
  hPL := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  ProcessStruct.dwSize := SizeOf(TProcessEntry32);
  OK := Process32First(hPL, ProcessStruct);
  while OK do
    begin
      if UpperCase(ProcessStruct.szExeFile) = UpperCase(ProcName) then
         begin
           Result := True;
           // find path info
           hML := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessStruct.th32ProcessID);
           ModuleStruct.dwSize := SizeOf(TModuleEntry32);
           Module32First(hML, ModuleStruct);
           if hML > 0 then
             begin
               ShowMessage(ModuleStruct.szExePath);
             end;
           CloseHandle(hML);
         end;
      OK := Process32Next(hPL, ProcessStruct);
    end;
  CloseHandle(hPL);
end;

end.

0 0

相关博文

我的热门文章

img
取 消
img