Solved

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

Posted on 2000-02-23
9
370 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

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…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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