Improve company productivity with a Business Account.Sign Up

x
?
Solved

Neat method for blocking multiple instances

Posted on 1999-01-25
8
Medium Priority
?
757 Views
Last Modified: 2008-02-01
Here is a neat method for blocking multiple instances of an application from running.  I came up with this while answering another question on EE, and thought it might be nice to post for all to see.  I have never seen this method anywhere else and it seems like it might possibly be something other members of EE could use or improve upon. Anyway here it is


unit PREVINST;

{Unit PREVINST.pas -
      Include this unit in your project file (.dpr) to keep multiple instances
  of your application from running.  Works the same way as the standard method
  of using mutexes, except uses a semaphore and stores the application handle in
  the semaphore's count so we can get it to show the original instance.
  You would call it like this:

  program Project1;

      uses
        Forms,
        Unit1 in 'Unit1.pas',
        PREVINST in 'PREVINST.pas';

      begin
    if not HavePrevInstance(Application.Handle, 'SOmeWEIRDnameFoRTheSemaphOre') then
    begin
          Application.Initialize;
              Application.CreateForm(TForm1, Form1);
              Application.Run;
    end;
  end.}

interface

uses Windows;

function HavePrevInstance(AppHandle: THandle; UniqueName: String): boolean;

implementation

function HavePrevInstance(AppHandle: THandle; UniqueName: String): boolean;
const
  ERROR_ALREADY_EXISTS = 183; //Not in Windows.pas - dont know why
  SEMAPHORE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or 3; //Ditto
  WM_SYSCOMMAND = 274; //Define here so dont have to include Messages.pas
var
  MyHandle: THandle;
begin
  Result := False; //Set Initial Result to false
  //Now we create the semaphore and store the applications handle as its count
  MyHandle := CreateSemaphore(nil, AppHandle, AppHandle, PChar(UniqueName));
  //The following will be true if an instance of out app is already running
  if (MyHandle <> 0) and (GetLastError = ERROR_ALREADY_EXISTS) then
  begin
    Result := True; //Set to result to true
    //Now we open the semaphore to get its handle
    MyHandle := OpenSemaphore(SEMAPHORE_ALL_ACCESS, False, PChar(UniqueName));
    //This replaces the value of MyHandle with the Application handle of the prev instance
    ReleaseSemaphore(MyHandle, 0, @MyHandle);
    //now we have the handle to the previous instance - do whatever you want
    //This is what I usually do but you could pop up a messagebox or whatever
    if IsIconic(MyHandle) then
          PostMessage(MyHandle, WM_SYSCOMMAND, SC_RESTORE, 0)
    else
          SetForeGroundWindow(MyHandle);
  end;
end;

end.




Benefits of this method.

1. Same as using a mutex - can create using a name that is very unlikely to exist anywhere else on the system.

2. Stores the apps handle so the findwind calls to get it are not needed.

3. Very simple to add to existing code and keeps the app from starting at all if a previous instance is found.



Any suggestions on how this might be improved upon are welcome.

Heath
0
Comment
Question by:heathprovost
  • 6
8 Comments
 
LVL 5

Author Comment

by:heathprovost
ID: 1363244
Could someone answer this so that it gets pushed into the PAQs?

Thanks

Heath
0
 

Accepted Solution

by:
lorenkoss earned 0 total points
ID: 1363245
Consider it answered.. And thanks.. I like the method!
0
 
LVL 5

Author Comment

by:heathprovost
ID: 1363246
Thanks
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:cqhall
ID: 1363247
A) This technique does not appear to work in NT. The counter in the ReleaseSemaphore returns a random value instead of the handle (presumably) stored there.  I have tried in three systems - NT4/SP3, NT4/SP4 and Windows 95b.  Only Windows 95b returned the proper handle to the previous instance. NT does tell me a previous instance exists, so I can quit - I just can't get its handle to communicate with it.

B) I added an initialization section with logic to call application.terminate if a previous instance is found.  I use the ExtractFilename(paramstr(0)) as the unique name.  The result is that I can simply include the PrevInst.pas unit in the project if I only allow one copy.  I had built a similar unit in Delphi 1.

0
 
LVL 5

Author Comment

by:heathprovost
ID: 1363248
cqhall, if you ever look at this again.  Try changing this line

