Board index » delphi » is there a delphi equiv to vb's app.previnstance?

is there a delphi equiv to vb's app.previnstance?

I don't want the user to be able to run more than one instance
of the application.  Vb had an app object (with previnstance method)
similar to the Tapplication object here, but the fObjectInstance
property for TApplication objects is private.  Any help is greatly
appreciated.

Thanks,
Eric J. Lannert
Northwestern University, Evanston, IL.   USA
lann...@ils.nwu.edu

 

Re:is there a delphi equiv to vb's app.previnstance?


lann...@ils.nwu.edu (Eric J. Lannert) wrote:

Quote
>I don't want the user to be able to run more than one instance
>of the application.  Vb had an app object (with previnstance method)
>similar to the Tapplication object here, but the fObjectInstance
>property for TApplication objects is private.  Any help is greatly
>appreciated.

Create the following UNIT
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.

Then add the following code to your Project Source code:
  if HPrevInst <> 0 then begin
    ActivatePreviousInstance;
    Exit;
  end;

To give credit where do, I got this from a program called AppLaunch by
        Anders Ohlsson
        Tj?derv?gen 4
        S-746 34 B?lsta
        Sweden

-------------------------------------
Name: Bill Florac
Company: Electronic Theatre Controls
E-mail: fl...@etcconnect.com
-------------------------------------

Re:is there a delphi equiv to vb's app.previnstance?


lann...@ils.nwu.edu (Eric J. Lannert) wrote:

Quote
>I don't want the user to be able to run more than one instance
>of the application.  Vb had an app object (with previnstance method)
>similar to the Tapplication object here, but the fObjectInstance
>property for TApplication objects is private.  Any help is greatly
>appreciated.

HPrevInst Variable

See Also

quoted from delphi.hlp:
Unit
System

Declaration
var HPrevInst: Word;

Description
In a program, the HPrevInst variable contains the handle of the
previous instance of the application, or 0 if there are no previous
instances. In a library, HPrevInst is always zero.

I don't have the reference here with me now, but Delphi Developers
Guide has a mention of how one can use the winAPI custom message
facility to send private messages between instances.  On receipt of
message, the instance can take focus, shutdown, do whatever . . .

Tom

Other Threads