//copy from borland//
var
hMutex : Thandle;
WaitResult : word;
BroadcastList : DWORD;
begin
MessageID := RegisterWindowMessage('Check For Choice Previous Inst');
hMutex := createMutex(nil,false,pchar('App_Choice'));
// grab a mutex
handle
WaitResult := WaitForSingleObject(hMutex,10);
// wait to see
if we can have exclusive use of the mutex
if ( waitResult = WAIT_TIMEOUT ) then
// if we can't then
broadcast
the message to make the owner of the mutex respond
{ request that the running application takes focus }
begin
BroadcastList := BSM_APPLICATIONS;
BroadcastSystemMessage(
BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0);
//32 bit - broadcast the
message to all apps - only a prev inst will hear it.
end
else
begin
{do
the normal stuff}
Application.Title := 'Choice Organics Purchase &
Sales System';
Application.CreateForm(TMainForm, MainForm);
Application.Run;
ReleaseMutex(hMutex);
// release the mutex as a politeness
end;
CloseHandle(hMutex);
// close the mutex handle
end.
This goes in the MainForm
procedure Tmainform.OnAppMessage(var Msg : TMsg ;
Var Handled : Boolean);
begin
{ If it's the special message then
focus on this window}
if Msg.Message = MessageID then
// if we get the broadcast message from an
another instance of this app that is trying to start up
begin
show;
WindowState := wsMaximized;
BringToFront;
SetFocus;
Handled := true;
end;
end;
//And this goes in the TMainForm.FormCreate ;-
Application.OnMessage:= OnAppMessage;