EWX_FORCE=4; //关闭所有程序并以其他用户身份登录
EWX_LOGOFF=0; //重新启动计算机并切换到MS-DOS方式
EWX_REBOOT=2; //重新启动计算机
EWX_SHUTDOWN=1;//关闭计算机
ExitWindowsEx(x,0);x=0 or x=4 时候可以注销计算机,1和2失效,
操作系统是xp和win 2003我想用shutdown命令关机,可不可以实现,怎么寻找系统的system32目录?谢谢

解决方案 »

  1.   

    function ShutDownSystem():BOOL;varhProcess,hAccessToken:THandle;LUID_AND_ATTRIBUTES:TLUIDAndAttributes;TOKEN_PRIVILEGES: TTokenPrivileges;BufferIsNull:DWORD;ConstSE_SHUTDOWN_NAME='SeShutdownPrivilege';beginhProcess:=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_REBOOT, 0);ShutDownSystem:=True;end;
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        ComboBox1: TComboBox;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        function WinExitInNT(iFlags : integer): boolean;
        function SetPrivilege(sPrivilegeName : string; bEnabled : boolean): boolean;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    {$R *.dfm}
    //Enabled or DisEnabled the SE_SHUTDOWN_NAME privilege
    //sPrivilegeName:
    // 'SE_SHUTDOWN_NAME'
    //bEnabled :
    // Enabled or DisEnabled 'SE_SHUTDOWN_NAME' privilege
    function TForm1.SetPrivilege(sPrivilegeName : string; bEnabled : boolean): boolean;
    var
      TPPrev, TP : TTokenPrivileges;
      Token : THandle;
      dwRetLen : DWord;
    begin
      Result := False;
    //opens the access token associated with a process.
      OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);              //handle to process
     //Required to change the privileges specified in an access token.
    //Required to query the contents of an access token.
    TP.PrivilegeCount := 1;
    //retrieves the locally unique identifier (LUID) used on a specified system to
    //locally represent the specified privilege name.
    if LookupPrivilegeValue(Nil, PChar(sPrivilegeName), TP.Privileges[0].LUID) then
    //attempts to find the privilege name on the local system.
    // address of string specifying the privilege
    // address of locally unique identifier
    begin
      if(bEnabled)then //Give this privileges
      begin
      TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
      end
      else begin //NOT Give this privileges
        TP.Privileges[ 0 ].Attributes := 0;
      end;
      dwRetLen := 0;
    //enables or disables privileges in the specified access token.
      Result := AdjustTokenPrivileges(Token, False, TP, Sizeof(TpPrev), TPPrev, dwRetLen);
    // handle to token that contains privileges
    //False
    //modifies privileges
    //TP
    // pointer to new privilege information
    //SizeOf( TPPrev )
    // size
    //in bytes
    //of the TPPrev buffer
    //TPPrev
    // receives original state of changed privileges
    //dwRetLen // receives required size of the TPPrev buffer
      end;
      CloseHandle( Token );
    end;{
    NT中的关闭计算机,重新登陆的实现
    在NT中要实现以上的功能,首先必须获得SE_SHUTDOWN_NAME权限后,才能调用ExitWindowsEx;这是和Windows95的不同
    //iFlags:
    // one of the following must be specified
    // EWX_LOGOFF
    // EWX_REBOOT
    // EWX_SHUTDOWN
    // following attributes may be combined with above flags
    // EWX_POWEROFF
    // EWX_FORCE : terminate processes
    }
    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
      // handle errors...
        Result := False;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      if WinExitInNt(ComboBox1.ItemIndex) then Close;end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      ComboBox1.ItemIndex := 0;
    end;end.
      

  3.   

    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_PRIVI
    LEGES),Nil,BufferIsNull);
      ExitWindowsEx(EWX_REBOOT, 0);
    ShutDownSystem:=True;
    end;