1 1: Gets the user name of the program run2 functiongetprocessidentity (): String;3 var4 Htoken:thandle;5 UserName, userdomain:string;6 CBName, Cbdomainname:ulong;7 Returnlength:dword;8Buff:Array ofByte;9 Tu:ptokenuser;Ten Peuse:sid_name_use; One label A Cleanup; - begin -Result: ="'; the //Open a Process token - if notOpenProcessToken (GetCurrentProcess, maximum_allowed, Htoken) ThenExit; - - //querying user account tokens + - //first get the required buffer size + if notGetTokenInformation (Htoken, Tokenuser,Nil,0, Returnlength) Then A ifGetLastError = Error_insufficient_buffer Then at begin - //Set Buffer size - SetLength (Buff, returnlength); - //Get Data -GetTokenInformation (Htoken, Tokenuser, @Buff [0], returnlength, returnlength); -Tu: = Ptokenuser (@Buff [0]); in End - Else to GotoCleanup + Else - GotoCleanup; the * //querying the user name and login domain via SID $CBName: =0;Panax NotoginsengCbdomainname: =0; - //get the required buffer size the if notLookupAccountSid (Nil, Tu. User.sid,Nil, CBName,Nil, Cbdomainname, Peuse) Then + ifGetLastError = Error_insufficient_buffer Then A begin the //Set string length (contains NULL characters) + SetLength (UserName, cbname); - SetLength (UserDomain, cbdomainname); $ //Get Data $ ifLookupAccountSid (Nil, Tu. User.sid, @UserName [1], CBName, @UserDomain [1], Cbdomainname, Peuse) Then - begin - //truncate the last NULL character the SetLength (UserName, cbname); - SetLength (UserDomain, cbdomainname);Wuyi End the Else - GotoCleanup; Wu End - Else About GotoCleanup $ Else - GotoCleanup; - - //Combination Information AResult: = UserDomain +'\'+UserName; + the Cleanup: - //Turn off Tokens $ CloseHandle (htoken); the End; the the 2: Gets whether the user is running in Administrator mode the functiongetprocessisadmin (): Boolean; - var in Htoken:thandle; the UserName, userdomain:string; the CBName, Cbdomainname:ulong; About Returnlength:dword; theBuff:Array ofByte; theBuffA:Array[0..3] ofByte; the tu:ptokenelevation; + Peuse:sid_name_use; - te:ptokenelevation; the beginBayiResult: =False; the if notOpenProcessToken (GetCurrentProcess, maximum_allowed, Htoken) ThenExit; the //querying user account tokens - //first get the required buffer size - if notGetTokenInformation (Htoken, tokenelevation, @BuffA [0],4, Returnlength) Then the begin the ifGetLastError = Error_insufficient_buffer Then the begin the //Set Buffer size - SetLength (Buff, returnlength); the //Get Data theGetTokenInformation (Htoken, tokenelevation, @Buff [0], returnlength, returnlength); theTu: = Ptokenelevation (@Buff [0]);94Result:=tu. Tokeniselevated=1; the End ; the End Else the begin98Tu: = Ptokenelevation (@BuffA [0]); AboutResult:=tu. Tokeniselevated=1; - End;101 //Turn off Tokens102 CloseHandle (htoken);103 End;
View Code
With Delphi written Exe,vista system and above system, start EXE need administrator rights as long as add UAC,XP system does not support UAC, need to determine whether the administrator is started.
1 var2 Mutex:hwnd;3 4 begin5 if notGetprocessisadmin () Then //to determine whether to start with an administrator6 begin7Application.messagebox ('Please start as an administrator','Warning', Mb_iconwarning +MB_OK);8 End9 ElseTen begin OneMutex: = CreateMutex (Nil, False, ' global\xxx ');{Create a single instance} A if(Mutex <>0) and(GetLastError () = error_already_exists)) Then - begin -Application.messagebox ('program already running','Warning', Mb_iconwarning +MB_OK); the End - Else - begin - application.initialize; +Application.mainformontaskbar: =True; -Application.title: ='XXX'; + Application.createform (Tfrmmain, frmmain); AApplication.showmainform: =False; at Application.Run; - End; - End; - End.
View Code
The judgment of a single instance.
Delphi determines if EXE is started as an administrator