The easiest way to find a previous application instance

I rewrote the VB6 application in Delphi. It should have only one instance. How can I do this with minimal code?

In VB6, we just need to use one line of code>

If App.PrevInstance Then "Take Action End If

With glasses I found a solution, but it is very long, and we need to mess with the .drp file.

I do not want to do this.

I want something simpler.

+3
source share
3 answers

I have the code line by line:

var
    AppMutex: THandle;

{....}


initialization
    // Create the mutex
    AppMutex: = CreateMutex (nil, True, 'MY-APPLICATION-NAME');
    if (AppMutex = 0) or (GetLastError = ERROR_ALREADY_EXISTS) then
    begin
        MessageDlg('My application is already running on this computer.'#13#10+
            'You should close the other instance before starting a new one.',mtError,
            [mbOK],0);
        Halt;
    end;

finalization
    // Close the mutex
    CloseHandle(AppMutex);

, , @mghie, /!

: , , .

+3

, , , . , / "".

+3

, , . , , - . , , , .

, , , .

, GUID. exe, , - .

exe, , - exe.

+1

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


All Articles