Archived

This topic is now archived and is closed to further replies.

Zimond

uhhm... prevent apps from starting...

Recommended Posts

Zimond    158
i have a game with three editors... all different EXEs i have to get sure that only one of these is executed at once, means : if one of these is active another one shell detect this and close itself (with giving an error message) any suggestions?

Share this post


Link to post
Share on other sites
MButchers    123
Hi there,

Use the following

var
Mutex: THandle;
begin
Application.Initialize;
Application.Title := ''AppTitle'';
//stop more than one instace running at a time
Mutex := CreateMutex(nil,True,PChar(''AppTitle''));

if GetLastError <> ERROR_ALREADY_EXISTS then
begin
Application.CreateForm(TfrmMain, frmMain);
end else begin
MessageDlg(''AppTitle program already running, application will be terminated'',mtError,[mbOk],0);
end;

ReleaseMutex(Mutex);
Application.Run;
end.

Share this post


Link to post
Share on other sites
Zimond    158
uhmmm.. wtf is a mutex?
and this seems to prevend only an application from starting it self more than one time.. i need something that prevends it from starting when a completely diffent application is running

Share this post


Link to post
Share on other sites
Sly    128
Mutex is short for MUTual EXclusion. A mutex object is often used to synchronize access to shared resources. A mutex has a name associated with it. If you try to create a mutex with a name that already exists, you get a handle to the original mutex and the value returned by GetLastError is set to ERROR_ALREADY_EXISTS. So, if you use the same mutex name in all three applications, then they will not start if any of the three applications is already running.

I would also change the code to this. Note the addition of the CloseHandle() call to free the mutex.

var
Mutex: THandle;
begin
// Create a mutex with a given name
Mutex := CreateMutex(nil, True, ''Zimond'');
// If the mutex did not already exist, then run normally
if GetLastError <> ERROR_ALREADY_EXISTS then
begin
Application.Initialize;
Application.CreateForm(TfrmMain, frmMain);
Application.Run;
end;
// Free the mutex and any resources used by it
CloseHandle(Mutex);
end.

Note that all three applications MUST use the same name in the call to CreateMutex for this to work properly.

Steve ''Sly'' Williams  Monkey Wrangler  Krome Studios

Share this post


Link to post
Share on other sites