大家好:
    一个应用程序运行后,怎样才能防止它被第二次打开。
                                               王振亚
                                              2003.12.14

解决方案 »

  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.   

    if findwindow('主窗口的名称',nil)>0 then 程序已运行
    sele 程序未运行;你试试行不行,我只在本程序中用过!
      

  3.   

    主窗口的名称是什么东西?
    是name 还是caption?
      

  4.   

    就是你的form1.caption
    注意一个字也不要写错
      

  5.   

    在工程文件里面:
    var
      //...
      Mutex:THandle;
    begin
      Mutex:=CreateMutex(NIL,True,'HIS');
      if GetLastError<>ERROR_ALREADY_EXISTS then  //如果不存在另一实例
      begin
        Application.Initialize;
        Application.CreateForm(TMain, Main);
        //...
        //...
      end;
      ReleaseMutex(Mutex);
      

  6.   

    http://www.csdn.net/develop/read_article.asp?id=20379
      

  7.   

    我试过了,这个findwindow的方法好像不能用,我的程序是有splash的。
      

  8.   

    yurenjf(极限) 的方法最简单!
      

  9.   

    用yurenjf(极限)方法就对了,楼主可以揭贴了