Solved

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

Posted on 2004-04-23
8
552 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 64
Correct Component for Shopping Cart. 2 105
Working with hours 3 57
How to load 2 images in same column in Delphi 2 45
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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