我的OS是windows NT 5.0
为何运行exitwindowsex(EWX_POWEROFF,0);exitwindowsex(EWX_REBOOT,0);
不能关机和重起!
而其它参数EWX_SHUTDOWN,EWX_FORCEIFHUNG只会注销?
而MSDN帮助说:
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Res section. 
这SE_SHUTDOWN_NAME权限是啥?如何NT下用API关机,重起?

解决方案 »

  1.   

    在NT结构的WINDOWS中,要想用exitwindowsex关机,必须要有关机的权限,所以在调用exitwindowsex函数时必须要先得到关机权限.unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        function WinExitInNT(iFlags: Integer): Boolean;
        function SetPrivilege(sPrivilegeName: string; bEnabled: Boolean): Boolean;
        procedure Button1Click(Sender: TObject);  private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}function Tform1.WinExitInNT(iFlags: Integer ): Boolean;
    begin
     Result := True;
     if (SetPrivilege('SeShutdownPrivilege',True)) then
     begin
       if(not ExitWindowsEx(iFlags,0))then
       begin
         Result := False;
       end;
       SetPrivilege('SeShutdownPrivilege',False)
     end else begin
       Result := False;
     end;
    end; function Tform1.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
       begin
         TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
       end else begin 
         TP.Privileges[ 0 ].Attributes := 0;
       end;
       dwRetLen := 0;
       Result := AdjustTokenPrivileges(Token,False,TP,SizeOf( TPPrev ),TPPrev,dwRetLen);
     end;
     CloseHandle(Token );
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
       WinExitInNT(EWX_POWEROFF);
    end;end.