Solved

How do I stop multiple copies of a delphi appication starting

Posted on 2004-04-07
9
265 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
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 11

Expert Comment

by:shaneholmes
Comment Utility
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
Comment Utility
great ... got that bit of code I found working fine now thx
0
 

Expert Comment

by:tadoox
Comment Utility
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
Comment Utility
You can also use abfComponents pack (free) they have a nice component called abfOneInstance.
Cheers
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
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 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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

6 Experts available now in Live!

Get 1:1 Help Now