Solved

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

Posted on 2000-02-23
9
362 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
 
LVL 5

Expert Comment

by:Wyn
ID: 2555215
Ekarim,dont you read my comment?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now