"Go" MFC prohibits multiple instances

Source: Internet
Author: User

Windows is a multi-process operating system, and a framework-generated application can run multiple times to form several running instances. In some cases, however, in order for the application to run safely, a program can only run one instance, such as a program that uses special hardware (such as modems) that can be used alone by a single process, and must restrict the program from running only one instance.
Here are two basic questions, one is how to discover when the second instance of the program is started, how the program has an instance running, but how to activate the first instance, and the second instance exits.

For the first problem, you can set the semaphore to the application, and the instance starts by detecting the semaphore first, and if it already exists, the program is running an instance. The difficulty of the second problem is to get the main window object pointer or handle of the first instance and then use SetForegroundWindow to activate it. Although the FindWindow function can look for a window that is running, the function requires that you indicate the title or window class name of the window you are looking for, not the way to implement a generic method. We can use the Win + SDK function SetProp to set a unique tag for the application main window. With GetDesktopWindow you can get the Windows System Master window object pointer or handle, all application main windows can be considered as child windows of the window, you can use the GetWindow function to get their object pointer or handle. Use the Win + SDK function Getprop to find out if each application main window contains a specific tag we set to determine whether it is the first instance of the main window we are looking for. Making the second instance exit is simple, just let the InitInstance function of its Application object return False. Also, when the main window exits, apply the Removeprop function to remove the markup that we set for it.

The following InitInstance, OnCreate, and OnDestroy function codes will do the above:
BOOL cellipsewndapp::initinstance ()
{
Create semaphores with application name
HANDLE Hsem = CreateSemaphore (NULL, 1, 1, m_pszexename);

The semaphore already exists?
Semaphore exists, the program already has an instance running
if (GetLastError () = = error_already_exists)
{
Turn off semaphore handle
CloseHandle (Hsem);

Find the main window of a previous instance
HWND hwndprevious =:: GetWindow (:: GetDesktopWindow (), gw_child);
while (:: IsWindow (hwndprevious))
{
Check to see if the window has a preset tag?
Yes, it's the main window we're looking for.
if (:: Getprop (Hwndprevious, M_pszexename))
{

if (!::iswindowvisible (hwndprevious)) {ShowWindow (hwndprevious, sw_show);}
The main window is minimized, then its size is restored
if (:: Isiconic (hwndprevious))
:: ShowWindow (Hwndprevious, Sw_restore);
Activating the main window
:: SetForegroundWindow (hwndprevious);
Activates the dialog box for the main window
:: SetForegroundWindow (:: Getlastactivepopup (hwndprevious));
Exit this instance
return FALSE;
}

Continue looking for the next window
Hwndprevious =:: GetWindow (Hwndprevious, Gw_hwndnext);
}

The previous instance already exists, but its main window cannot be found
Maybe something went wrong.
Exit this instance
return FALSE;
}

AfxEnableControlContainer ();

Standard initialization
If you is not using these features and wish to reduce the size
of your final executable, you should remove from the following
The specific initialization routines you does not need.

#ifdef _afxdll
Enable3dcontrols (); Call the When using the MFC in a shared DLL
#else
Enable3dcontrolsstatic ();//linking to MFC statically
#endif

Cellipsewnddlg Dlg;
m_pMainWnd = &dlg;
int nresponse = dlg. DoModal ();
if (Nresponse = = IDOK)
{
TOD Place code-here-handle when the dialog is
Dismissed with OK
}
else if (nresponse = = IDCANCEL)
{
TOD Place code-here-handle when the dialog is
Dismissed with Cancel
}

Since the dialog has been closed and return FALSE so that we exit the
Application, rather than start the application ' s message pump.
return FALSE;
}

int cellipsewnddlg::oncreate (lpcreatestruct lpcreatestruct)
{
if (cdialog::oncreate (lpcreatestruct) = =-1)
return-1;

Set the Seek tag
:: SetProp (M_hwnd, AfxGetApp ()->m_pszexename, (HANDLE) 1);
return 0;
}

void Cellipsewnddlg::ondestroy ()
{
Cdialog::ondestroy ();

Delete look-up tags
:: Removeprop (M_hwnd, AfxGetApp ()->m_pszexename);
}

"Go" MFC prohibits multiple instances

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.