findwindow和创建互斥对象的方法;但是这两个办法的缺点就是对同一操作系统;
不同的登陆用户就可以重复启动这个程序;怎么样能保证操作系统只存在一个这样的进程;应该是启动程序的时候先到系统进程列表中去找是否存在这个进程;如果存在则不启动?如何做?

解决方案 »

  1.   

    var
      hMutex : Thandle;
      WaitResult : word;
      BroadcastList : DWORD;
    begin
         MessageID := RegisterWindowMessage('Check For Choice Previous Inst');
    // register a message to use later on
         hMutex := createMutex(nil,false,pchar('App_Choice')); // grab a mutex
    handle
         WaitResult := WaitForSingleObject(hMutex,10); // wait to see
    if we can have exclusive use of the mutex
         if ( waitResult = WAIT_TIMEOUT ) then // if we can't then broadcast
    the message to make the owner of the mutex respond     { request that the running application takes focus }
           begin
              BroadcastList := BSM_APPLICATIONS;
              BroadcastSystemMessage(
    BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0); //32 bit - broadcast the
    message to all apps - only a prev inst will hear it.
           end
         else
          begin
          { do the normal stuff}
          Application.Title := 'Choice Organics Purchase & Sales System';
          Application.CreateForm(TMainForm, MainForm);
          Application.Run;
          ReleaseMutex(hMutex); // release the mutex as a politeness      end;
          CloseHandle(hMutex); // close the mutex handle
    end.This goes in the MainFormprocedure Tmainform.OnAppMessage(var Msg : TMsg ; Var Handled : Boolean);
    begin
    { If it's the special message then focus on this window}
    if Msg.Message = MessageID then // if we get the broadcast message from an
    another instance of this app that is trying to start up
       begin
          show;
          WindowState := wsMaximized;
          BringToFront;
          SetFocus;
          Handled := true;   end;
    end;//And this goes in the TMainForm.FormCreate ;-Application.OnMessage:= OnAppMessage;
      

  2.   

    如何防止一个程序执行两次这是一个比较简单的防止程序执行两次的方法implementationvar hnd: THandle;initializationhnd := CreateMutex(nil, True, 'irgendwaseinmaliges');if GetLastError = ERROR_ALREADY_EXISTS then Halt;finalizationif hnd <> 0 then CloseHandle(hnd);end.
      

  3.   

    为什么大家总是说废话呢,我说了两遍了
    CreateMutex创建互斥对象的方法和findwindow的API这两个方法对
    远程登陆操作系统的两个不同用户根本不起作用
    我登陆多少次就启动多少个
    怎么后面的还说这两个愚笨的方法
      

  4.   

    全局原子法 
      我们也可以利用向系统添加全局原子的方法,来防止多个程序实例的运行。全局原子由Windows 系统负责维持,它能保证其中的每个原子都是唯一的,管理其引用计数,并且当该全局原子的引用计数为0时,从内存中清除。我们用GlobalAddAtom 函数向全局原子添加一个255个字节以内的字符串,用GlobalFindAtom来检查是否已经存在该全局原子,最后在程序结束时用GlobalDeleteAtom函数删除添加的全局原子。示例如下: 
      Uses Windows 
      const iAtom=‘SingleApp’; 
      begin 
       if GlobalFindAtom(iAtom)=0 then 
       begin 
       GlobalAddAtom(iAtom); 
       Application.Initialize; 
       Application.CreateForm(TForm1,Form1); 
       Application.Run; 
       GlobalDeleteAtom(GlobalFindAtom(iAtom)); 
       end 
       else 
       MessageBox(0,‘You can not run a second copy of this App’,‘’,mb_OK); 
      end. 
      利用全局原子的引用计数规则,我们还可以判断当前共运行了该程序的多少个实例: 
      var i:Integer; 
      begin 
       I:=0; 
      while GlobalFindAtom(iAtom)&lt;&gt;0 do 
       begin 
       GlobalDeleteAtom(GlobalFindAtom(iAtom)); 
       i:=i+1; 
       end; 
       ShowMessage(IntToStr(I)); 
      end; 
    =====================================================================
    设置一个变量记录(INI文件啊,注册表啊),在程序运行前进行判断,条件符合就运行,不符合就不运行。