ReleaseSemaphore(MyHandle, 0, @MyHandle);

to this

ReleaseSemaphore(MyHandle, 1, @MyHandle);


I dont have NT to test this on, but I think that this would solve the problem.  I didnt read the API help thouroughly when I wrote this code.  ReleaseSemaphore fails when called with zero for the increment parameter.  It even fails under 95! (I wasnt bothering to check for success so I never noticed before)  But for some reason under 95 it still places the previous count into pointer passed to it.  My guess is NT does not exhibit the same behavior.  Passing a 1 should allow the function to succeed.

Heath  
0
 
LVL 5

Author Comment

by:heathprovost
ID: 1363249
Also, do you mind emailing me your modifications?  That initialization thing sounds pretty neat.

Heath

heath_provost@hotmail.com
0
 
LVL 5

Author Comment

by:heathprovost
ID: 1363250
btw - you also have to change this line

MyHandle := CreateSemaphore(nil, AppHandle, AppHandle, PChar(UniqueName));

to this

MyHandle := CreateSemaphore(nil, AppHandle, AppHandle + 1, PChar(UniqueName));

Heath
0
 
LVL 5

Author Comment

by:heathprovost
ID: 1363251
Yep, you were right and so was I.  I sent a friend with NT and app using the original code and then an app using the following modified code.  The original did not work, but the following code did.  I had to change more than I though but this works for both Windows 95/98 and NT.



///////////////////////CODE STARTS

unit PREVINST;

{Unit PREVINST.pas -
Include this unit in your project file (.dpr) to keep multiple instances
  of your application from running.  Works the same way as the standard method
  of using mutexes, except uses a semaphore and stores the application handle in
  the semaphore's count so we can get it to show the original instance.
  You would call it like this:

  program Project1;

uses
   Forms,
   Unit1 in 'Unit1.pas',
   PREVINST in 'PREVINST.pas';

begin
    if not HavePrevInstance(Application.Handle, 'SOmeWEIRDnameFoRTheSemaphOre') then
    begin
     Application.Initialize;
   Application.CreateForm(TForm1, Form1);
   Application.Run;
    end;
  end.}

interface

uses Windows;

function HavePrevInstance(AppHandle: THandle; UniqueName: String): boolean;

implementation

function HavePrevInstance(AppHandle: THandle; UniqueName: String): boolean;
const
  ERROR_ALREADY_EXISTS = 183; //Not in Windows.pas - dont know why
  SEMAPHORE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or 3; //Ditto
  WM_SYSCOMMAND = 274; //Define here so dont have to include Messages.pas
var
  MyHandle: THandle;
begin
  Result := False; //Set Initial Result to false
  //Now we create the semaphore and store the applications handle as its count adding one to it so we can keep toggling it back and forth
  MyHandle := CreateSemaphore(nil, AppHandle + 1, AppHandle + 1, PChar(UniqueName));
  //The following will be true if an instance of out app is already running
  if (MyHandle <> 0) and (GetLastError = ERROR_ALREADY_EXISTS) then
  begin
    Result := True; //Set to result to true
    //Now we open the semaphore to get its handle
    MyHandle := OpenSemaphore(SEMAPHORE_ALL_ACCESS, False, PChar(UniqueName));
    //at this point MyHandle is our app handle plus one. now we reduce count by one to get back our original handle
    WaitForSingleObject(MyHandle, 0);
    //This replaces the value of MyHandle with the Application handle of the prev instance and pushes our count back up again
    ReleaseSemaphore(MyHandle, 1, @MyHandle);
    //now we have the handle to the previous instance - do whatever you want
    //This is what I usually do but you could pop up a messagebox or whatever
    if IsIconic(MyHandle) then
     PostMessage(MyHandle, WM_SYSCOMMAND, SC_RESTORE, 0)
    else
     SetForeGroundWindow(MyHandle);
  end;
end;

end.

//////////////////CODE ENDS

Sorry about that.  You learn something new everyday.

Heath
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …
Watch the software video of Kernel Import PST to Office 365 tools which can easily import PST and OST files to Office 365 for bulk mailboxes. The process of migration is simple and user can map source and destination mailboxes and easily import data…

606 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question