Use VC ++ to implement a unique instance for program running

Source: Internet
Author: User

Use VC ++ to implement a unique instance for program running

Method 1:

I. Implementation Method

For applications with windows, you can use the static function cwnd: findwindow () to find a fixed window and determine whether the program is running. Function prototype:

Cwnd * Pascal findwindow (maid, maid );

This function has two parameters: the first is the class of the window to be searched, and the second is the title of the window to be searched. You may not know either of them when searching, but at least you must know one of them. Some window titles are relatively easy to get, such as "Calculator", so you should use titles to search. However, some software titles are not fixed, such as "Notepad". If the opened files are different and the window titles are different, it is easier to use window-Class search. If a window that meets the conditions is found, this function returns the pointer to the window. Otherwise, the return value is null.

Considering the robustness of the program, we also need to determine whether the window is in the minimized state and whether there is a pop-up subwindow. In this case, we need to use cwnd: getlastactivepopup (), cwnd: isiconic () function. Their prototype is:

Cwnd * getlastactivepopup ()

This function returns a pointer to the most recently activated pop-up window in the specified parent window. If the window is just activated or does not contain any pop-up window, the function returns a pointer to the parent window.

Bool isiconic ()

This function is used to determine whether the current window is in the minimized state. If the window is in the minimized state, the function returns true; otherwise, flase is returned.

For Windows in the minimized state, you can call cwnd: showwindow (INT ncmdshow) to restore the normal state of the window. The prototype of this function is:

Bool showwindow (INT ncmdshow)

If the window is visible before, true is returned after the function is called; otherwise, false is returned. The value of the ncmdshow parameter can be any of the following constants:

Sw_hide: hides a window, and the activity status gives it a window;

Sw_minimize: minimizes the window, and the activity status is given to another window;

Sw_restore: displays a window with the original size and position, and enables it to enter the active status;

Sw_show: displays a window with the current size and position, and enables it to enter the active state;

Sw_showmaximized: Maximize the window and activate it;

Sw_showminimized: minimizes the window and activates it;

Sw_showminnoactive: minimizes a window without changing the activity window;

Sw_showna: displays a window with the current size and position without changing the activity window;

Sw_shownoactivate: displays a window with the nearest size and position without changing the activity window;

Sw_shownormal: Same as sw_restore;

Finally, do not forget to use the cwnd: setforegroundwindow () function to set the pop-up window as the front-end of the desktop.

With the above knowledge, we can modify the initinstance () function of the application class in the program. If the program has been run, we can find the corresponding program window, the window is displayed, and the initinstance () function returns false. The program exits early, otherwise it runs normally.

Although the above method is easy to implement, it is powerless for applications without windows.

  Ii. programming steps

1. Start visual c ++ 6.0 and generate a dialog box-based application named "instance ";

2. Modify the initinstance () function of the program;

3. Add code and compile and run the program;

The Code is as follows:
Bool ctestapp: initinstance ()
{// ------------ Add --------------
If (! Firstinitinstance ())
Return false;
// -------------- End --------------

Afxenablecontrolcontainer ();

// Setdialogbkcolor (RGB (160,180,220), RGB (, 0); // set the background color
# Ifdef _ afxdll
Enable3dcontrols ();
# Else
Enable3dcontrolsstatic ();
# Endif

Ctestrdlg DLG;
M_pmainwnd = & DLG;
Int nresponse = DLG. domodal ();
If (nresponse = idok)
{
 
}
Else if (nresponse = idcancel)
{

}
Return false;
}
// ----------- Added member function firstinitinstance ()---------------
Bool ctestapp: firstinitinstance ()
{
Cwnd * P;
If (P = cwnd: findwindow (null, "OCR "))
{
P-> getlastactivepopup ();
If (p-> isiconic ())

{
P-> showwindow (sw_restore );

}
P-> setforegroundwindow ();
Return false;
}
Else
Return true;
}
// The type declaration of member functions is:
Public:
Ctestapp ();

// {Afx_virtual (cocrapp)
Public:
Virtual bool initinstance ();
Virtual bool firstinitinstance ();
//} Afx_virtual

Method 2:

1. Declare the handle happ

2. Write the following code in the initinstance () function:

// Run only one instance
Happ = createmutex (null, false, "onlyoneinstanceexample ");
If (getlasterror () = error_already_exists)
{
Afxmessagebox ("already exist an instance! ");
Return false;
}

3. Write the following code in the exitinstance () function:
{
Closehandle (HAPP );
Return cwinapp: exitinstance ();
}

Method 3:

By dynamically connecting to the library DLL, you can achieve a more general method for controlling program running. Use the # pragma data_seg command in the DLL to implement the shared data segment. Define a variable long m_nrun in the Data Segment and set its initial value to-1, at the same time, you must add the statement m_nrun ++ before the statement of the DLL entry point function dllmain to return the successful value, which means to count the running instances when the application starts successfully connecting to the DLL, then, export a function in the DLL to return the value of the variable. Finally, configure the project of the application to be dependent on the DLL. In the application, determine whether the program is running based on the value of the m_nrun variable in the DLL.

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.