Board index » delphi » Delphi version of VB Function 'PrevInstance'?

Delphi version of VB Function 'PrevInstance'?

Hi,

Before my application is run I want to test to see if it is already running.
In other words I want to allow only one instance of my application running
at one time.

In VB I used 'App.PrevInstance'.  Is there something like this in Delphi 4?

Thanks

Dave Birks
dbi...@halesowen.ac.uk

 

Re:Delphi version of VB Function 'PrevInstance'?


I think it's "hPrevInst" in Delphi...

Regards,
Mathias Nyman
SE Datateknik AB
Sweden

David Birks skrev i meddelandet <73lshs$...@ccuh.wlv.ac.uk>...

Quote
>Hi,

>Before my application is run I want to test to see if it is already
running.
>In other words I want to allow only one instance of my application running
>at one time.

>In VB I used 'App.PrevInstance'.  Is there something like this in Delphi 4?

>Thanks

>Dave Birks
>dbi...@halesowen.ac.uk

Re:Delphi version of VB Function 'PrevInstance'?


Put this lot in a separate unit and add the
unit to the uses clause of your .dpr file.

unit BHWinUtils;

interface

uses
  windows;

function  BHShowPrevInstance: boolean;
procedure BHShowWindow;

implementation

var
  MyAppName         : Array[0..255] of Char;
  MyClassName       : Array[0..255] of Char;
  NumFound          : Integer;
  AllowedInstances : integer = 1;
  hWindow          : hWnd;

procedure BHShowWindow;
var
  MyPopup: HWnd;
begin
  if hWindow = 0 then exit;
  MyPopup := GetLastActivePopup(hWindow);
  BringWindowToTop(hWindow);
  if IsIconic(MyPopup) then
    ShowWindow(MyPopup,SW_RESTORE)
  else
    SetForegroundWindow(MyPopup);
end;

function BHLookAtAllWindows(Handle: HWND; Temp: LongInt):BOOL;stdcall;
// this function is called by Windows
var
  WindowName : Array[0..255] of Char;
  ClassName  : Array[0..255] of Char;
begin
  result:= false;
  if GetClassName(Handle,ClassName,SizeOf(ClassName)) > 0 then
    if StrComp(ClassName,MyClassName) = 0 then
      if GetWindowText(Handle,WindowName,SizeOf(WindowName)) > 0 then
        if StrComp(WindowName,MyAppName)=0 then
          begin
            inc(NumFound);
            if Handle <> Application.Handle then
              hWindow:= Handle;
          end;
  result:= true;
end;

function BHShowPrevInstance: boolean;
begin
  result:= false;
  NumFound := 0; hWindow := 0;
  GetWindowText(Application.Handle,MyAppName,SizeOf(MyAppName));
  GetClassName(Application.Handle,MyClassName,SizeOf(MyClassName));
  EnumWindows(@BHLookAtAllWindows,0);
  if NumFound > AllowedInstances then
  begin
    BHShowWindow;
    result:= true;
  end;
end;

Then your .dpr file should look something like this:-

begin
  if not BHShowPrevInstance then
  begin
      Application.Initialize;
      Application.CreateForm(Tform1, form1);
      Application.Run;
  end;
end.

Hope this helps (*grin*).
I might have missed a unit or two out of the uses clause but
I'm sure you'll figure it out. Just put the cursor over the offending
article and press F1...Delphi'll tell you which unit needs to be
added.

Barry Harkcom

On Fri, 27 Nov 1998 09:47:32 -0000, "David Birks"

Quote
<dbi...@halesowen.ac.uk> wrote:
>Hi,

>Before my application is run I want to test to see if it is already running.
>In other words I want to allow only one instance of my application running
>at one time.

>In VB I used 'App.PrevInstance'.  Is there something like this in Delphi 4?

>Thanks

>Dave Birks
>dbi...@halesowen.ac.uk

Re:Delphi version of VB Function 'PrevInstance'?


no, you have to write your own function, using Findwindow for a simple
solution, or a mutex for a more rigourous result.
look for mutex in the archives of this newsgroup, you will find details
about this subject.

Denis

Re:Delphi version of VB Function 'PrevInstance'?


   Not in Delphi 4 (or 32bit...)  --  Use a Mutex (Take a look at CreateMutex
and OpenMutex)  --  If the mutex is open, you're app. is already running...  --
If it's not, you create one, and let your app. run....)  --  It's really easy to
use, but E-mail me if you get stuck....

--
Jason Wallace
SL Software
Dark...@SLSoftware.reno.nv.us
--
"We are Microsoft.  You will be assimilated.  Resistance is Futile."
--

Quote
David Birks <dbi...@halesowen.ac.uk> wrote in message

news:73lshs$m49@ccuh.wlv.ac.uk...
Quote
>Hi,

>Before my application is run I want to test to see if it is already running.
>In other words I want to allow only one instance of my application running
>at one time.

>In VB I used 'App.PrevInstance'.  Is there something like this in Delphi 4?

>Thanks

>Dave Birks
>dbi...@halesowen.ac.uk

Other Threads