Solved

How do I stop multiple copies of a delphi appication starting

Posted on 2004-04-07
9
272 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
[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
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
[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.

 
LVL 23

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

623 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