Solved

Prevend double launching of app

Posted on 2002-03-27
10
208 Views
Last Modified: 2010-04-04
What is the code to prevend someone from launching more than 1 instance of my programme ? In simpler terms, it means at any time, only 1 abc.exe is running and not 2.
0
Comment
Question by:tyfing
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 1

Expert Comment

by:Madmarlin
ID: 6899068
0
 

Author Comment

by:tyfing
ID: 6899161
sorry, i'm a newbie...can u put the code into a button ?
0
 
LVL 3

Accepted Solution

by:
smurff earned 10 total points
ID: 6899477
Try this

implementation

var
  mHandle: THandle; // Mutexhandle
  Projekt: string;

initialization
  Project := '';          // define your project-name here

  mHandle := CreateMutex(nil, True, PChar(Projekt));
  if GetLastError = ERROR_ALREADY_EXISTS then begin
    showMessage('Project ' + Project + ' is still running');
    halt;                               // kill the second instance
  end;

finalization                            // alt end free the mHandle
  if mHandle <> 0 then CloseHandle(mHandle)
end.


regards
Smurff
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.

 

Author Comment

by:tyfing
ID: 6899508
where is the 'Begin'  ?
0
 
LVL 3

Expert Comment

by:smurff
ID: 6899773
In the actual project1.dpr copy the code, this is out of one I already have.

program Mdiapp;

uses
  Forms,Windows,Dialogs,
  MAIN in 'MAIN.PAS' {MainForm},
  CHILDWIN in 'CHILDWIN.PAS' {MDIChild},
  about in 'about.pas' {AboutBox};

{$R *.RES}

var
 mHandle: THandle; // Mutexhandle
 Project: string;

begin
  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm);
  Application.CreateForm(TAboutBox, AboutBox);
  Project := 'SmurffsProggy';
  mHandle := CreateMutex(nil, True, PChar(Project));
  if GetLastError = ERROR_ALREADY_EXISTS then begin
   MessageDlg('Application already exists, this instance will now close',mtError,[mbOK],0);
   halt;                               // kill the second instance
  end
  else
  Application.Run;
   if mHandle <> 0 then CloseHandle(mHandle);
end.


To view the project file in the ide goto the delphi menu Project then select view source.

Hope this helps mate,
Regards
Smurff
0
 
LVL 3

Expert Comment

by:smurff
ID: 6899804
Also as a newbie you should know about some sites that would have this kind of code on it so you can save your points on EE. Try these first before asking Qs.


www.torry.net and click on tips
www.delphi3000.com
www.delphifaq.com
www.delphipages.com

and theres lots more and these have links to them.
Just a note, if you know them then just ignore me :)
regards
Smurff
0
 
LVL 3

Expert Comment

by:tongalite
ID: 6900335
Hi
here's a routine from another forum
Thanks to RLord:
T.

 
   
 
How to prevent multiple instances  
One of the best way (and the fastest) is with CreateMutex
--------------------------------

Example:
--------------------------------
program project1;

uses....


begin
// make sure this comes before everything else
CreateMutex(nil, false, ’Someproject’);
if GetLastError = ERROR_ALREADY_EXISTS then begin
SendMessage(HWND_BROADCAST,
RegisterWindowMessage(’Someproject’),
0,
0);
Halt(0);
end;
// and other stuff...
end.  
 
 
0
 
LVL 3

Expert Comment

by:smurff
ID: 6902095
tongalite,

isnt that what Ive put :)

S.
0
 
LVL 3

Expert Comment

by:tongalite
ID: 6902225
Hi smurff..

errrr...well I guess it is now I come to look at your offering... blame sun glare on my screen :-)
T.
0
 
LVL 3

Expert Comment

by:smurff
ID: 6902355
lol the more help the better i suppose :)

Regards
S.
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ddeman not working in activex 3 103
LAN or WAN ? 11 90
Error E2158 compiling with Delphi XE10 Seattle 2 99
When i run adoquery my application freezes 26 165
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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

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