delphi 调用ExitWindows 关闭系统


本文整理自网络,侵删。

 
type
  TShutdownAction = (saLogoff, saReboot, saPoweroff);
 
function Shutdown(Action: TShutdownAction; AForce: Boolean): Boolean;
  function EnableShutdownPrivilege: Boolean;
  var
    hProcess, hToken: THandle;
    priv, lastpriv: TOKEN_PRIVILEGES;
    lastlen: Cardinal;
  const
    SE_SHUTDOWN_NAME: PWideChar = 'SeShutdownPrivilege';
  begin
    FillChar(priv, sizeof(priv), 0);
    FillChar(lastpriv, sizeof(priv), 0);
    hProcess := GetCurrentProcess;
    Result := False;
    if OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)
    then
    // 打开当前进程的访问令牌句柄(OpenProcessToken函数调用失败返回值为零)
    begin
      if LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME, priv.Privileges[0].LUID)
      then
      begin
        priv.PrivilegeCount := 1; // 欲调整的权限个数
        priv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
        // 权限的属性,SE_PRIVILEGE_ENABLED为使能该权限
        if AdjustTokenPrivileges(hToken, False, priv, sizeof(priv), lastpriv,
          lastlen) then // 调整访问令牌里的指定权限(AdjustTokenPrivileges函数调用失败返回值为零)
          Result := GetLastError = ERROR_SUCCESS;
      end;
      CloseHandle(hToken);
    end;
  end;
 
var
  AFlags: Integer;
begin
  Result := EnableShutdownPrivilege;
  if Result then // 使能关机特权函数
  begin
    if AForce then
      AFlags := EWX_FORCE
    else
      AFlags := 0;
    case Action of
      saLogoff:
        AFlags := AFlags or EWX_LOGOFF;
      saReboot:
        AFlags := AFlags or EWX_REBOOT;
      saPoweroff:
        AFlags := AFlags or EWX_SHUTDOWN;
    end;
    Result := ExitWindowsEx(AFlags, 0) // 强制关机
  end;
end;

相关阅读 >>

Delphi与汇编杂谈

Delphi 判断特定字符是为单字节还是双字节

Delphi twebbrowser get html source after ajax load

Delphi 利用google api生成二维码图像

Delphi 通得进程id获取主窗口句柄

Delphi 注入win7的单元

Delphi之猥琐的webserver实现

Delphi 屏幕取色

Delphi 编写网址加密解密代码

Delphi canvas.lineto 画线

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...