The restriction program only opens an instance

xiaoxiao2021-03-31  218

【Author: Fictiony (fictiony@china.com)] [reprint please indicate the source when we do some kind of program management platform (such as Windows Task Manager), often need to limit the program can open only one instance. The rough idea of ​​solving this problem is simple, nothing more than judging if there is a process of being in the same way, if there is, turn it off, otherwise it will run. However, the problem is how to discriminate whether there is a process with yourself. I searched the relevant article online, I found that the solution is not more than the following ways: 1. Use :: CREATEMUTEX to create a mutually exclusive object when the process is initialized, and determine if the mutex is existing Whether the program has been run. This method is indeed very easy to implement whether the discriminator instance has existed, only needs to add the following statement at the beginning of the InitInstance method: m_hunique = :: Createmutex (null, false, unique_id); if (getLastError () == Error_already_exists) Return False; Unique_id is a string with uniqueness, which can be automatically generated with VC for the main program header file (that is, the long string macro definition on the top of the .h file), of course, can be manually generated by the tool yourself. Saicited ^^ It should be noted that don't forget to turn the mutectable object in the exitInstance method. But this approach has a big problem, it is difficult to get the main window handle that has opened the program instance. And most of us, you need to activate the main window of that program instance. To get the main window handle, you need to use other methods mentioned later. 2. Traverse all the open process main windows, compare the window title, if you find the title of the condition, indicating that the program has run and activate the window. Although the program's main window can be found, the problem is obvious: a. What should I do if the window title changes (such as the file name of the document in the title)? B. What should I do if the main window title of other programs is just the same as the program? The first question can be solved by writing a registry or writing the INI file. That is, when the main window title changes, the new title is written to the registry or INI file. However, this kind of solution is also troublesome. -_- || The second question is troubles, at least I have not found a good solution. If you have to say one, then I suggest that you "try to do a way", "unscrupulous means" is absolutely different. However, it is estimated that this step is done, you are almost gone. 3, use: SetProp to the main window to add an attribute value that has a uniqueness of the attribute to be judged by traverse all windows at the time of the process. The code to add attribute values ​​can be placed at the end of the InitInstance method, as follows:: setProp (m_pmainwnd-> m_hwnd, "unique_id", (handle) unique_id); unique_id is an integer value with uniqueness (why not use strings • Because the comparison of the string needs to read the string, this can only record the string address, and this address is meaningless in other programs, so this string cannot be read). This way only problem is how to determine that the integer value is unique. The solution we have raised later is based on this method.

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

New Post(0)