Lonewolf Online

The website of an amateur photographer, astronomer and petrolhead.

Prevent Multiple Instances of Program Running

This code will prevent another instance from opening, and pass parameter details (such as file open command) to the existing application.

Sometimes you may only want a single instance of your application to run at any given time. This code will prevent another instance from opening, and pass parameter details (such as file open command) to the existing application.

To go in main (Form1) unit

var WM_FINDINSTANCE: Integer;

{The basic code contained in the next three procedures,
 which works with the DPR code to allow file association
 and open double-clicked file in the running instance of app
 was written by Andrius Adamonis}

procedure TForm1.WMRestoreApp(var Msg: TMessage);
begin
  if IsIconic(Application.Handle) then
    Application.RESTORE
  else
    Application.BringToFront;
end;

procedure TForm1.DefaultHandler(var message);
var
  s: String;
begin
  with TMessage(message) do
    if (Msg = WM_FINDINSTANCE) then
    begin
      Result := MyUniqueConst;
    end
    else
    if Msg = WM_OPENEDITOR then
    begin
      SetLength(S, MAX_PATH);
      GlobalGetAtomName(WPARAM, PChar(S), MAX_PATH);
      ShowMessage(S);
      try
        OpenDocument(S);

        except on E: Exception do
            raise Exception.Create('Error opening ' + S + ' - ' + E.message);
      end;
    end
    else
      inherited DefaultHandler(message);
end;

This code goes at the very end of the main unit

initialization {Used in the file association and running instance code}
  WM_FINDINSTANCE := RegisterWindowMessage('Your Custom Handle Message Here');

  if WM_FINDINSTANCE = 0 then
    raise Exception.Create('Initialization failed');

end.

This code goes in the .dpr

program Sample;

uses
  Forms,
  SysUtils,
  graphics,
  Windows,
  Unit1 in 'Unit1.pas' {Form1},

{$R *.RES}

var Previous: HWND;
    ATOM: TAtom;

function EnumWindowsCallback(Handle: HWND; Param: LPARAM): Boolean; stdcall;
  function IsMyClass: Boolean;
  var
    ClassName    : array[0..30] of Char;
  begin
    GetClassName(Handle, ClassName, 30);
    [i]//If you change the name of Form1 don't forget to change the string below![/i]
    Result := (StrIComp(ClassName, 'TForm1') = 0) and (SendMessage(Handle, WM_FINDINSTANCE, 0, 0) = MyUniqueConst);
  end;
begin
  Result := not IsMyClass; [i]{ needs True to continue }[/i]
  if not Result [i]{ = MyClass }[/i] then Previous := Handle;
end;

begin
  Previous := 0;
  EnumWindows(@EnumWindowsCallback, 0);

  if Previous <> 0 then
  begin
    PostMessage(Previous, WM_RESTOREAPP, 0, 0);
    if (ParamCount > 0) then
    begin
      ATOM := GlobalAddAtom(PChar(ParamStr(1)));
      SendMessage(Previous, WM_OPENEDITOR, ATOM, 0);
      GlobalDeleteAtom(ATOM);
    end;
    Exit;
  end;

  Application.Initialize;
  Application.Title := 'Sample 1';
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

 

Share/Save/Bookmark

 

Have Your Say

 

 

 

 

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment. The authors reserve the right not to publish any comments that they believe are hateful, racist, demeaning or otherwise inappropriate.

 

What Others Are Saying

There are no comments for this item yet. Click to Add Your Comments

 

  • Random Photographs
    • Montecatini Alto
    • The Leaning Tower and the Duomo
    • Firework Night 2006
    • The Leaning Tower of Pisa and the Duomo
    • Firework Night 2006
    • Firework Night 2006
    • Filled and Rounded
    • Merlin Crew Wave To Crowd
    • Duck
    • Unknown Biplane

  • Recent Additions
  • Search



  • Page copy protected against web site content infringement by Copyscape
This page was last updated on Monday, July 28th, 2008.
Unless otherwise stated, all photographs and content Copyright © 2000 - 2008 Tim Trott, All Rights Reserved. No graphics, photographs or content may be used without written permission.
Website Design and Graphics Copyright © 2005-2007 Tim Trott. home :: sitemap :: disclaimer:: contact