?
Solved

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

Posted on 2000-02-23
9
Medium Priority
?
372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
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 300 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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