Make sure that only one instance of the program is running.

Source: Internet
Author: User

This is a very simple method. By creating a mutex object, you can also verify that an instance with this program is running.

 

 

Greatemutex function prototype:

Handle createmutex (
Lpsecurity_attributes lpmutexattributes,
Bool binitialowner,
Lptstr lpname
);

The first parameter, lpmutexattributes, is a pointer to a security_attributes structure, used to determine whether the sub-process is allowed to inherit the handle returned by the function. If this parameter is null, it cannot be inherited.

The second parameter, binitialowner. If this parameter is true and is created by the caller (who calls the createmutex function), then the called thread (the thread that calls the createmutex function) obtain initial ownership. In addition, the calling thread cannot obtain the original ownership. To determine whether a mutex object is created by the caller, see "return values.

The third function, lpname, points to the string ending with null (null) used to name the mutex object. The number of characters in this name is limited to the number of max_path characters. The name is case sensitive. If the lpname parameter is null (null), a mutex object without a name is created. If the lpname parameter matches any of the following strings: Existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and is accompanied by calling (as soon as possible) The getlasterror function returns the error_invalid_handle constant. The result is that duplicate namespaces are assigned to these mutex objects (these namespaces can be viewed by searching createmutex in msdn ).

Return value. If the function succeeds, the return value is the handle of a newly created mutex object. If the function fails, the return value is null ). If the obtained mutex object is a mutex object named before this function (before this call of the createmutex function) and exists, the returned value is the handle of the existing object, call the getlasterror function as soon as possible to return error_already_exists. In any case, when the second caller (Note 1) has limited access permissions, createmutex will fail and will return error_access_denied with getlasterror, then the caller should use the openmutex function (for more information, refer to msdn. Now, this information is enough for us to ensure that our program runs only one instance ).

Now I want to confirm the idea: first create a mutex object. If the object is successfully created (the return value of createmutex is not null) and the getlasterror function is called to return the error_already_exists constant, the current process is not the first instance of the application, end the program.

In VC, we use a Win32 application without a window or MFC as an example. Open vc6, create a project, select Win32 application as the type, and set the project name to onlyone. Click OK and select an empty project. Add a new C ++ source code file named onlyone. C to the project, and enter the following code:

# Include <windows. h>

Int
Winapi
Winmain (
Hinstance,
Hinstance hprevinstance,
Lpstr lpcmdline,
Int nshowcmd
)
{
Char strappname [] = "onlyone ";
Handle hmutex = NULL;

// Create a mutex object
Hmutex = createmutex (null, false, strappname );
If (hmutex! = NULL)
{
If (getlasterror () = error_already_exists)
{
MessageBox (null, text ("is not the first time to run this program. "), Text (" onlyone "), mb_ OK | mb_iconinformation );
// Close the mutex object and exit the program
Closehandle (hmutex );
Return (-1 );
} Else
{
MessageBox (null, text ("first run this program. "), Text (" onlyone "), mb_ OK | mb_iconinformation );
}
} Else
{
MessageBox (null, text ("failed to create the mutex object. "), Text (" onlyone "), mb_ OK | mb_iconinformation );
}
// Close the mutex object
Closehandle (hmutex );
Return (-1 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.