Solved

Prevend double launching of app

Posted on 2002-03-27
10
219 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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