Preventing an Application from Starting Multiple Times

Q. Is it possible to prevent an application from starting multiple times?

Yes, it is possible. The most frequently used method is to create a mutual exclusion object (mutex). This object is intended for coordinating the interoperation of various processes. To create a mutex, use the CreateMutex function. Parameters of this function are briefly outlined as follows :

  • First parameterThe pointer to the structure that defines an access attribute. Usually, this parameter is set to NULL .

  • Second parameterThe priority flag. If this flag has a nonzero value, the process requires immediate access to the object.

  • Third parameterThe pointer to the object name .

When the program is started, it creates a mutex. The second parameter of the function must have a nonzero value. When the user makes an attempt to start another copy of the program, the attempt to create a mutex will result in an error, which will cause the program to terminate immediately.

The same result can be achieved using a semaphore or a memory-mapped file. In this case, the task is trivial.

Another approach is based on shared memory. To use this method, define a shared memory area and a variable within it. When the application starts, it checks the variable value and, if it is equal to zero, sets it to one. If the variable is already set to one, then the program must either exit or carry out the actions, for which provision was made just for this case.

All methods described in this section are so easy that there is no need to concentrate special attention on them.



The Assembly Programming Master Book
The Assembly Programming Master Book
ISBN: 8170088178
EAN: 2147483647
Year: 2004
Pages: 140
Authors: Vlad Pirogov

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net