468,457 Members | 1,779 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Prevent starting up twice

Hello,

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

Thanks,
Dirk


Jul 22 '05 #1
8 2425
"Dirk" <so*****@nomail.xx> wrote in message
news:Gq********************@casema.nl...
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.

--
Unforgiven

Jul 22 '05 #2
Dirk wrote:
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
Jul 22 '05 #3
Victor Bazarov posted:
Dirk wrote:
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


For Windows:

go to msdn.microsoft.com and look up "CreateMutex".
-JKop
Jul 22 '05 #4
On Mon, 20 Sep 2004 14:28:23 +0200, "Dirk" <so*****@nomail.xx> wrote:
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
}

Jul 22 '05 #5

"Terry Liittschwager" <te***@epud.net> wrote in message
news:5l********************************@4ax.com...
On Mon, 20 Sep 2004 14:28:23 +0200, "Dirk" <so*****@nomail.xx> wrote:
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
}


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

Jul 22 '05 #6
Mike Wahler wrote:
"Terry Liittschwager" <te***@epud.net> wrote in message
news:5l********************************@4ax.com...
On Mon, 20 Sep 2004 14:28:23 +0200, "Dirk" <so*****@nomail.xx> wrote:
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
}

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

--
Mike Smith
Jul 22 '05 #7

"Dirk" <so*****@nomail.xx> wrote in message news:Gq********************@casema.nl...
Hello,

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


Fix it so that it doesn't start up once.

Jul 22 '05 #8
Dirk wrote:

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.
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by entoone | last post: by
5 posts views Thread by R. Ian Lee | last post: by
6 posts views Thread by Arthur Dent | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.