Prevent starting up twice

D

Dirk

Hello,

Can anyone tell me what the best way is to prevent an application from
starting up twice.

Thanks,
Dirk
 
U

Unforgiven

Dirk said:
Hello,

Can anyone tell me what the best way is to prevent an application from
starting up twice.

This is off-topic in this group, as standard C++ defines no such mechanism.
It usually involves things such as creating named pipes and checking if that
pipe exists or not, stuff like that, but it is usually far from trivial and
highly platform specific.

Ask again in a newsgroup for your specific OS.
 
V

Victor Bazarov

Dirk said:
Can anyone tell me what the best way is to prevent an application from
starting up twice.

You cannot prevent it from starting up twice if your OS allows that.
However, you can try to avoid letting your application proceed far
beyond the startup code if you can determine that another copy of your
application is already running. The only standard C++ way I know is
to check if there exists a file in a predetermined location and when
the application starts, and if it does, exit, and if it doesn't, create
one to indicate that your application is the first one. The application
that creates the file shouldn't forget to delete it when it closes.

If you want something that doesn't involve creating files, you need
a platform-specific solution. Ask in a newsgroup dedicated to your
platform.

Victor
 
J

JKop

Victor Bazarov posted:
You cannot prevent it from starting up twice if your OS allows that.
However, you can try to avoid letting your application proceed far
beyond the startup code if you can determine that another copy of your
application is already running. The only standard C++ way I know is
to check if there exists a file in a predetermined location and when
the application starts, and if it does, exit, and if it doesn't, create
one to indicate that your application is the first one. The application
that creates the file shouldn't forget to delete it when it closes.

If you want something that doesn't involve creating files, you need
a platform-specific solution. Ask in a newsgroup dedicated to your
platform.

Victor

For Windows:

go to msdn.microsoft.com and look up "CreateMutex".


-JKop
 
T

Terry Liittschwager

Can anyone tell me what the best way is to prevent an application from
starting up twice.

If you're using Windows, here's the code generated by VC++ to prevent
a second instance of an application named WWB:

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if (hPrevInstance)
{
MessageBox(0,"WWB is already running",NULL,MB_OK);
return FALSE; // previous instance not allowed
}
 
M

Mike Wahler

Terry Liittschwager said:
If you're using Windows, here's the code generated by VC++ to prevent
a second instance of an application named WWB:

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if (hPrevInstance)
{
MessageBox(0,"WWB is already running",NULL,MB_OK);
return FALSE; // previous instance not allowed
}

This gives an example of why it's not a good idea to
ask or answer off topic questions. This off topic
answer is incorrect. See a Windows newsgroup and/or
MSDN for further information.

-Mike
 
M

Mike Smith

Mike said:
This gives an example of why it's not a good idea to
ask or answer off topic questions. This off topic
answer is incorrect. See a Windows newsgroup and/or
MSDN for further information.

Well, since we're all indulging on OTness - the posted answer is correct
for 16-bit Windows (i.e. pre-Win95), but not Win32. ;-P
 
J

Julie

Dirk said:
Hello,

Can anyone tell me what the best way is to prevent an application from
starting up twice.

When the app starts, open a predefined (temporary) file in exclusive mode*. If
it succeeds, continue; if it fails, terminate. Close the file when exiting.

*Presumes that your OS and C/C++ library supports exclusive file access.

Other than that, you will need to resort to more OS specific solutions -- find
a newsgroup that discusses your OS and post the question there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top