Solved

Prevend double launching of app

Posted on 2002-03-27
10
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

615 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