Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > single instance only at a time

Reply
Thread Tools

single instance only at a time

 
 
perera
Guest
Posts: n/a
 
      02-09-2007
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

 
Reply With Quote
 
 
 
 
perera
Guest
Posts: n/a
 
      02-09-2007
On Feb 9, 4:04 pm, "perera" <(E-Mail Removed)> wrote:
> I want to create a MFC program *.exe. My reguirement is at a time only
> one instance can be run.
> In Other words no mutile instnces can be run at a time



 
Reply With Quote
 
 
 
 
abadura
Guest
Posts: n/a
 
      02-09-2007
> 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

 
Reply With Quote
 
abadura
Guest
Posts: n/a
 
      02-09-2007
> 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

 
Reply With Quote
 
abadura
Guest
Posts: n/a
 
      02-09-2007
> 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

 
Reply With Quote
 
Haro Panosyan
Guest
Posts: n/a
 
      02-09-2007
perera wrote:
> 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



> In Other on mutile instnces can be run at a time
>

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-09-2007
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Failed to generate a user instance of SQL Server. Only an integratedconnection can generate a user instance. Harlan Messinger ASP .Net 2 03-28-2010 06:51 PM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Making sure script only runs once instance at a time. Hari Sekhon Python 11 10-03-2006 01:50 PM
Custom Taglib problems - instead of a single instance per page, I have a single instance per application. chris brat Java 1 05-10-2006 11:16 AM
Limit Thunderbird to only one instance running at a time? Z Firefox 2 05-02-2005 09:01 AM



Advertisments