Solved

How do I stop multiple copies of a delphi appication starting

Posted on 2004-04-07
9
266 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

18 Experts available now in Live!

Get 1:1 Help Now