Several methods to achieve mutually exclusive program running
Methods for implementing mutually exclusive program running
1. Implementation under VC
Visual c ++ is the most important and powerful programming tool for Win32 programming. To cite a name, VC is "what you can't think of, what VC can't do ". To put it bluntly, let's take a look at how VC works.
A. Use a compiler (using the MFC framework ):
Add the following lines at the beginning of the. cpp file:
# Pragma data_seg ("shared") int volatile g_lappinstance = 0; # pragma data_seg ()
Let's take a look at the above content:
The first sentence # pragma data_seg ("shared") creates a new section called shared.
Int volatile g_lappinstance = 0: Put g_lappinstance in the shared section. Note that the compiler will put g_lappinstance into the shared section only when g_lappinstance is initialized. Otherwise, it will be placed in a section other than shared. (In fact, the visual C ++ compiler provides an allocate specifier that allows us to put data in any section .)
The third sentence indicates that the compiler shared section ends.
# Pragma comment (linker, "/section: shared, RWS") makes the compilation linker know that our shared section has read, write, and shared attributes. This is the key to implementing mutex operations. In this way, the g_lappinstance variable can be shared among multiple instances of the application.
Add the following to the initinstance () function:
If (++ g_lappinstance> 1) {afxmessagebox ("the program is running! "); Return false;} The above code is used to add 1 to g_lappinstancd at the beginning of the program. If the value is found to be greater than 1, the message box is displayed and false is returned. (Note: In MFC, if initinstance returns false, the program will not be run !)
B. Use the naming mutex object:
Using the API function createmutex to create a named mutex object to implement program mutex is a common method. We can add the following code to the ininininstance () function:
Handle hobject = createmutex (null, false, "ljpxyxc"); If (getlasterror () = error_already_exists) {closehandle (hobject); afxmessagebox ("the program is already running! "); Return false;} The createmutext function above creates a mutex object named" ljpxyxc ". When the second instance of the program is run, createmutex is called to return error_already_exists. We can use this value to implement mutually exclusive program running.
Ii. VB implementation
In programs implemented by VB, The Global Object app. preinstance can implement this function. App. preinstance is a Boolean value. If the value is true, it indicates that an instance of the program is running. If the value is false, the program can run.
We can program the loading event of the form to implement this function. The Code is as follows:
Private sub formateload () If app. preinstance = true then msgbox "the program is running !" Unload me end ifend sub 3. Implementation of Delphi and C ++ Builder
I have not found that the compilers of Delphi and C ++ builder have the same functions as VC. So we can call createmutex to implement this function.
For details about the functions createmutext () and getlasterror (), refer to msdn.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/linyaoxin/archive/2009/01/12/3760955.aspx