怎样在delphi中用命令控制计算机的重起和关机?

解决方案 »

  1.   

    参见msdn
    BOOL ExitWindowsEx(
      UINT uFlags,     // shutdown operation
      DWORD dwReason   // shutdown reason
    );
    uFlags取值:
    关机 EWX_POWEROFF
    关闭windows EWX_SHUTDOWN
    重启:EWX_REBOOT
      

  2.   

    98下用EXWINDOWSEX()
    2000下麻烦一些了.
    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.
      

  3.   

    调用api函数,如上面各位所写!
      

  4.   

    先提高进程的优先级在调用API关机涵数(WIN2000),WIN98下就直接用关机涵数就行了!