Restrict the number of applications

xiaoxiao2021-03-06  37

Some applications processes the shortage of resources, such as erased optical drives, serial ports, or a large amount of memory, usually do not want multiple instances of this application to run simultaneously.

In fact, you have no way to limit users can only start once. What you can do is to find if the user starts another instance after the application starts. If there is no existing instance, the application starts in a normal way. Otherwise, the usual processing is to exit.

To find an existing instance, you can find a list of processes, compare the file name of each process and file name of this application, if you find the process of file name matching, then you think the application has started (SPY example of the reference platform SDK And Visual C TList example). The disadvantage of this method is that additional work (such as enumeration-founded windows) is required to pass command line parameters. For how to enumerate a process or thread, you can refer to GetWindowThreadProcessID. You can also identify the agreed number, such as creating an object, with a very difficult reproducible attribute, and then lookup whether there is an object of the same attribute in the system. Usually create a mutual (MUTEX) or a window with a specified window (not for the dialog, because the window class of the dialog is # 32770). Some other methods include checking the return value of the window process to handle a specific message, check the apparatus of the window, and the like. It is recommended to lock the code to create an object with a mutex to avoid simultaneous creation of two objects.

Some procedures need to pass command line parameters to existing instances before exiting, such as some document handlers and browsers. The official method is to register the DDL command in the corresponding file type in the registry: HKCR / FileType / shell / open / ddeexec = Open ("% 1"). Unfortunately, the MFC only implements the package of DDE only in the CFrameWnd class. If your program is not based on the document view framework, it is very troublesome to implement DDE support. Fortunately, you don't have to do this, relatively simple method is to send a message, such as WM_CopyData, to find the existing instance window, pass the command line parameter as a parameter.

Some programs, such as IE or resource manager, Office, etc., use multipro-top windows to replace multiple instances of applications. Although the MFC7 application wizard provides options for multi-top windows, this implementation is in perfect - all top windows are still in the same thread such that each top window has no separate state. For example, if a browser window displays a message box, then another top window will hang. People who use Tencent Browser may be very familiar with this situation.

The MFC sample MTMDI demonstrates how to create a window in a separate thread in the MDI program, and the corresponding modification message and command delivery process. Although the application changes in the application, this breaks the Doc / View architecture, and the top layer window is still in a thread.

The dialog is the simplest, and the creation dialog is also easy to create a dialog box in a separate thread. However, create a toolbar on the dialog to write some extra code to imitate the behavior of the frame, and there is no support for document view, You need to write your own code to open the document and URL. There are also other side effects, such as the automatic update of the toolbar on the dialog, the status bar menu, cannot find existing application instances by custom window classes.

转载请注明原文地址:https://www.9cbs.com/read-65898.html

New Post(0)