DoUnit.pas
上传用户:mjqmds
上传日期:2022-05-05
资源大小:2827k
文件大小:2k
- unit DoUnit;
- interface
- uses Windows;
- function WinExit(iFlags:integer):boolean;
- function SetPrivilege(sPrivilegeName:string; bEnabled:boolean ):boolean;
- implementation
- {iFlags:
- one of the following must be specified
- EWX_LOGOFF
- EWX_REBOOT
- EWX_SHUTDOWN
- following attributes may be combined with above flags
- EWX_POWEROFF
- EWX_FORCE : terminate processes}
- function WinExit(iFlags:integer):boolean;
- begin
- Result:= True;
- if SetPrivilege('SeShutdownPrivilege', True) then
- begin
- if not ExitWindowsEx( iFlags, 0 ) then
- Result:= False;
- SetPrivilege('SeShutdownPrivilege',False);
- end
- else
- Result:= False;
- end;
- function SetPrivilege(sPrivilegeName:string; bEnabled:boolean ):boolean;
- var
- TPPrev,TP:TTokenPrivileges;
- Token:THandle;
- dwRetLen:DWord;
- begin
- Result:= False;
- OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);
- TP.PrivilegeCount:= 1;
- if LookupPrivilegeValue(Nil,PChar(sPrivilegeName),TP.Privileges[0].LUID) then
- begin
- if(bEnabled)then //Give this privileges
- TP.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED
- else //NOT Give this privileges
- TP.Privileges[0].Attributes:=0;
- dwRetLen:= 0;
- //enables or disables privileges in the specified access token.
- Result:=AdjustTokenPrivileges(Token,False,TP,SizeOf( TPPrev ),TPPrev,dwRetLen);
- end;
- CloseHandle(Token);
- end;
- end.