473,473 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

single instance only at a time

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

Feb 9 '07 #1
6 5502
On Feb 9, 4:04 pm, "perera" <janaka.perera2...@gmail.comwrote:
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

Feb 9 '07 #2
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

Feb 9 '07 #3
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

Feb 9 '07 #4
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

Feb 9 '07 #5
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
Feb 9 '07 #6
* 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?
Feb 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steve Barnett | last post by:
I want to ensure that there is only ever one instance of my app running on a single PC at any time. I understand that I can achieve this by using a mutex and, if I can't take ownership of the...
7
by: jsale | last post by:
I have made an ASP.NET web application that connects to SQL Server, reading and writing data using classes. I was recommended to use session objects to store the data per user, because each user...
7
by: Maileen | last post by:
Hi, I've read several threads about 1 single instance of application and I tried them but without sucess. Does anyone have a good example for making the main Form as single instance application...
9
by: MrSpock | last post by:
1. Create a new Windows Application project. 2. Open the project properties and check "Make single instance application". 3. Build. 4. Go to the release folder and run the application. 5. Try to...
3
by: Mark Jerde | last post by:
VS 2005. When I google "CSharp single instance form" the returned pages usually use a Mutex or the VB.NET runtime library....
6
by: buffinator | last post by:
I have two applications that should work together, let's call them A and B. The first time A starts, it should open a B process and start communicating with it. All other times an A instance...
13
by: JohnQ | last post by:
Why would anyone write: class SomeThing // class littered with non-domain single-instancing code :( { private: SomeThing(); static SomeThing* pInstance_; public: static SomeThing*...
1
by: =?Utf-8?B?UkY=?= | last post by:
Our client made a Vb.Net application which is configured to start as a Single instance application. Calling this application a second time with a specific command line argument will execute the...
3
by: sklett | last post by:
I suspect the answer might be in one of the words of my subject, but here goes anyway. I'm working on a system that will execute a very long (300+) chain of task objects. Here is some quick...
7
by: =?Utf-8?B?YWVzcGVy?= | last post by:
Is there such a thing as a single instance web service? Is it possible to have all calls to a web service share one property value by declaring it as static? I know web service should be stateless,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.