Solved

How do I stop multiple copies of a delphi appication starting

Posted on 2004-04-07
9
267 Views
Last Modified: 2010-04-05
I've lost some of my old code notes, one of them to stop multiple instances of a delphi application starting

Has anyone got a snip of code I can include for this in my software please.
0
Comment
Question by:mikelittlewood
9 Comments
 
LVL 6

Expert Comment

by:Amir Azhdari
ID: 10774610
procedure TForm1.FormCreate(Sender: TObject);
var   hMapp : THandle;
begin

     hMapp := CreateFileMapping( THandle( $ffffffff ),
                                   NIL,
                                   PAGE_READONLY,
                                   0,
                                   32,
                                   'MyTest1Map' );
     if Boolean( hMapp ) then
       if GetLastError = ERROR_ALREADY_EXISTS then
          halt;

end;
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 10774811
I tried to use the

  hMutex := CreateMutex(nil, false, 'FSTDERunning');
  if ((GetLastError = ERROR_ALREADY_EXISTS)) then
  begin
    SendMessage(HWND_BROADCAST,RegisterWindowMessage('FSTDERunning'),0,0);
    Messagedlg('FSTDE is already running.',mtError,[mbOK],0);
    Halt(0);
  end;

section of code from another thread but Im not sure what goes in the "USES" section of the dpr
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 10774971
you may also try some free ready to use components at
www.torry.net

more specific

http://www.torry.net/pages.php?id=516

>thread but Im not sure what goes in the "USES" section of the dpr
 it should be the windows-unit

meikl ;-)
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10775309
mikelittlewood....you can use the Hmutex by this way:

uses
windows, system, dialogs;
var
hmutex: Thandle;

HMutex := CreateMutex (nil, False, PChar('FSTDERunning'));
      if WaitForSingleObject (hMutex, 0) = wait_TimeOut then
      begin
                    MessageDlg('FSTDE is already running,mtError,[mbOK],0);
                  Halt(0);
               end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10775319
About.com has a great article on (step By step) creating your own TInstanceControl  
 
Full source code of a Delphi component that can control the behavior of your application's multiple instances: with the option to limit the number of running instances.

"Controlling the number of application instances"
 
http://delphi.about.com/library/weekly/aa110203a.htm

Hope this helps!

Shane
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 10775394
great ... got that bit of code I found working fine now thx
0
 

Expert Comment

by:tadoox
ID: 10775836
The MultInst.pas Unit, Which Permits Only One Application Instance


unit MultInst;
interface
const
MI_QUERYWINDOWHANDLE = 1;
MI_RESPONDWINDOWHANDLE = 2;
MI_ERROR_NONE = 0;
MI_ERROR_FAILSUBCLASS = 1;
MI_ERROR_CREATINGMUTEX = 2;
// Call this function to determine if error occurred in startup.
// Value will be one or more of the MI_ERROR_* error flags.
function GetMIError: Integer;
implementation
uses Forms, Windows, SysUtils;
const
UniqueAppStr = ‘DDG.I_am_the_Eggman!’;
var
MessageId: Integer;
WProc: TFNWndProc;
MutHandle: THandle;
MIError: Integer;
function GetMIError: Integer;
begin
Result := MIError;
end;
function NewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint):
Longint; stdcall;
begin
Result := 0;
// If this is the registered message...
if Msg = MessageID then
begin
case wParam of
MI_QUERYWINDOWHANDLE:
// A new instance is asking for main window handle in order
// to focus the main window, so normalize app and send back
// message with main window handle.
begin
if IsIconic(Application.Handle) then
begin
Application.MainForm.WindowState := wsNormal;
Application.Restore;
end;
PostMessage(HWND(lParam), MessageID, MI_RESPONDWINDOWHANDLE,
Application.MainForm.Handle);
end;
MI_RESPONDWINDOWHANDLE:
// The running instance has returned its main window handle,
// so we need to focus it and go away.
begin
SetForegroundWindow(HWND(lParam));
Application.Terminate;
end;
end;
end
// Otherwise, pass message on to old window proc
else
Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam);
end;
procedure SubClassApplication;
begin
// We subclass Application window procedure so that
// Application.OnMessage remains available for user.
WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC,
Longint(@NewWndProc)));
// Set appropriate error flag if error condition occurred
if WProc = nil then
MIError := MIError or MI_ERROR_FAILSUBCLASS;
end;
procedure DoFirstInstance;
// This is called only for the first instance of the application
begin
// Create the mutex with the (hopefully) unique string
MutHandle := CreateMutex(nil, False, UniqueAppStr);
if MutHandle = 0 then
MIError := MIError or MI_ERROR_CREATINGMUTEX;
end;
procedure BroadcastFocusMessage;
// This is called when there is already an instance running.
var
BSMRecipients: DWORD;
begin
// Prevent main form from flashing
Application.ShowMainForm := False;
// Post message to try to establish a dialogue with previous instance
BSMRecipients := BSM_APPLICATIONS;
BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
@BSMRecipients, MessageID, MI_QUERYWINDOWHANDLE,
Application.Handle);
end;
procedure InitInstance;
begin
SubClassApplication; // hook application message loop
MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr);
if MutHandle = 0 then
// Mutex object has not yet been created, meaning that no previous
// instance has been created.
DoFirstInstance
else
BroadcastFocusMessage;
end;
initialization
MessageID := RegisterWindowMessage(UniqueAppStr);
InitInstance;
finalization
// Restore old application window procedure
if WProc <> Nil then
SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc));
if MutHandle <> 0 then CloseHandle(MutHandle); // Free mutex
end.
0
 
LVL 11

Expert Comment

by:calinutz
ID: 10778630
You can also use abfComponents pack (free) they have a nice component called abfOneInstance.
Cheers
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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…

803 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