Delphi prevents programs from running "turn" multiple times

Source: Internet
Author: User

//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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.