Board index » delphi » Previnstance?

Previnstance?

Is there a way to know if the application is alredy running? In Visual Basic,
this function was called; "App.Previnstance". Is it a API function?

Christian Landgren (christian.landg...@sting.dextel.se)

 

Re:Previnstance?


In article: <christian.landgren.6.000D7...@sting.dextel.se>  christian.landg...@sting.dextel.se (Christian Landgren) writes:

Quote

> Is there a way to know if the application is alredy running? In Visual Basic,
> this function was called; "App.Previnstance". Is it a API function?

> Christian Landgren (christian.landg...@sting.dextel.se)

The method I use to detect if app is already running is as follows :-

I have a unit called PREVINST.PAS (code below) which I add to every project.

unit PrevInst;

interface

uses WinProcs, WinTypes, SysUtils;

type
  PHWnd = ^HWnd;

function EnumFunc(Wnd : HWnd; TargetWindow : PHWnd): Bool; export;
procedure ActivatePreviousInstance;

implementation

function EnumFunc(Wnd : HWnd; TargetWindow : PHWnd): Bool;
var
  ClassName : array [0..30] of char;
begin
  Result := True;
  if GetWindowWord(Wnd,GWW_HINSTANCE) = HPrevInst then
  begin
    GetClassName(Wnd,ClassName,30);
    if StrIComp(ClassName,'TApplication') = 0 then
    begin
      TargetWindow^ := Wnd;
      Result := False;
    end;
  end;
end;

procedure ActivatePreviousInstance;
var
  PrevInstWnd : HWnd;
begin
  PrevInstWnd := 0;
  EnumWindows(@EnumFunc,Longint(@PrevInstWnd));
  if PrevInstWnd <> 0 then
    if IsIconic(PrevInstWnd) then
       ShowWindow(PrevInstWnd,SW_RESTORE)
    else
       BringWindowToTop(PrevInstWnd);
end;

end.

I then insert the following code into the projects source code before any form create/run commands.

  { If there's another instance already running, activate that one and terminate
    this one }
  if HPrevInst <> 0 then
  begin
     ActivatePreviousInstance;
     Exit;
  end;

i.e.

program Ttrick1;

uses
  Forms,
  Ttrick01 in '\SOURCE\BRASS\FORMS\TTRICK01.PAS' {MainForm},
  PrevInst in '\SOURCE\BRASS\FORMS\PREVINST.PAS',
  Splash in '\SOURCE\BRASS\FORMS\SPLASH.PAS' {SplashForm},
  Ttrick02 in '\SOURCE\BRASS\FORMS\TTRICK02.PAS' {BrowseForm};

{$R *.RES}

begin
  { If there's another instance already running, activate that one and terminate
    this one }
  if HPrevInst <> 0 then
  begin
     ActivatePreviousInstance;
     Exit;
  end;
  { Display title screen while loading }
  SplashForm := TSplashForm.Create(NIL);
  SplashForm.lTitle.Caption := 'Delphi - Tips && Tricks';
  SplashForm.Show;
  SplashForm.Repaint;
  Application.Title := 'Delphi - Tips & Tricks';
  Application.CreateForm(TMainForm, MainForm);
  Application.CreateForm(TBrowseForm, BrowseForm);
  SplashForm.Free;
  Application.Run;
end.

Hope the above is of some help. Any probs mail me.

Regards from Ian.
--
                     ||||||||
                     | ~  ~ |
                    (| @  @ |)
----------------oOOo---(__)---oOOo------------------------
| Ian - "Smoke me a kipper I'll be back for breakfast !" |
|                                                        |
| EMail IanMa...@breallon.demon.co.uk    Banco Real s.a. |
|                                        (London Branch) |
----------------------------------------------------------

Re:Previnstance?


There is a problem with your method.  The classname
you selected to find was 'TApplication'.  Unfortunately,
this is the default classname for any Delphi application.
Therefore you must change the classname of you application
to something unique.  You must create a descendant of
TApplication that creates an alternate classname.  I
never persued this, but shows one of the shortcomings
of Delphi, and that is in inheriting TApplication.  Similar
words can be said about inheriting from TForm.  Every
now and then someone will post a question about
inheriting from TForm.  Mostly you don't understand the
difficulties until you actually try to inherit from
one of these types and you realize that you loose the
"visualness" and have to dynamically create everything,
which is now worse than OWL because of the void of creating
dialogs with resource files.

Anyway enough soap box stuff...  If you find a solution,
please share it with the rest of us.

By the way there is nothing wrong with using hPrevInst, even
in Windows 95 or NT.  Provided that you compile in 16 bit
Delphi.  This is because 16 bit programs still run in a 16 bit
environment.  When you compile native Windows 32 bit programs
then that is a different story.  Then I would worry about the
use of hPrevInst.  Always program for the present, not the
future.  This will ensure that your product gets developed
faster, and provides job security for future rewrites :^)

Best regards,
Michael Vincze
m...@asd470.dseg.ti.com

In article 407520837...@breallon.demon.co.uk, "Ian A. Macey" <IanMa...@breallon.demon.co.uk> () writes:

Quote
>In article: <christian.landgren.6.000D7...@sting.dextel.se>  christian.landg...@sting.dextel.se (Christian Landgren) writes:

>> Is there a way to know if the application is alredy running? In Visual Basic,
>> this function was called; "App.Previnstance". Is it a API function?

>> Christian Landgren (christian.landg...@sting.dextel.se)

>The method I use to detect if app is already running is as follows :-

>I have a unit called PREVINST.PAS (code below) which I add to every project.

> [snip]

>function EnumFunc(Wnd : HWnd; TargetWindow : PHWnd): Bool; export;
>procedure ActivatePreviousInstance;

>implementation

>function EnumFunc(Wnd : HWnd; TargetWindow : PHWnd): Bool;
>var
>  ClassName : array [0..30] of char;
>begin
>  Result := True;
>  if GetWindowWord(Wnd,GWW_HINSTANCE) = HPrevInst then
>  begin
>    GetClassName(Wnd,ClassName,30);
>    if StrIComp(ClassName,'TApplication') = 0 then

                            ^^^^^^^^^^^^
____________________________||||||||||||

- Show quoted text -

Quote
>    begin
>      TargetWindow^ := Wnd;
>      Result := False;
>    end;
>  end;
>end;

Other Threads