各位大虾,请问个问题,我想在程序中加个功能,就是在运行的时候,先判断是否已经运行了程序的实例,如果是那么就不运行,如果没有运行实例,那么就运行。
   比如vb中的App.PrevInstance这个属性的功能,该属性如果为ture,那么内存中就有该程序的实例,如果为false,内存中没有该程序的实例。
   我这样说,不知道你们是否明白了文中的意思。

解决方案 »

  1.   

    program   Project1;   
        
      uses   
          Forms,windows,   
          Unit1   in   'Unit1.pas'   {Form1};   
        
      var   hw:hwnd;   
        
      {$R   *.RES}   
      begin   
          Application.Initialize;   
          application.title:='程序只能运行一次';//名字自己定义   
          CreateMutex(nil,   false,   'ADManager');   
          if   GetLastError   <>   ERROR_ALREADY_EXISTS   then   
          begin   
              Application.CreateForm(TForm1,   Form1);   
              Application.Run;   
          end;   
      end.
      

  2.   

    这样也行function checkAppExists(appN: string): wordbool;
    var
      processStr: tstringlist;
      i, j: integer;
    begin
      result := false;
      j := 0;
      processStr := tstringlist.create;
      try
        processStr := GetNowProcesses;
        for i := 0 to processStr.Count - 1 do
        begin
          if uppercase(processStr.Strings[i]) = uppercase(appN) then
          begin
            inc(j);
            if j > 0 then
            begin
              result := true;
              break;
            end;
          end;
        end;
      finally
        processStr.Free;
      end;
    end;
    ////
    //
      if checkSelfExists then
      begin
        showmessage(application.ExeName + '已经运行');
      end
      else
      begin
        Application.Initialize;  end