请问高人怎么实现在程序中实现让程序关闭并重新启动

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/4876/4876501.xml?temp=.3394434
      

  2.   

    以下是 liangqingzhi(老之) 提供的代码:
    这只是正常关机代码,不是一秒关定义全局变量 send:integer;
    定义过程如下:procedure tform1.ShutDown;
      procedure AdjustToken(); //获取关机控制权
      var
        hdlProcessHandle, hdlTokenHandle, lBufferNeeded: Cardinal;
        tmpLuid: Int64;
      //tkpPrivilegeCount: Int64;
        tkp, tkpNewButIgnored: TOKEN_PRIVILEGES;
        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;
    begin
      AdjustToken;
      case send of
        1: ExitWindowsEx(EWX_FORCE, 0); //在紧急情况下强制关机。 ;
        2: ExitWindowsEx(EWX_LOGOFF, 0); //以其他用户身份登录。 ;
        3: ExitWindowsEx(EWX_POWEROFF, 0); //关闭系统并关闭电源。
        4: if application.MessageBox('是否重新启动', '重启计算机提示框', 1+48)=1 then  //idok
                   begin
                      showmessage('reboot');
                      ExitWindowsEx(EWX_REBOOT, $FFFF); //重启操作系统
                   end
                   else
                     exit;
        5: if application.MessageBox('是否关闭', '关闭计算机提示框', 1+48)=1 then  //idok
                   begin
                      showmessage('close');
                      ExitWindowsEx(EWX_SHUTDOWN, $FFFF); //关机;
                   end
                   else
                     exit;
      else
        EXIT;
      end;
    end;调用方法如下:procedure TForm1.Button1Click(Sender: TObject);
    begin
    send:=3;
    ShutDown;
    end;
      

  3.   

    要重启时,生成一个bat文件,来调用主程序:procedure TForm1.Button1Click(Sender: TObject);
    var f:TextFile;
    begin
      AssignFile(f,'temp.bat');
      Rewrite(f);
      Writeln(f,ExtractFileName(Application.ExeName));
      Writeln(f,'del %0');
      CloseFile(f);
      Winexec('temp.bat',sw_hide);
      Application.Terminate;
      sleep(100);
    end;
      

  4.   

    application.Terminate;//直接关,或者用close;看情况用
      ShellExecute(Application.Handle, 'OPEN', PChar(paramstr(0)),
        '', '', SW_SHOWNORMAL);