[fpc-pascal] single application instance

Jose Pascual josepascual at almudi.com
Fri Sep 23 18:04:57 CEST 2005


Hi Alexey,

thank you for your answer, I'm thinking about some portable solution for
win32 and linux?
How can I also do it in linux?

tia!
best regards


> -----Mensaje original-----
> De: fpc-pascal-bounces at lists.freepascal.org 
> [mailto:fpc-pascal-bounces at lists.freepascal.org] En nombre de 
> Alexey Pavluchenko
> Enviado el: viernes, 23 de septiembre de 2005 10:35
> Para: FPC-Pascal users discussions
> Asunto: Re: [fpc-pascal] single application instance
> 
> 
> Hello Jose,
> 
> Thursday, September 22, 2005, 1:20:48 PM, you wrote:
> 
> JP> How can avoid to run my program more than once?
> 
> I suppose you wanted to ask "how can I allow only one 
> instance of my program running under Win32". Correct me if 
> I'm wrong. If I'm right, then you may do it like this: check 
> for named mutex with the name unique to your program, if it 
> does not exist then create it, if it does then terminate the 
> program. The following code is from APPINIT unit written by 
> Marc Batchelor:
> 
> === cut ===
> procedure InitInstance;
> begin
>   { Check to see if the mutex is already there }
>   InstanceMutexHandle := OpenMutex(MUTEX_ALL_ACCESS, false,
>     @UniqueApplicationString[1]);
>   if InstanceMutexHandle = 0 then
>   begin
>     { This is the first instance }
>     InstanceMutexHandle := CreateMutex(nil, false,
>       @UniqueApplicationString[1]);
>     { Error checking to see if anyone beat us... }
>     if InstanceMutexHandle = 0 then
>       IsFirstInstance := false
>     else
>       IsFirstInstance := true;
>   end
>   else
>     IsFirstInstance := false;
> end;
> === cut ===
> 
> UniqueApplicationString in the above example is a 
> shortstring, if you use ansistring then you don't need '@' 
> operator and '[1]' index, just typecast it to pchar. The 
> string should be unique system-wide so it is probably best to 
> generate it based on some pseudo-random numbers.
> 
> -- 
> Best regards,
>  Alexey
> 
> 
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org 
> http://lists.freepascal.org/mailman/listinfo/fp> c-pascal
> 
> 
> __________ InformaciĆ³n de NOD32, revisiĆ³n 1.1230 
> (20050922) __________
> 
> Este mensaje ha sido analizado con  NOD32 antivirus system 
http://www.nod32.com





More information about the fpc-pascal mailing list