exitwindowsex不起作用,看帮助说是要取得重起系统的权利,请问怎么才能获取权利呢?

解决方案 »

  1.   

    const
      SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';   // Borland forgot this declaration
    var
      hToken: THandle;
      NewTP: TTokenPrivileges;
      PreTP: TTokenPrivileges;
      Zero: DWord;
    begin
      if Application.MessageBox('现在就重启Windows系统,还是待会再说?',
        'Regfere提示您', MB_ICONWARNING + MB_OKCANCEL) = IDOK then
      begin
        OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);
        LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME, NewTP.Privileges[0].Luid);
        NewTP.PrivilegeCount := 1;
        NewTP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
        AdjustTokenPrivileges(hToken, False, NewTP, SizeOf(TTokenPrivileges), PreTP, Zero);
        ExitWindowsEx(EWX_REBOOT, 0);
      end
      else
        Exit;
    end;
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
    procedure AdjustToken();  public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.AdjustToken();
    var
      hdlProcessHandle : Cardinal;
      hdlTokenHandle : Cardinal;
      tmpLuid : Int64;
      tkpPrivilegeCount : Int64;
      tkp : TOKEN_PRIVILEGES;
      tkpNewButIgnored : TOKEN_PRIVILEGES;
      lBufferNeeded : Cardinal;
      Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;
    begin
            hdlProcessHandle := GetCurrentProcess;
            OpenProcessToken(hdlProcessHandle,
                            (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),
                              hdlTokenHandle);        // Get the LUID for shutdown privilege.
            LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
            Privilege[0].Luid := tmpLuid;
            Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
            tkp.PrivilegeCount := 1;  // One privilege to set
            tkp.Privileges[0] := Privilege[0];
            // Enable the shutdown privilege in the access token of this
            // process.
            AdjustTokenPrivileges(hdlTokenHandle,
                                  False,
                                  tkp,
                                  Sizeof(tkpNewButIgnored),
                                  tkpNewButIgnored,
                                  lBufferNeeded);end;
    //******************
    //在Windows2000下关闭计算机
    function ShutDownSystem():BOOL;
    var
      hProcess,hAccessToken:THandle;
      LUID_AND_ATTRIBUTES:TLUIDAndAttributes;
      TOKEN_PRIVILEGES: TTokenPrivileges;
      BufferIsNull:DWORD;
    Const
      SE_SHUTDOWN_NAME='SeShutdownPrivilege';
    begin
      hProcess:=GetCurrentProcess();OpenProcessToken(hprocess,TOKEN_ADJUST_PRIVILEGES+TOKEN_QUERY,hAccessToken);
      LookupPrivilegeValue(Nil,SE_SHUTDOWN_NAME,LUID_AND_ATTRIBUTES.Luid);
      LUID_AND_ATTRIBUTES.Attributes:=SE_PRIVILEGE_ENABLED;
      TOKEN_PRIVILEGES.PrivilegeCount:=1;
      TOKEN_PRIVILEGES.Privileges[0]:=LUID_AND_ATTRIBUTES;
      BufferIsNull:=0;AdjustTokenPrivileges(hAccessToken,False,TOKEN_PRIVILEGES,sizeof(TOKEN_PRIVILEGES),Nil,BufferIsNull);
      ExitWindowsEx(EWX_shutdown, 0);
    ShutDownSystem:=True;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    ShutDownSystem
    end;end.
      

  3.   

    var
      VerInfo: TOSVersionInfo;
      hToken: THANDLE;
      tkp: TOKEN_PRIVILEGES;
      Nothing: Cardinal;
    begin
      VerInfo.dwOSVersionInfoSize := SizeOf(VerInfo);
      GetVersionEx(VerInfo);
        if VerInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
          OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
            hToken);
          LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid);
          tkp.PrivilegeCount := 1;
          tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
          AdjustTokenPrivileges(hToken, FALSE, tkp, 0, nil, Nothing);
        end;
        ExitWindowsEx(EWX_REBOOT, 0);
    end;