📜 ⬆️ ⬇️

Prevent the launch of a copy of a third-party application

Robot Burning Copy Notebook Many software programmers have been faced with the task of preventing the launch of copies of an application. This is done with different goals and depends on the situation. There is even a separate term for solving this problem - Mutex .

Also, this task sometimes arises before system administrators with the difference that the application is third-party (developed by another organization). This article describes a relatively simple way to solve the problem of preventing launching copies of a third-party application.


Task


One day I had a problem at work - some employees managed to run multiple copies of applications. Running multiple copies, of course, did not lead to catastrophic results, but sometimes generated various "glitches", unstable program operation. This, naturally, gave me a headache from numerous complaints about these “glitches”.
')
First of all, I had an explanatory conversation with employees about the problem of running copies of the application and how to avoid it. This measure helped for the next couple of days, and then everything returned to normal. To complain to the authorities about such connivance of some workers was fraught with my dismissal, since the authorities insanely valued these workers, and the attitude towards me was radically opposite.

As the saying goes, "Since the mountain does not go to Mohammed, then Mohammed goes to the mountain." I decided to programmatically prohibit the launch of copies of the application. The task was not trivial.

A simple analysis revealed 2 features of my situation:
First, I have a third-party application, i.e. its developer is not our organization and there is no access to the source code (I will explain that the application is one of Microsoft products). The application does not have any regulatory settings or parameters.
Secondly, the application is launched not directly, but as a default program for opening files of a certain extension. That is, the application starts with the parameter (the path of the data file that you want to open).

Solution - Theory


Searching the internet did not give me an explicit solution. Prohibiting the launch of the second copy of the application in the form of system settings, as I understand it does not exist (although there is an API). I also found dozens of examples of implementing mutexs in various programming languages, but all of them, naturally, required changes to the code and did not suit me. We had to look for our original solution, and preferably easier.

The only solution I saw was the creation of a kind of "layer" between the user and the application. The following should occur:

As you understand, this approach is quite versatile. This application-layer can perform not only the task of prohibiting the launch of a copy of the program, but many others (logging, launching related programs, notifications, etc.). For the user, the operation of the interlayer application will be invisible.

Solution - Practice


A practical solution is given for the Notepad application (of course, any application can be in its place).

The choice of a programming language for writing an interlayer application did not cause me any difficulties. I needed to do this quickly and without problems, so I chose the AutoIt automation language. Lightweight AutoIt was originally "sharpened" for work with third-party applications. You can download it from the official site . In addition to the bundle of the language pack itself, the SciTE Script Editor is a fairly convenient code editor, in which it is convenient to write scripts for AutoIt.
Naturally, you can use any other language with the necessary capabilities.

The program code of the script (text file with the extension .au3) is nothing complicated:

; **     ** ;  Singleton     WinAPI.     ,    Func Singleton($semaphore) Local $ERROR_ALREADY_EXISTS = 183 DllCall("kernel32.dll", "int", "CreateSemaphore", "int", 0, "long", 1, "long", 1, "str", $semaphore) Local $lastError = DllCall("kernel32.dll", "int", "GetLastError") If $lastError[0] = $ERROR_ALREADY_EXISTS Then Exit -1 EndFunc Singleton("Mutex") ;   ShellExecuteWait         ;    ,    Mutex ; $CmdLineRaw -     ShellExecuteWait("C:\WINDOWS\notepad.exe", $CmdLineRaw, @ScriptDir, "open", @SW_MAXIMIZE) 

Habrawar mayorovp correctly noted that the name of the “Mutex” semaphore is better to replace with something more unique.

As you can see, the whole code can be divided into two parts: mutex and the launch of the required application. I will not explain in more detail, because The article is devoted not to studying the capabilities of AutoIt.

After writing the script, you can compile it either in the SciTE Script Editor, or through the context menu of the file (right click on the script file). After which it can already be used. You can also compile with additional settings (system bit and application icon) using the Aut2Exe utility, which also comes with AutoIt.

Then everything is elementary - we put the resulting interlayer application in some directory and configure the default opening for data files (in the example for files with the .txt extension) using this interlayer application.

That's all. We check and rejoice at the solved problem - now no one can run two copies of Notepad.

Afterword


This method allowed me to solve the problem of double-launch applications. True, the next day after installing the interlayer application, careless employees came running to me with the words "It does not start." I had to hold an explanatory conversation with employees on the topic "The application does not start because it is already running." This conversation was much more effective, because employees had no particular choice. For almost a month these problems have not arisen.

If any reader knows a way to write mutex as a .bat file for this task, then share your experience. I have a suspicion that there is such a way.

Source: https://habr.com/ru/post/212413/


All Articles