single instance only at a time

P

perera

I want to create a MFC program *.exe. My reguirement is at a time only
one instance can be run.
In Other on mutile instnces can be run at a time
 
A

abadura

I want to create a MFC program *.exe. My reguirement is at a time only
one instance can be run.

Try using mutexes (this method is described along WinAPI WinMain
function).

Add to yout CWinApp a HANDLE member variable. Lets name it like
this:

private:
HANDLE m_hOneInstanceMutex;

and initialize it to NULL in constructor.
In your CWinApp InitInstance place at the very beging a code like
this:

hOneInstanceMutex = CreateMutex(NULL, TRUE, UNIQE_NAME);
if(hOneInstanceMutex == NULL) {
if(GetLastError() == ERROR_ALREADY_EXISTS) {
// one instance is already running so display a message or
something like this and exit
return 0;
}
// there was an error, do what you want but it is not clear if this
is first instance or not
}
// if execution comes here then you are the first and only instance of
your application

Add the code at the begining because there is no point in
initialization of anything if you are the exiting because another
instance is already running.
You don't have to delete the mutex while exiting because system
will do it for you (as described in CreateMutex specification).

Choose UNIQE_NAME wisely because if you use a name already used by
antoher mutex (another application) you will end being not able to run
your application (not that a "hacker" can prevent your application
from runnig in this way - as described in WinMain specification).

Also I think question has more common with WinAPI and MFC than C++
so perhaps the choice of group you made was not the best one.

Adam Badura
 
A

abadura

Try using mutexes (this method is described along WinAPI WinMain
function).

MFC has a class CMutex but I am not quite sure if it can be used in
this problem - and I suspect that it cannot.
Add to yout CWinApp a HANDLE member variable. Lets name it like
this:

private:
HANDLE m_hOneInstanceMutex;

Since system automaticly releases the handle when the process exits
you can even simplify this by not storing the HANDLE! It is not needed
and so can be a local variable in InitInstance (actually you only need
to know if CreateMutex returns NULL or not - and tha actual handle is
useless in this problem).

Adam badura
 
A

abadura

Try using mutexes (this method is described along WinAPI WinMain
function).

MFC has CMutex class but I think it cannot be used here for this
purpose.
Add to yout CWinApp a HANDLE member variable. Lets name it like
this:

private:
HANDLE m_hOneInstanceMutex;

Since the handle is closed by the system after process exits and
you don't need it for nothing but to know if CreateMutex returned NULL
or not that the sollution can be simplified by omiting this member...

Adam Badura
 
H

Haro Panosyan

perera said:
I want to create a MFC program *.exe. My reguirement is at a time only
one instance can be run.

It has been long time (~8 years), so if my memory is right, there was an
example code in Visual C++ package. Try to find and if not, you can also
try to check all open windows names from within your first "init"
function and then if you find you own window, then send message to
activate the one you found and exit from current.

-haro
 
A

Alf P. Steinbach

* perera:
I want to create a MFC program *.exe. My reguirement is at a time only
one instance can be run.
In Other on mutile instnces can be run at a time

off-topic in clc++.

try posting the question in a Windows newsgroup.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top