Link to home
Start Free TrialLog in
Avatar of Pandora
PandoraFlag for United Kingdom of Great Britain and Northern Ireland

asked on

One instance on all OS & with Systray app - question for Madshi et al

Hi, I really need a cast iron safe & reliable way of stopping 2 instances of a program running on *all* Windows OS versions. Also, the program can be system tray based or a normal app. I've read lots of Paqs about this - lots & one posted from Madshi advocates a mutex approach:
https://www.experts-exchange.com/questions/10323536/Preventing-another-instance-in-systray.html?query=one+instance+madshi&searchType=topic#2688807

But then others, eg:
https://www.experts-exchange.com/questions/20666313/Running-Executables.html?query=mutex&searchType=topic
say that this requires TSecurityAttributes etc & advocate findwindow (but does this work with Systray?)

I've looked at some Torry components too but these seem a bit flakey and some caused errors in XP.
Normally I'd just go with Madshis answer but it was posted in 2000, so I'm worried there are security issues now on XP. I have Madshis Mad component suite if there's something fiendishly clever in there that gives a stable solution - wouldn't surprise me!  Ooh & I'm using D5 too if that makes a difference.
Thanks alot,
P :)
Avatar of Ivanov_G
Ivanov_G
Flag of Bulgaria image

ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shaneholmes
shaneholmes

http://delphi.about.com/library/weekly/aa110203a.htm

Controlling the number of application instances [page 1/4]
Controlling the number of application instances. In this article you'll learn how to 'run-once enable' a Delphi application that can check for its previous (running) instance. Along the process, several techniques of implementing such a check will be

http://delphi.about.com/library/weekly/aa100703a.htm


Controlling the number of application instances [page 2/4]
Controlling the number of application instances. In this article you'll learn how to 'run-once enable' a Delphi application that can check for its previous (running) instance. Along the process, several techniques of implementing such a check will be

http://delphi.about.com/library/weekly/aa100703b.htm

Controlling the number of application instances [page 3/4]
Controlling the number of application instances. In this article you'll learn how to 'run-once enable' a Delphi application that can check for its previous (running) instance. Along the process, several techniques of implementing such a check will be

http://delphi.about.com/library/weekly/aa100703c.htm


Hope this helps!

Shane



hello Pandora, from all I've read about it, the  CreateMutex method is the most sure fire (reliable, and tested) method to use.. . however the other methods also work. . . Here is a version that I use


program EETest;

uses
  Windows, Forms,    // Put Windows in your Uses clause
  EETest1 in 'EETest1.pas' {Form1};

{$R *.RES}

var
MutexHandle: Cardinal;

function MakeMutex(UniqueName: String) : THandle;
var
SecAtr: TSecurityAttributes;
SecDes: TSecurityDescriptor;

begin
secAtr.nLength := sizeOf(SecAtr);
secAtr.lpSecurityDescriptor := @SecDes;
secAtr.bInheritHandle := False;
InitializeSecurityDescriptor(@SecDes, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@secDes, True, nil, False);
Result := CreateMutex(@secAtr, False, PChar(UniqueName));
if getlasterror = ERROR_ALREADY_EXISTS then
  begin
  CloseHandle(Result);
  Result := 0;
  end;
end;

begin
  MutexHandle := MakeMutex('G6H8jfr4d2');
  if MutexHandle = 0 then Exit;

  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
  CloseHandle(MutexHandle);
end.
If you read the articles I posted above, you can choose the method of your choice.

The CreateMutex function requires the name of the mutex object (unique). What this means, is that for every application you want to be executed only once, you'll need to assign a different mutex name - and this could lead you into problems."

http://delphi.about.com/library/weekly/aa100703b.htm


Shane


The mutex solution has never let me down yet. But please don't give me the points for this one sentence. The others have earned it more...   :-)
>(but does this work with Systray?)

Yes. I can't see any reason for not using FindWindow and an unique WindowClassName (unless your app is windowless, of course). Both methods (mutex and ClassName) require an unique identifier but mutex solution requires/ocupies additional resourses.

Regards, Geo
Avatar of Pandora

ASKER

Hi all, thanks ever so much for your help on this. I would of course like to give you all some points ;-) but actually I'm gonna give them to rlibby as he answered the question first by allaying my fears and doing a bit of hand-holding on the mutex front - and thats what I asked for - gotta be the easiest 350 points you've ever got rlibby, but still worth it for me so thanks! Shaneholmes & Slick thanks for your code/component (that's a great article v interesting - I've posted 2 questions for each of you for 100 points as your contributions were really helpful too).
Shaneholmes: https://www.experts-exchange.com/questions/20967560/Points-for-Shaneholmes.html
Slick: https://www.experts-exchange.com/questions/20967562/Points-for-Slick812.html
And of course thanks to Geo & Madshi for customary wisdom - no points for you but a nice smile instead! :-)
Hope you think is fair! Thx all
P