How to make the application run only one instance

Source: Internet
Author: User

To make the application run only one instance, a simple method is to use mutex in the application class, which can be the GUIDGEN under VC. EXE program generation. GUIDGEN. EXE is located in the CommonTools directory of the VC installation directory.

Instance

1: Create a project ex1 Based on the dialog box and use the default settings.

2: Use GUIDGEN. EXE to generate a global flag, # define one "generated global flag"

The statements in this example are as follows: # define one "0xbe8e2ce1, 0xdab6, 0x11d6, 0xad, 0xd0, 0x0, 0xe0, 0x4c, 0x53, 0xf6, 0xe6"

3: In the application class CEx1App: InitInstance (), use the CreateMutex function to create a mutex, and then call the GetLastError () function ()

If the result is equal to ERROR_ALREADY_EXISTS, it indicates that an instance is running and FALSE is returned. BOOL CEx1App: InitInstance () {handle =: CreateMutex (NULL, FALSE, one); // handle is the declared global variable of the HANDLE type if (GetLastError () = ERROR_ALREADY_EXISTS)

{

AfxMessageBox ("the application is already running ");

Return FALSE;

}

}

4: In CEx1App: ExitInstance (), delete the mutex int CEx1App: ExitInstance (){

CloseHandle (handle );

Return CWinApp: ExitInstance ();

}

& Nbs

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.