我已经已经知道利用exitwindowsex(ewx_reboot,0)进行restart!
  但是在win2k中,只能起到logoff的作用.而且exitwindowsex(ewx_shutdown,0)
  也只是起到了logoff的作用!只对98起作用。
              请指教!!

解决方案 »

  1.   

    控制WINDOWS的开关:如关闭WINDOWS,重新启动WINDOWS等, ExitWindowsEx(UINT uFlags,DWORD dwReserved);是实现这一功能的API函数
    首先定义常数
    const
    EWX_FORCE=4; //关闭所有程序并以其他用户身份登录
    EWX_LOGOFF=0; //重新启动计算机并切换到MS-DOS方式
    EWX_REBOOT=2; //重新启动计算机
    EWX_SHUTDOWN=1;//关闭计算机
    运行时给How赋值,让他等于EWX_SHUTDOWN或其他,调用以下语句
    ExitWindowsEx(How,0);///////////////////////////////////////////
    Function ShutDownWindows(Flags: Byte) : Boolean;
    begin
         Result := ExitWindowsEx(Flage, 0)
         // 如果成功返回 True.
    end;Flags可用参数:EWX_LOGOFF:
      (=0)
      安全地关闭所有进程,并关闭用户登录。EWX_SHUTDOWN:
      (=1)
      关闭系统。所有缓冲区的内容都能被安全的存盘,所有进程都将被停止。
      对于Windows NT: 必须有 SE_SHUTDOWN_NAME 的安全特权方可进行此项操作。
      对于Windows 95: 不必有任何特权。EWX_REBOOT:
      (=2)
      关闭并重新启动系统。
      对于Windows NT: 必须有 SE_SHUTDOWN_NAME 的安全特权方可进行此项操作。
      对于Windows 95: 不必有任何特权。EWX_FORCE:
      (=4)
       强制切断连接,关闭所有应用程序。当使用这个参数时,Windows 将不向正在运行的应用程序发送 WM_QUERYENDSESSION 和 WM_ENDSESSION 消息,有可能造成数据丢失。所以推荐只在紧急时使用这个参数。EWX_POWEROFF:
      (=8)
      关闭系统并切断电源,需要ATX电源支持。
    /////////////////////////////////////
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, 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;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
         AdjustToken;
         ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), $FFFF);
    end;end.
      

  2.   

    procedure TForm1.Button6Click(Sender: TObject);
    begin
      if Application.MessageBox('确定關机?','確認關閉電腦',MB_YESNO )=IDYES then
        begin
          if RadioButton1.Checked=true then
            ExitWindowsEx(EWX_LOGOFF,0) //以其他用戶身份登錄
          else if RadioButton2.Checked=true then
            ExitWindowsEx(EWX_SHUTDOWN,1) //安全關机
          else if RadioButton3.Checked=true then
            ExitWindowsEx(EWX_REBOOT,2) //重新啟動計算机
          else if RadioButton4.Checked=true then
            ExitWindowsEx(EWX_FORCE,4) //強行關机
          else if RadioButton5.Checked=true then
            ExitWindowsEx(EWX_POWEROFF,8); //關閉係統並關閉電源
        end;end;
      

  3.   

    但是这些都适合再98中使用
    在win2k中只有logoff的作用
    不信你试试!
    现在的问题就是如何可以拿到shutdown的权限,用那个函数! 
                          谢谢!指教!!
      

  4.   

    下面一段代码,是实现本地/远程关机:
    unit main;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DsFancyButton;type
      TForm1 = class(TForm)
        EdtRomateName: TEdit;
        BtnShutDown: TButton;
        procedure BtnShutDownClick(Sender: TObject);
      private
        { Private declarations }
        function EnablePrivilege(lpSystemName:PChar; lpName:PChar):Bool;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function TForm1.EnablePrivilege(lpSystemName, lpName: PChar): Bool;
    var
        hToken : THANDLE;
        fOk : Bool;
        fTest : Bool;
        tp, tpNew : TOKEN_PRIVILEGES;
        ReturnLength : Cardinal;
    begin
        fOk := FALSE;
        if OpenProcessToken(GetCurrentProcess,(TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),hToken) then
          begin
              tp.PrivilegeCount := 1;
              if not (LookupPrivilegeValue(lpSystemName,lpName,tp.Privileges[0].Luid)) then
                  ShowMessage('Can not lookup privilege value!');
              tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
              if not (AdjustTokenPrivileges(hToken,FALSE,tp,sizeof(tp),tpNew,ReturnLength)) then
                      ShowMessage('Can not adjust privilege value.');
              if GetLastError = ERROR_SUCCESS then
                  fOk := TRUE
              else
                  fOK := FALSE;
              CloseHandle(hToken);
          end;
          Result := fOk;
    end;procedure TForm1.BtnShutDownClick(Sender: TObject);
    var
        RemoteShutDown : Bool;
        ComputerName : PChar;begin
        if EdtRomateName.Text <> '' then
            begin
                ComputerName := PChar(EdtRomateName.Text;
                EnablePrivilege(ComputerName,'SeRemoteShutdownPrivilege');
                {此句EnablePrivilege(nil,'SeShutdownPrivilege');
                下一句改为RemoteShutDown :=InitiateSystemShutdown(nil,nil,0,TRUE,FALSE);将实现本地关机}
                RemoteShutDown :=InitiateSystemShutdown(ComputerName ,nil,0,TRUE,FALSE);
                {将InitiateSystemShutdown函数的最后一个参数改为TRUE将实现restart}
                if RemoteShutDown then
                    ShowMessage('(远程)关机成功!')
                else
                    ShowMessage('(远程)关机失败!');
            end;
    end;end.代码已经编译通过。
    商榷!
      

  5.   

    我的MSN为[email protected],不要叫我大哥,大家只是共同学习。
      

  6.   

    上面的朋友,我爱死你了,我到处寻找这段代码,我以前有这段代码,不过丢失了。以前使用C++写的,现在看到DELPHI的了。谢谢你。
      

  7.   

    新版MSDN上有C程序实现,,,找找看吧,,
    我看了一下,,和上面几位大哥写的一样,,