我现在碰到这样一个问题,我想判断指定目录下的EXE的执行文件是否执行,如果执行,提示用户关闭程序,否则把指定目录下的最新程序覆盖掉哪个EXE文件。

解决方案 »

  1.   

    通过编写标题为“关闭已开启的外部应用程序”组件的OnClick事件,来关闭已开启的外部应用程序。代码如下:procedure TForm1.Button2Click(Sender: TObject);varhWndClose: HWnd; file://存储指定的外部应用程序窗口句柄str: String; file://存储指定的外部应用程序的窗口名beginstr := InputBox('提示','请输入应用程序名:',''); file://获取要关闭的应用程序窗口名if str <> '' then beginfile://根据窗口名查找要关闭的窗口句柄hWndClose := FindWindow(nil, PChar(str));if hWndClose <> 0 then file://如果查找成功,则发送消息,关闭指定的窗口SendMessage(hWndClose,WM_CLOSE,0,0);else file://否则,给出提示信息ShowMessage('没找到指定的应用程序,所以无法关闭!');end;end;
      

  2.   

    var
      pe:PROCESSENTRY32;
      me:MODULEENTRY32;
      hp,hm:Thandle;
      b,b1:boolean;
    begin
      hp:=CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS,0);
      pe.dwSize:=SizeOf(pe);
      b:=Process32First(hp,pe);
      while  b do
      begin
        hm:=CreateToolHelp32SnapShot(TH32CS_SNAPModule,pe.th32ProcessID);
        me.dwSize:=sizeof(ModuleEntry32);
        if Module32First(hm,me) then
        begin
        b1:=Module32First(hm,me);
        while  b1 do
        begin
    //      if me.th32ModuleID=pe.th32ModuleID then
            listbox1.Items.Add(copy(me.szExePath,length(extractfilepath(me.szExePath))+1,length(me.szExePath)-length(extractfilepath(me.szExePath))));  //移到路径只要文件名;
          b1:=Module32Next(hm,me);
        end;
      end;
        b:=Process32Next(hp,pe);
    end;这段代码可以列出当前所有在运行的EXE文件和DLL文件;
    你在LISTBOX1中查找是否有你想要查找的文件(如DELPHI32.EXE),用一个循环就可搞定;
    for i:=0 to listbox1.items.count-1 do
       begin
           if listbox1.items[i]='delphi32.exe' then
              begin
                 存在,执行存在代码;
                 break;
              end
           else
              不存在;
       end;
      

  3.   

    1、CreateMutex函数创建互斥关系。
    2、或者用WinApi函数寻找当前运行的程序中是否有目标应用程序,FindWindows应该可以,如何使用参看Delphi帮助或者MSDN。
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, TLHelp32, Controls, Forms, Dialogs,
      StdCtrls;type
      TProcessInfo = record
        ExeFile: string;
        ProcessId: DWORD;
      end;
      ProcessInfo = ^TProcessInfo;
      TForm1 = class(TForm)
        ListBox1: TListBox;
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure ProcessList(var pList: TList);
        procedure My_RunFileScan(ListboxRunFile: TListBox);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        Current: TList;
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ProcessList(var pList: TList);
    var
      p: ProcessInfo;
      ok: Bool;
      ProcessListHandle: THandle;
      ProcessStruct: TProcessEntry32;
    begin
      PList := TList.Create;
      PList.Clear;
      ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      ProcessStruct.dwSize := Sizeof(ProcessStruct);
      ok := Process32First(ProcessListHandle, ProcessStruct);
      while Integer(ok) <> 0 do
        begin
          new(p);
          p.ExeFile := ProcessStruct.szExeFile;
          p.ProcessID := ProcessStruct.th32ProcessID;
          PList.Add(p);
          ok := Process32Next(ProcessListHandle, ProcessStruct);
        end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      h: THandle;
      a: DWORD;
      p: PRocessInfo;
    begin
      if ListBox1.ItemIndex >= 0 then
        begin
          p := Current.Items[ListBox1.ItemIndex];
          h := openProcess(Process_All_Access, true, p.ProcessID);
          GetExitCodeProcess(h, a);      if Integer(TerminateProcess(h, a)) <> 0 then
            begin
              My_RunFileScan(ListBox1);
            end;
        end
      else
        Application.MessageBox('请先选择一个进程!', '黑洞', MB_ICONERROR + MB_OK);
    end;procedure TForm1.My_RunFileScan(ListboxRunFile: TListBox);
    var
      i: Integer;
      p: PRocessInfo;
    begin
      current := TList.Create;
      Current.Clear;
      ListboxRunFile.Clear;
      ProcessList(Current);
      for i := 0 to Current.Count - 1 do
        begin
          new(p);
          p := Current.Items[i];
          ListboxRunFile.Items.Add(p.ExeFile);
        end;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      My_RunFileScan(ListBox1);
    end;end.