//Prevent multiple opening
UnitMultinst;InterfaceusesWindows, Messages, Sysutils, Classes, Forms;ImplementationConstStr_unique='{2be6d96e-827f-4bf9-b33e-8740412cde96}'; Mi_activeapp=1;{Activating the application}Mi_gethandle=2;{Get handle}varImessageid:integer; Oldwproc:tfnwndproc; Muthandle:thandle; Bsmrecipients:dword;functionNewwndproc (Handle:hwnd; Msg:integer; WParam, Lparam:longint): Longint;stdcall;beginResult:=0; ifMSG = Imessageid Then begin CaseWParam ofMi_activeapp:{Activating the application} ifLparam<>0 Then begin {a previous instance of the activation of the message received} {Why do I need to activate in another program?} {because SetForegroundWindow in the same process does not refer to the front of the form} ifIsiconic (LParam) ThenOpenicon (LParam)ElseSetForegroundWindow (LParam); Application.terminate; {Terminate this instance} End; Mi_gethandle:{get the program handle} beginPostMessage (HWND (LParam), Imessageid, Mi_activeapp, Application.handle); End; End; End ElseResult:=CallWindowProc (Oldwproc, Handle, MSG, WParam, LParam);End;procedureInitInstance;begin {Message handling for superseded applications}Oldwproc:=Tfnwndproc (SetWindowLong (Application.handle, GWL_WNDPROC, Longint (@NewWndProc))); {Open Mutex Object}Muthandle:=OpenMutex (mutex_all_access, False, Str_unique); ifMuthandle =0 Then begin {Creating mutex Objects}Muthandle:= CreateMutex (Nil, False, Str_unique); End Else beginApplication.showmainform:=False; {There are already program instances, broadcast messages get instance handles}bsmrecipients:=bsm_applications; Broadcastsystemmessage (Bsf_postmessageorbsf_postmessage, @BSMRecipients, Imessageid,mi_gethandle,application.handle); //broadcastsystemmessage (Bsf_ignorecurrenttask or Bsf_postmessage, @BSMRecipients, Imessageid, Mi_gethandle, Application.handle); End;End;initialization {Register Message}Imessageid:=RegisterWindowMessage (Str_unique); InitInstance;Finalization {restore the message processing process} ifOldwproc <> Nil ThenSetWindowLong (Application.handle, GWL_WNDPROC, Longint (Oldwproc)); {Close Mutex Object} ifMuthandle <>0 ThenCloseHandle (muthandle);End.
The test passes under XE7.
Delphi prevents programs from running "turn" multiple times