Windows Limit Program Running Example

zhaozj2021-02-08  207

Limiting a program that can only have two ways to run an instance, that is, the "signal" and FindWindow, usually the first method is safer, and the second method must know the WINDOW Class (it is very painful in the MFC. And once the Class Name has changed later, the program is also changed), otherwise it may be wrong. However, both methods can only limit the program to run once. If you want to control the number of runs, such as 3 times, the above two methods will be difficult. I wrote a hook function before, in which a shared variable was used. After this program was written, I suddenly thought, I don't know if this thing is useful in the exe file? If you think of it, try it, try, really is quite refreshing, actually, then think that if you want to limit a program, you can only run an instance is not very simple, try it, of course, there is no doubt The program only runs one, the code is as follows:

1. Add the following sentences in all function entities in a CPP. : Shardat, RWS ")

2. Assign GhmainWnd = getsafehwnd () in the oncreate of your main window.

3, the program is running (such as initInstance) Judging whether ghmainwnd is nullif (ghmainwnd! = Null && iswindow (ghmainwnd, wm_yourmessage, ... return)

How is it, more than the first method, with the above code, you have to limit the number of procedures, as follows: 1, add the following sentences in a CPP, add the following sentence #pragma data_seg ("Shardat" HWND GHMAINWND [10] = {}; // Save the running window handle, not much use INT gruned = 0; #pragma data_seg () # Pragma Comment (Linker, "/ Section: Shardat, RWS")

2, in your main window on the oncreate to GhmainWnd assignment IF (gruned <10) ghmainwnd [gruned] = getsafehwnd (); gruned ;

3, note, you must reduce the gruned when appropriate, such as middleness in ONDESTROY

4, the program is running (such as initInstance) Judging whether ghmainwnd is nullif (gruned> = max_run) {sendMessage (ghmainwnd, wm_yourmessage, ... return ...} Note: Pay attention to synchronization when working with GRUNED

Where Max_Run can be saved in REG or INI, set according to different situations

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

New Post(0)