Solved

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

Posted on 2004-04-23
8
549 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
 
LVL 33

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now