如何在程序启动时,判断是否已经有同一个.exe在运行?

chenwencheng 2003-05-14 05:53:00
防止用户打开两个相同的本应用程序
...全文
95 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
winterxu416 2003-05-16
  • 打赏
  • 举报
回复
关注互斥量
zbc 2003-05-16
  • 打赏
  • 举报
回复
体力活,呵呵
  • 打赏
  • 举报
回复
在你程序的WinMain的 try{ } 的上面加上:
HANDLE Mutex=OpenMutex(MUTEX_ALL_ACCESS,false,"OneInstanceAllowed");
if(Mutex==NULL)
{ Mutex=CreateMutex(NULL,true,"OneInstanceAllowed"); }
else
{ SendMessage(HWND_BROADCAST,RegisterWindowMessage("OnlyOne"),0,0);
return 0;//有多个程序实例,退出
}
偷的楼上的,用互斥量最保险、最快。
yingyys 2003-05-15
  • 打赏
  • 举报
回复
使用互斥量,同上
xiaoxing1978 2003-05-15
  • 打赏
  • 举报
回复
错了,是FindWindow
xiaoxing1978 2003-05-15
  • 打赏
  • 举报
回复
假设你的程序主窗口的caption为"exam"
if(Findow(NULL,"exam")!=0)
ShowMessage(该程序已运行");
chenwencheng 2003-05-14
  • 打赏
  • 举报
回复
太长啦,我还没吃晚饭,都晕了。有没有短一点的啊?
chenwencheng 2003-05-14
  • 打赏
  • 举报
回复
老兄,连第一个程序都自杀了啊。
Friecin 2003-05-14
  • 打赏
  • 举报
回复
哇!我来晚了~
Friecin 2003-05-14
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------

#include <vcl.h>
#include "FormSys.h"
#include "FormMain.h"
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("FormSys.cpp", frmSys);
USEFORM("FormMain.cpp", frmMain);
USEFORM("DataMod.cpp", DMod); /* TDataModule: File Type */
USEFORM("FormEdit.cpp", frmEdit);
。。。。

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
//这里创建一个互斥量(你就复制吧)!!!!!!!!!!!!!!!!!!!
HANDLE Mutex=OpenMutex(MUTEX_ALL_ACCESS,false,"OneInstanceAllowed");
if(Mutex==NULL) //this is the only instance
{
//Create the Mutex...
Mutex=CreateMutex(NULL,true,"OneInstanceAllowed");
}
else //this is not only instance
{
//Send a message to the previous instance of the application,and asking_
//it to restore and front itself.
SendMessage(HWND_BROADCAST,RegisterWindowMessage("OnlyOne"),0,0);
return 0;
}
//创建互斥结束

Application->Initialize();
Application->CreateForm(__classid(TDMod), &DMod);
Application->CreateForm(__classid(TfrmSys), &frmSys);
,GWL_STYLE);
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}
//---------------------------------------------------------------------------


工程主窗体中:
.h

public:

....
unsigned int msgRestore;
bool __fastcall MessageHandler(TMessage &Message);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DROPFILES, TMessage, DragDropFiles);
END_MESSAGE_MAP(TForm);
...

.cpp

//创建系统消息,用于接受Mutex(互斥量)发送的消息
bool __fastcall TfrmSys::MessageHandler(TMessage &Message)
{
if(Message.Msg==msgRestore)
{
if(frmMain)
{
frmMain->Show();
frmMain->WindowState=wsMaximized;
}
else
Close();

Application->Restore();
Application->BringToFront();
return true;
}
else
return false;
}
//---------------------------------------------------------------------------

//窗体的OnCreate
void __fastcall TfrmSys::FormCreate(TObject *Sender)
{
//注册Onlyone到Mutex,并且设置应用程序与MessageHandler的消息挂钩
msgRestore=RegisterWindowMessage("onlyone");
Application->HookMainWindow(MessageHandler);
}
codecb 2003-05-14
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("Unit1.cpp", Form1);
USERES("SingleInstance.res");
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
HANDLE mutex ;
try
{
const char mutexname [] = "SingleInstanceProgram" ;
// See if the mutex already exists.
mutex = OpenMutex (0, false, mutexname) ;
if (mutex == NULL)
{
// The mutex is not there. This means another instance of the program is
// not already running. Create the mutex so any other instance of the
// program will know that this one is already running.
mutex = CreateMutex (NULL, true, mutexname) ;
}
else
{
// The mutex exists so another instance of the program is running
// OR some other program created a mutex called "SingleInstanceProgram".
ShowMessage ("Application Already Running") ;
// Returning from WinMain terminates the program.
return 0 ;
}

Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
ReleaseMutex (mutex) ;
return 0;
}
//---------------------------------------------------------------------------

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