I often encounter someone asking how to ensure that only one instance is running. It turns out that I have gave two or three ways. Today, I simply come to a big summary, I hope to help everyone when they are designed.
A program only runs an instance (or limit instance) can usually take the following method:
1) FindWindow
By finding a window title to determine if the previous instance is running, not suitable for a dynamic change of window titles.
2) FindWindow
By looking for a taskbar pressing the title to determine if the previous instance is running, not suitable for dynamic changes in the new title (such as Winamp). Normally, the method is still prioritized because pressing the title is generally fixed.
3) WINDOW Property
Add a data (which can be a string or handle) to add a Property List of the specified window via SetProp, the program runtime enumeration window and check if the data exists to determine if the previous instance is running.
4) Global Atom
Add a particular string to the global atomic table (Global Atom Table), and the program is run when the program is running to determine if the previous instance is running. This method has a limitations, which must explicitly call GlobalDeleTeatom before the program is terminated to release the Atom, otherwise the atom will not be released automatically. If the program is unexpected, the next instance cannot be executed normally. There is this phenomenon in the early version of RealPlayer, and I don't know if it is used.
5) MUTEX / EVENT / SEMAPHORE
Determine if the instance is existing by a thread synchronization object such as a mutually exclusive object / semaphore / event.
6) DLL global sharing area
The DLL project under the VC can create a process-shared data segment by the following code:
#pragma data_seg (". Share")
// Shared for All Processes That Attach To The DLL
DWORD DLLGS_DWRUNCOUNCOUNT = 1; / / must initialize the variable here, otherwise the work is white!
#pragma data_seg ()
#pragma comment (Linker, "/ Section: .Share, RWS")
Export 3 functions, respectively:
DWORD INCRUNCOUNT (VOID); // Run counter plus 1, return counter results
DWORD DECRUNCOUNT (VOID); // Run counter minus 1, return counter results
DWORD GETRUNCOUNT (VOID); // Take the current Run counter
Since the DLL global sharing segment is only initialized once when it is mapped to each process address, and is the first time by Windows, the program instance can be reliably count.
7) Memory mapping file (file maping)
You can also control the number of program instances running by placing program instance information (such as window handle, counter, etc.) to a cross-process, and the truth is similar to the DLL global sharing area.
8) Others
I have seen some people to process instance control issues through registry, disk file, etc., but because these reference objects are non-volatile resources, they are quite troublesome when they come to the procedure from normal end and have not cleared instance identifiers. It has great limitations.