Solved

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

Posted on 2004-04-23
8
554 Views
Last Modified: 2010-04-05
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
Comment
Question by:Pandora
8 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10903015
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 350 total points
ID: 10903044

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
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10903189
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
Independent Software Vendors: 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!

 
LVL 34

Expert Comment

by:Slick812
ID: 10910560
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
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10912130
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
 
LVL 20

Expert Comment

by:Madshi
ID: 10913595
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
 
LVL 17

Expert Comment

by:geobul
ID: 10915999
>(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
 
LVL 1

Author Comment

by:Pandora
ID: 10917831
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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