Avoiding two processes of the same application in Pascal

Asked

Viewed 690 times

1

What is the most viable way to identify the processes of my application in memory and close them if there is more than one execution. I use the Lazarus IDE platform (looks like Delphi), which supports the object-oriented Pascal programming language.

OBS: Even if there are different process names I can identify my application.

2 answers

3


The component Uniqueinstance can do this job for you, to use it just drop a component in the main form, manipulate the property Identifier(used to identify your application) and activate it.

You can also do this using the function CreateMutex() for identify the application, if the function succeeds the return value will be identity otherwise a null value.

If such an identifier already exists before calling the function, the return will be the identifier for the existing object, in which case when calling the function GetLastError(), the return value will be ERROR_ALREADY_EXISTS. That in practice would be something like this:

var
  mutex: THandle;
  ID: string;
begin
  ID:= 'MyAppUniqueID';
  mutex := CreateMutex(nil, False, PChar(ID));

  if GetLastError = ERROR_ALREADY_EXISTS then begin
    Application.Terminate;
  end;
end;

The code snippet above can be used in the event OnCreate() of the main form. To release the identifier you can use the function CloseHandle() at the event OnClose() or OnDestroy(), for this the variable mutex would have to be a global variable.

  • Thank you, you helped me a lot.

1

The function is used CreateSemaphore windows. If the function GetLastError say that there is already a semaphore with that name, it is already running. Otherwise, no.

Relevant part of the function documentation:

Return value

If the Function succeeds, the Return value is a Handle to the Semaphore Object. If the named Semaphore Object existed before the Function call, the Function Returns a Handle to the existing Object and Getlasterror Returns ERROR_ALREADY_EXISTS. If the Function fails, the Return value is NULL. To get Extended error information, call Getlasterror.

Example of use:

repeat
sleep(10000);
createsemaphore("umnomequalquer");
until getlasterror <> ERROR_ALREADY_EXISTS;
  • I need it to work on the other platforms. Another thing is that I would like to analyze the memory of the process and see if it is identical or relative, not only by the name of the application.

  • 2

    This is in Pascal, could explain better the use?

  • 1

    Caion, I have merged your two answers into one. Keep in mind that you can always edit your questions here on the site, and it is often preferable to add more detail to one answer than to post another. Our site is a little different from the others, take a look at [tour], [help] and Community FAQ.

  • It could specify another way, thus avoiding the use of the "application name". Preferably there is a way to check the memory of my application and compare with other processes? So if you find an identical or relatively equal application in memory you could be closing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.