[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

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:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_10323536.html?query=one+instance+madshi&searchType=topic#2688807

But then others, eg:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20666313.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 :)
0
Pandora
Asked:
Pandora
1 Solution
 
Ivanov_GCommented:
0
 
Russell LibbySoftware Engineer, Advisory Commented:

The mutex is safe / sound on all Win32 OS'es. It is the recommended technique advocated my the MSDN as well, in regards to limiting to one instance. I have used it as well, and never had issues with it.

Regards,
Russell
0
 
shaneholmesCommented:
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



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Slick812Commented:
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.
0
 
shaneholmesCommented:
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


0
 
MadshiCommented:
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...   :-)
0
 
geobulCommented:
>(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
0
 
PandoraAuthor Commented:
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: http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20967560.html
Slick: http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20967562.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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now