Solved

Prevend double launching of app

Posted on 2002-03-27
10
201 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

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

20 Experts available now in Live!

Get 1:1 Help Now