DoUnit.pas
上传用户:mjqmds
上传日期:2022-05-05
资源大小:2827k
文件大小:2k
源码类别:

DirextX编程

开发平台:

Delphi

  1. unit DoUnit;
  2. interface
  3. uses Windows;
  4.     function WinExit(iFlags:integer):boolean;
  5.     function SetPrivilege(sPrivilegeName:string; bEnabled:boolean ):boolean;
  6. implementation
  7. {iFlags:
  8.  one of the following must be specified
  9.     EWX_LOGOFF
  10.     EWX_REBOOT
  11.     EWX_SHUTDOWN
  12.  following attributes may be combined with above flags
  13.     EWX_POWEROFF
  14.     EWX_FORCE : terminate processes}
  15.     function WinExit(iFlags:integer):boolean;
  16.     begin
  17.         Result:= True;
  18.         if SetPrivilege('SeShutdownPrivilege', True) then
  19.         begin
  20.             if not ExitWindowsEx( iFlags, 0 ) then
  21.                 Result:= False;
  22.             SetPrivilege('SeShutdownPrivilege',False);
  23.         end
  24.         else
  25.             Result:= False;
  26.     end;
  27.     function SetPrivilege(sPrivilegeName:string; bEnabled:boolean ):boolean;
  28.     var
  29.         TPPrev,TP:TTokenPrivileges;
  30.         Token:THandle;
  31.         dwRetLen:DWord;
  32.     begin
  33.         Result:= False;
  34.         OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);
  35.         TP.PrivilegeCount:= 1;
  36.         if LookupPrivilegeValue(Nil,PChar(sPrivilegeName),TP.Privileges[0].LUID) then
  37.         begin
  38.             if(bEnabled)then //Give this privileges
  39.                 TP.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED
  40.             else //NOT Give this privileges
  41.                 TP.Privileges[0].Attributes:=0;
  42.             dwRetLen:= 0;
  43.             //enables or disables privileges in the specified access token.
  44.             Result:=AdjustTokenPrivileges(Token,False,TP,SizeOf( TPPrev ),TPPrev,dwRetLen);
  45.         end;
  46.         CloseHandle(Token);
  47.     end;
  48. end.