Solved

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

Posted on 2000-02-23
9
360 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 31
repeatEnd java challenge 42 83
withoutTen challenge 14 88
Change to event 1 73
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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 …
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

24 Experts available now in Live!

Get 1:1 Help Now