Use shared memory mapping to implement a program that can only be initiated one time limit

Source: Internet
Author: User

=============================================================================////Description: Program single run Detection unit//sgao//Date: 2015-09-25//Note: The handle created by the program is automatically freed after the process ends, so there is no closing handle operation////==========================================================  ===================unit urunoncechecker;interfaceuses Sysutils,windows;type//shared memory information Tshareinfo = record AppHandle:  Thandle;  End  Pshareinfo = ^tshareinfo; Check if the program is running, create shared memory without running//parameter 1: Program ID//Parameter 2: Shared memory information (externally created, internal release)//return value: Whether the program has run function checkorcreate (aguidid:string; var A  Shareinfo:pshareinfo): Boolean; Check whether the program is running//parameter 1: Program ID//Parameter 2: Shared memory information//return value: Whether the program has run function Check (aguidid:string; out ashareinfo:pshareinfo): Boo Lean;implementation//Check whether the program is running, such as not running create shared memory//Parameter 1: Program ID//Parameter 2: Shared memory information (externally created, internally released)//return value: Whether the program has run function checkorcreate (A guidid:string;  var ashareinfo:pshareinfo): Boolean;    var Hmapfile:thandle;  Pinfo:pshareinfo;    Begin Result: = False;    If Ashareinfo = Nil then raise Exception.create (' need to pass in shared memory information ');Check if shared memory has been created hmapfile: = OpenFileMapping (file_map_all_access, False, PChar (Aguidid)); If the shared memory has not been created if Hmapfile = 0 THEN BEGIN//create memory image file--The handle is released when the program exits Hmapfile: = CreateFileMapping ($FFFFFFFF, n      Il, page_readwrite, 0, SizeOf (tshareinfo), PChar (Aguidid));      Mapped memory PInfo: = MapViewOfFile (Hmapfile, file_map_write or file_map_read, 0, 0, 0); Initializes the memory pinfo^.      Apphandle: = Ashareinfo.apphandle;      Dispose (Ashareinfo);    Ashareinfo: = PInfo;      End//shared memory already created else begin Dispose (ashareinfo);      PInfo: = MapViewOfFile (Hmapfile, file_map_write or file_map_read, 0, 0, 0);      Update shared memory information ashareinfo: = PInfo;      CloseHandle (Hmapfile);    Result: = True;  End  End Check whether the program is running//parameter 1: Program ID//Parameter 2: Shared memory information//return value: Whether the program has run function Check (aguidid:string; out ashareinfo:pshareinfo): Boo  Lean  var Hmapfile:thandle;    Begin Result: = False; Check if shared memory has been created hmapfile: = OpenFileMapping (file_map_all_access, False, PChar (AGUidid)); If shared memory has been created if Hmapfile <> 0 THEN begin ashareinfo: = MapViewOfFile (Hmapfile, file_map_write or File_map      _read, 0, 0, 0);      CloseHandle (Hmapfile);    Result: = True;  End End;end.

Test code:

Unit ufrmtest;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls , Rzedit, Urunoncechecker;const mapfile_name_guid_1 = ' {1141D1B7-F276-4F31-BB01-2E2447C256BC} '; const MAPFILE_NAME_    Guid_2 = ' {7ebaaa87-dbac-4f69-8b77-c2d58eeffa38} '; type TForm1 = Class (Tform) Btncheckorcreate:tbutton;    Btncheck:tbutton;    Btncheckorcreate2:tbutton;    Btncheck2:tbutton;    Mmo1:trzmemo;    Procedure Btncheckorcreateclick (Sender:tobject);    Procedure Btncheckclick (Sender:tobject);    Procedure Btncheckorcreate2click (Sender:tobject);  Procedure Btncheck2click (Sender:tobject); Private procedure checkorcreate (aguid:string;    aname:string); Procedure Check (aguid:string;  aname:string); Public {public declarations} End;var form1:tform1;implementation{$R *.dfm}procedure tform1.btncheck2click (Sender: TObject); Begin check (mapfile_name_guid_2, ' B '); end;procedure Tform1.btncheckclick (sender:tobject); Begin Check ( MapfilE_name_guid_1, ' A '); end;procedure Tform1.btncheckorcreate2click (sender:tobject); Begin Checkorcreate (MAPFILE_NAME_ Guid_2, ' B '); end;procedure Tform1.btncheckorcreateclick (sender:tobject); Begin Checkorcreate (Mapfile_name_guid_1, ' A '); end;//check whether the program is running procedure Tform1.check (aguid:string;  aname:string); var Bret:boolean; Pinfo:pshareinfo;begin Mmo1.  Lines.add (' Check ' program ' + AName + ' "-----------------------------');  BRet: = Urunoncechecker.check (Aguid, pInfo); If BRet then begin MMO1.  Lines.add (' program ' + AName + ' "on Run '); End ELSE begin MMO1.  Lines.add (' program ' + AName + ' "not Running"); end;end;//check or create the shared file memory of the program procedure tform1.checkorcreate (aguid:string;  aname:string); var Bret:boolean;  Pinfo:pshareinfo;  Happ:thandle; Htopwindow:hwnd;begin Mmo1.  Lines.add (' Check or create ' program ' + AName + ' "-----------------------------');  New (PInfo);  Pinfo.apphandle: = Application.handle;  BRet: = Urunoncechecker.checkorcreate (Aguid, pInfo); If BRet then begin MMO1. Lines.add (' program ' + AName + ' "in the shippingLine ');    SendMessage (Pinfo.apphandle, Wm_syscommand, Sc_restore, 0);    Htopwindow: = Getlastactivepopup (Pinfo.apphandle); if (Htopwindow <> 0) and iswindowvisible (Htopwindow) and iswindowenabled (Htopwindow) then SetForegroundWindow (HT  Opwindow); End ELSE begin MMO1.  Lines.add (' program ' + AName + ' "not running, creating shared memory '); End;end;end.

Operation Result:

Use shared memory mapping to implement a program that can only be initiated one time limit

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.