怎么控制程序只运行一个实例(使exe文件不被重复打开)

发表者:cool222

一种更有优势的办法:  
  工程文件中:  
  winapi   winmain(hinstance,   hinstance,   lpstr,   int)  
  {  
          try  
          {  
                  handle   mutex=openmutex(mutex_all_access,false,"chenicehonlyone");  
                    if   (mutex==null)  
                          mutex=createmutex(null,true,"chenicehonlyone");  
                    else  
                    {  
                            sendmessage(hwnd_broadcast,registerwindowmessage("cheniconlyoneeh"),0,0);  
                            return   0;  
                    }  
                    application->initialize();  
                    application->title   =   "考场英豪";  
                    application->createform(__classid(tform1),   &form1);  
                    application->createform(__classid(tfrmlogin),   &frmlogin);  
                    application->run();  
                  releasemutex(mutex);  
          }  
          catch   (exception   &exception)  
          {  
                    application->showexception(&exception);  
          }  
          return   0;  
  }  
   
  主窗体中:  
  void   __fastcall   tform1::formcreate(tobject   *sender)  
  {  
          msgrestore=registerwindowmessage("cheniconlyoneeh");  
          application->hookmainwindow(messagehandler);  
  }  
  //---------------------------------------------------------------------------  
  bool   __fastcall   tform1::messagehandler(tmessage   &message)  
  {  
          if   (message.msg==msgrestore)  
          {  
                  application->restore();  
                  application->bringtofront();  
                  return   true;  
          }  
          else   return   false;  
  }  
  //---------------------------------------------------------------------------  
  void   __fastcall   tform1::formdestroy(tobject   *sender)  
  {  
          application->unhookmainwindow(messagehandler);          
  }  
   
  我在这里做的只是简单的将应用程序提到前台,你大可以举一反三做一个提示或记录重复启动次数之类的事,也可以根据这个方法给程序注册一些消息,在程序间传递控制指令,实现多个程序间的互动。

 谢谢cool222提供的方法,摘录在这里供大家参考。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值