Solved

Prevent launching more than one copy of an application using WinExec().

Posted on 2000-02-23
9
363 Views
Last Modified: 2013-11-20
Hi All,
I have a 32 bit MFC application(e.g. MainProg.exe) which launches another executable(e.g. Download.exe) using WinExec() method. I would like to have multiple copy of my MainProg.exe but only one copy of Download.exe. The problem is everytime I start my MainProg.exe, it launches a copy of Download.exe. It does not matter whether I use a SDI, MDI, or Dialog Based application for Download.exe, my MainProg.exe always launches a copy of Download.exe using WinExec(). The same project in 16 bit works fine.

Is there a way to limit launching only one copy of Download.exe even I start multiple copis of MainProg.exe in Win32 platform? Thanks,

EKarim
0
Comment
Question by:EKarim
  • 4
  • 4
9 Comments
 

Expert Comment

by:fangmeier
ID: 2552663
Hi,

With 32-bits applications, it's more complicated to detect already running instances of a program, since each copy runs in a separate address space.
A common approach to this problem is to use the FindWindow() API:
Start "Download.exe" and use SpyXX to get it's main window class and title.
Then from MainProg.Exe, use FindWindow() with these parameters to check for the presence of the Download.exe main window.

HTH, Werner
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2554747
Hmm.......

You should prevent this in download.exe.

Better way is constructing a Mutex:

hMutex = CreateMutex(NULL, FALSE, "MYMUTEX");
if(WAIT_TIMEOUT==WaitForSingleObject(hMutex,NULL))
{
ReleaseMutex(hMutex);      
return 0;
}      
//Here download.exe will exit if there already have one ...

Regards
W.Yinan
0
 

Author Comment

by:EKarim
ID: 2554959
Hi Fangmeier,

I used the SPYXX.exe and got the different class names when I have two Download.exe running:

1st Download.exe>>
Windows Caption: -Download
Class Name : Afx:400000:b:1456:6:6927

2nd Download.exe>>
Windows Caption: -Download
Class Name : Afx:400000:b:1456:6:6777

Looks like the class name is not unique. Will I be able to use FineWindow() using the above information? I remember it did not work for me when I tried last time using the Windows Title. Thanks,

EKarim

0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 5

Expert Comment

by:Wyn
ID: 2555215
Ekarim,dont you read my comment?
0
 

Author Comment

by:EKarim
ID: 2555302
Hi Wyn,

Thanks for your comment. Are you saying that I should use CreateMutex() in Download.exe or MainProg.exe where I call WinExec() to launch Download.exe? Do I need to use CreateProcess() before I use CreateMutex()? Also, can the mutex-object(third parameter) name be any name? I did not use CreateMutex() before. Could you please answere a bit more? Regards,

EKarim
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2556962
Okay,let me answer your question in details:

->Are you saying that I should use CreateMutex() in Download.exe or MainProg.exe where I call WinExec() to launch Download.exe?
========================

You should use CreateMutex() AT BEGINNING of Download.exe.
 

->Do I need to use CreateProcess() before I use CreateMutex()?
=========================
They are in different apps.
CreateProcess() in MainProg.exe
CreateMutex() in download.exe

You should not change any thing except add code I give you to the download.exe.
The code I wrote only has one purpose:
Prevent the second copy download.exe being executed.(More accurate,when the second copy of download.exe begin executing,it will exit if there is already one copy).

->Also, can the mutex-object(third parameter) name be any name?
==========================
Yes.

Here is a sample for download.exe//

Winmain()
{

hMutex = CreateMutex(NULL, FALSE, "MYMUTEX");
if(WAIT_TIMEOUT==WaitForSingleObject(hMutex,NULL))
{
ReleaseMutex(hMutex);
return 0; //Here download.exe will exit if there already have one.
}

//otherwise ,it's first instance of  download.exe and it will continue to execute.

***********(your code here begins)
***********

}


Try it.I always use this way to prevent second copy being launched.


Regards
W.Yinan
0
 

Author Comment

by:EKarim
ID: 2558436
Hi Wyn,

Thank you very much. It works fine. I do not have WinMain() but InitInstance(). My last question before I close this problem is that is it all right to write the code at top of InitInstance() method?
0
 
LVL 5

Accepted Solution

by:
Wyn earned 75 total points
ID: 2558543
Yes.

If my suggestion works,plz take mine as answer.This way will close the question.
Regards
W.Yinan
0
 

Author Comment

by:EKarim
ID: 2559716
Hi Wyn,
It works for me. Thanks for your help.

EKarim
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question