Delphi重新启动Windows

use*_*012 4 delphi

我正在使用Windows 10并且以管理员身份登录。

当我尝试重新引导系统时,它所做的只是使我注销。

ExitWindowsEx(EWX_REBOOT and EWX_FORCE, 0);
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我为什么不重启吗?

use*_*012 6

因此,即使我是管理员,似乎也需要使用以下功能设置权限

function NTSetPrivilege(sPrivilege: string; bEnabled: Boolean): Boolean;
var
  hToken: THandle;
  TokenPriv: TOKEN_PRIVILEGES;
  PrevTokenPriv: TOKEN_PRIVILEGES;
  ReturnLength: Cardinal;
begin
  Result := True;
  // Only for Windows NT/2000/XP and later.
  if not (Win32Platform = VER_PLATFORM_WIN32_NT) then Exit;
  Result := False;

  // obtain the processes token
  if OpenProcessToken(GetCurrentProcess(),
    TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
  begin
    try
      // Get the locally unique identifier (LUID) .
      if LookupPrivilegeValue(nil, PChar(sPrivilege),
        TokenPriv.Privileges[0].Luid) then
      begin
        TokenPriv.PrivilegeCount := 1; // one privilege to set

        case bEnabled of
          True: TokenPriv.Privileges[0].Attributes  := SE_PRIVILEGE_ENABLED;
          False: TokenPriv.Privileges[0].Attributes := 0;
        end;

        ReturnLength := 0; // replaces a var parameter
        PrevTokenPriv := TokenPriv;

        // enable or disable the privilege

        AdjustTokenPrivileges(hToken, False, TokenPriv, SizeOf(PrevTokenPriv),
          PrevTokenPriv, ReturnLength);
      end;
    finally
      CloseHandle(hToken);
    end;
  end;
  // test the return value of AdjustTokenPrivileges.
  Result := GetLastError = ERROR_SUCCESS;
  if not Result then
    raise Exception.Create(SysErrorMessage(GetLastError));
end;
Run Code Online (Sandbox Code Playgroud)

像这样 :

procedure TMain.Neustart1Click(Sender: TObject);
const
    SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
  NTSetPrivilege(SE_SHUTDOWN_NAME, True);

  ExitWindowsEx(EWX_REBOOT or EWX_FORCE, 0);
end;
Run Code Online (Sandbox Code Playgroud)

现在可以了。

  • 错误处理在这里严重中断。您需要通过检查返回值来检测错误。API失败后必须立即调用GetLastError。如果您在两者之间调用其他功能,则可能会修改错误代码。调用CloseHandle在这里发生。 (3认同)
  • 同样,具有返回True或引发异常的函数也毫无意义!使用Win32Check使所有错误处理变得简单。 (3认同)

And*_*dam 5

我正在寻找一种更好的解决方案来解决我大约 5 年前所做的问题(发布在下面),我需要对其进行一些调整才能在最新的 Delphi 上运行,旧的 Delphi 版本只需使用 Windows.AdjustTokenPrivileges。下面的代码是从 Windows XP 开始尝试和测试的。小心 - 它有效,请确保在运行之前保存您的工作!

//Uses WinApi.Windows on Latest Delphi 10.3.2
function MyExitWindows(RebootParam: Longword): Boolean;
var
  TTokenHd: THandle;
  TTokenPvg: TTokenPrivileges;


cbtpPrevious: DWORD;
  rTTokenPvg: TTokenPrivileges;
  pcbtpPreviousRequired: DWORD;
  tpResult: Boolean;
const
  SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    tpResult := OpenProcessToken(GetCurrentProcess(),
      TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
      TTokenHd);
    if tpResult then
    begin
      tpResult := LookupPrivilegeValue(nil,
                                       SE_SHUTDOWN_NAME,
                                       TTokenPvg.Privileges[0].Luid);
      TTokenPvg.PrivilegeCount := 1;
      TTokenPvg.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
      cbtpPrevious := SizeOf(rTTokenPvg);
      pcbtpPreviousRequired := 0;
      if tpResult then
        //Older Delphi - replace the WinApi. to read WinApi.AdjustTokenPrivileges
        WinApi.Windows.AdjustTokenPrivileges(TTokenHd,
                                      False,
                                      TTokenPvg,
                                      cbtpPrevious,
                                      rTTokenPvg,
                                      pcbtpPreviousRequired);
    end;
  end;
  Result := ExitWindowsEx(RebootParam, 0);
end;

//Examples
//Shutdown the computer

MyExitWindows(EWX_POWEROFF or EWX_FORCE);

//Reboot the computer
MyExitWindows(EWX_REBOOT or EWX_FORCE);
Run Code Online (Sandbox Code Playgroud)