Recent

Author Topic: avoid 2 instances program  (Read 4288 times)

Ericktux

  • Sr. Member
  • ****
  • Posts: 353
avoid 2 instances program
« on: April 01, 2015, 06:57:22 pm »
Good day to all, my question is

how avoid open twice my program
unused "UniqueInstance"

help please.  :( :(

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: avoid 2 instances program
« Reply #1 on: April 01, 2015, 07:07:26 pm »

Ericktux

  • Sr. Member
  • ****
  • Posts: 353
Re: avoid 2 instances program
« Reply #2 on: April 01, 2015, 07:18:21 pm »
thank, but I want to use otherwise.   :(

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: avoid 2 instances program
« Reply #3 on: April 01, 2015, 07:32:17 pm »
"Otherwise" has a huge range of meanings.

Do you have any issue with UniqueInstance?

Syndrome

  • New Member
  • *
  • Posts: 35
Re: avoid 2 instances program
« Reply #4 on: April 01, 2015, 07:36:01 pm »
example for windows:
Code: [Select]
uses LazUTF8, Windows;

function AlreadyRunning:boolean;
const
   ApplicationSecretString = 'qweqweqweqweqweqweqweqwe';
var
   MutexHandle:Handle;
   Str:UnicodeString;
begin
   Str:= UTF8ToUTF16(ApplicationSecretString);
   MutexHandle:= Crea]"]>BlockedtexW(nil,true,@Str[1]);
   if MutexHandle = 0 then exit(true);
   if GetLastError = ERROR_ALREADY_EXISTS then exit(true);
   Result:= false;
end;

procedure TForm1.FormCreate(Sender:TObject);
begin
   if AlreadyRunning then halt;
end;
« Last Edit: April 01, 2015, 07:37:57 pm by Syndrome »

Ericktux

  • Sr. Member
  • ****
  • Posts: 353
Re: avoid 2 instances program
« Reply #5 on: April 01, 2015, 08:11:13 pm »
I love you Syndrome, works perfectly  :D :D :D :D :D

last question  ::), how do I return the same form maximize or restore. thanks

Ericktux

  • Sr. Member
  • ****
  • Posts: 353
Re: avoid 2 instances program
« Reply #6 on: April 01, 2015, 10:31:37 pm »
I am testing these codes to restore the existing "form" in maximized:

Code: [Select]
procedure TForm1.FormCreate(Sender:TObject);
begin
   if AlreadyRunning then
   begin   
   //halt;
   //Application.Restore;   <--- nothing
   //Application.BringToFront;    <--- nothing
   end;
end;

but nothing happens, little help.

Syndrome

  • New Member
  • *
  • Posts: 35
Re: avoid 2 instances program
« Reply #7 on: April 01, 2015, 11:02:20 pm »
This example activates previous instance of application:
Code: [Select]
uses LazUTF8, Windows;

var
   InterprocVarHandle:Handle = 0;
   InterprocVar:PHandle = nil;

function AlreadyRunning:boolean;
const
   ApplicationSecretString = 'qweqweqweqweqweqweqweqwe';
var
   Str:UnicodeString;
   AlreadyCreated:Boolean;
begin
   Str:= UTF8ToUTF16(ApplicationSecretString);

   // Create an interprocess variable
   InterprocVarHandle:= CreateFileMappingW(Handle(-1),Nil,PAGE_READWRITE,0,SizeOf(InterprocVar^),@Str[1]);

   if InterprocVarHandle = 0 then exit(true);
   AlreadyCreated:= GetLastError = ERROR_ALREADY_EXISTS;

   // Get a pointer to the variable
   InterprocVar:= MapViewOfFile(InterprocVarHandle,FILE_MAP_ALL_ACCESS,0,0,SizeOf(InterprocVar^));

   if InterprocVar = nil then exit(true);
   if AlreadyCreated then exit(true);

   // Write main form's handle to the interprocess variable
   InterprocVar^:= Form1.Handle;

   Result:= false;
end;

procedure ActivatePreviousInstance;
var
   PrevMainForm:Handle;
begin
   if InterprocVar <> nil then begin
      // Read previous main form handle from the interprocess variable
      PrevMainForm:= InterprocVar^;

      SendMessage(PrevMainForm,WM_SYSCOMMAND,SC_RESTORE,0);
      SetForegroundWindow(PrevMainForm);
   end;
end;

procedure TForm1.FormCreate(Sender:TObject);
begin
   if AlreadyRunning then begin
      ActivatePreviousInstance;
      halt;
   end;
end;
« Last Edit: April 01, 2015, 11:04:30 pm by Syndrome »

Ericktux

  • Sr. Member
  • ****
  • Posts: 353
Re: avoid 2 instances program
« Reply #8 on: April 01, 2015, 11:15:05 pm »
Thanks Syndrome, lo probaré.  :)

 

TinyPortal © 2005-2018