Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If an installer program of an application is started , how the instance of the same application can be found if it is running already so that the installer stops and a warning screen comes as to close the current running application.

Can anyone help me with a code snippet or provide me some reference.
Posted

Did you try to find any by yourself? This is quite a popular topic. A different approaches can be used, with benefits and disadvantages.

Anyway, only on CodeProject:

Single Instance Application[^].

A very primitive minimal solution: To create single instance of application[^].

Via file mapping:
Implementing Single Instance Application Using “File Mapping” Technique[^].

For C++/CLI:
The Single Instance Class Library[^].

...would you like to search some more?

You may need to solve the following issues:
1) From and instance of the application, detect previously started instance of the same application; you need to decide what you consider the "same application"; the question is not so trivial, it can be the application started from exact same executable file; otherwise what's the same? It can be defined by some identical feature, such as Atom name...
2) Optionally, pass command line parameters to the first instance;
3) First instance must be able to receive some event from the second instance and activate its main window, not so trivial if a modal dialog is running;
4) Optionally, first instance must be able to receive command line parameters from the second instance and process them;
5) When communication of the second instance and the first instance is done, the second instance must terminate;
6) Optionally, sigle-instance behavior can be optional.

I'll need to find, understand and overview known techniques to understand what features exactly do you need.
 
Share this answer
 
Comments
mahadevank 18-Jan-11 1:09am    
Sakryukov > Thanks for the reply. Sorry that I was not so good in words. For example pls consider a Text editor viz. Sakura. Consider that Sakura is already running and now I am trying to install the sakurainstaller.exe and finding that the warning screen comes as close the running application for setup to run. I want to understand as how such code is acheived to find the running instance already.
Sergey Alexandrovich Kryukov 18-Jan-11 1:30am    
I understand: this is bit different, but techniques are the same. You best bet is to learn how single-instance behavior can be achieved anyway, because this is more popular topic but most techniques are good for your installer as well.
1. Create a mutex at the start of application. If it is already exist, terminate the new instance.

// For handling Single Instance of Application
HANDLE hSingleInstance = NULL;
hSingleInstance = CreateMutex( NULL, FALSE,_T("Name"));
DWORD dwLastError = GetLastError();

if( ERROR_ALREADY_EXISTS == dwLastError )
{
   AfxMessageBox(_T("Application already running"));
   CloseHandle(hSingleInstance);
   return 0;
}


2. If it is a window application, use FindWindow API to get application window. If FindWindow returns valid window pointer, terminate the new instance.

C#
CWnd* pWnd = FindWindow( 0, _T( "AppName" ));
if( 0 != pWnd )
{
    AfxMessageBox(_T("Application already running"));
    return 0;
}


Me too vote for option 1 (Mutex) :)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 1:27am    
Other methods: scan list of all process, establish pipe connection and exchange messages and more...
Sergey Alexandrovich Kryukov 18-Jan-11 12:01pm    
Same problem:
The problem with solutions like this is: how can you may sure you mutex or application name is really unique to this application? Using window name is worst, I think -- unacceptable.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 11:56am    
The problem with solutions like this is: how can you may sure you mutex name is really unique to this application? The text of the article you reference setup pretty bad example.
since people already given specific answers to this question, i just want to mention the names in general.

1.Mutex
2.File approch
3.Shared memory (using shared data segment )
3.searching window name
4.guys, any more ? :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 11:58am    
Yes, there are more... Searching window name is bad. There are problems with uniqueness.
Sergey Alexandrovich Kryukov 18-Jan-11 11:59am    
I up-vote to "4", I think "1" is a bit stupid (looks like a manifestation of reader's laziness)
One more link that i used in my project is
http://support.microsoft.com/kb/243953[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900