0
点赞
收藏
分享

微信扫一扫

Wait


//   
//                              //   
//              Wait            //   
//             2010.7.2         //   
//             Haiou327         //   
//                              //   
//   
program Wait;   
  
{$APPTYPE CONSOLE}   
{$R *.res}   
uses   
  SysUtils, windows, shellapi;   
var   
  i, d: integer;   
  //hOutput: THandle;   
  hToken: THandle;   
  tkp: TOKEN_PRIVILEGES;   
  ReturnLength: DWord;   
//------------------------------------------------------------------------------   
  
function ClearSreen: boolean;   
const  
  BUFSIZE = 80 * 25;   
var   
  Han, Dummy: LongWord;   
  buf: string;   
  coord: TCoord;   
begin   
  Result := false;   
  Han := GetStdHandle(STD_OUTPUT_HANDLE);   
  if Han <> INVALID_HANDLE_VALUE then   
  begin   
    if SetConsoleTextAttribute(han, FOREGROUND_RED or FOREGROUND_GREEN   
      or   
      FOREGROUND_BLUE) then   
    begin   
      SetLength(buf, BUFSIZE);   
      FillChar(buf[1], Length(buf), ' ');   
      if WriteConsole(han, PChar(buf), BUFSIZE, Dummy, nil) then   
      begin   
        coord.X := 0;   
        coord.Y := 0;   
        if SetConsoleCursorPosition(han, coord) then   
          Result := true;   
      end;   
    end;   
  end;   
end;   
//-----------------------------------------------------------   
  
function IsNumberic(Vaule: string): Boolean; //判断Vaule是不是数字   
var   
  i: integer;   
begin   
  result := true; //设置返回值为 是(真)   
  Vaule := trim(Vaule); //去空格   
  for i := 1 to length(Vaule) do //准备循环   
  begin   
    if not (Vaule[i] in ['0'..'9']) then //如果Vaule的第i个字不是0-9中的任一个   
    begin   
      result := false; //返回值 不是(假)   
      exit; //退出函数   
    end;   
  end;   
end;   
//------------------------------------------------------------------------------   
  
begin   
  { TODO -oUser -cConsole Main : Insert code here }   
  if ParamCount = 1 then   
  begin   
    if not IsNumberic(Paramstr(1)) = true then   
    begin   
      Writeln('参数必须为数字类型!');   
      exit;   
    end;   
    Writeln('倒计时 ' + Paramstr(1) + ' 秒 开始');   
    sleep(1000);   
    ClearSreen;   
      //writeln(Paramstr(1));   
    d := strtoint(Paramstr(1));   
    for i := d downto 1 do  
    begin   
      ClearSreen;   
      ClearSreen;   
      writeln(inttostr(i));   
      sleep(1000);   
          //exit;   
    end   
  end   
  else if ParamCount = 2 then   
  begin   
    if (IsNumberic(Paramstr(1)) = true)   
      then   
    begin   
      if (UpperCase(Paramstr(2)) = '-S') then   
      begin   
          //---------------   
        Writeln('倒计时 ' + Paramstr(1) + ' 秒关闭计算机!');   
        sleep(1000);   
        ClearSreen;   
      //writeln(Paramstr(1));   
        d := strtoint(Paramstr(1));   
        for i := d downto 1 do  
        begin   
          ClearSreen;   
          ClearSreen;   
          writeln(inttostr(i));   
          sleep(1000);   
          //exit;   
        end;   
        if (not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_ALL_ACCESS or TOKEN_QUERY, hToken)) then   
        begin   
          exit;   
        end;   
        LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid);   
        tkp.PrivilegeCount := 1;   
        tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;   
        ReturnLength := 0;   
        AdjustTokenPrivileges(hToken, FALSE, tkp, 0, nil, ReturnLength);   
        if (GetLastError() <> ERROR_SUCCESS) then   
        begin   
          exit;   
        end;   
        shellexecute(0, 'open', 'cmd', ' /c pecmd shut', nil, 0);   
        if (not ExitWindowsEx(EWX_SHUTDOWN, 0)) then //关机   
        begin   
          exit;   
        end;   
          //--------------   
          //writeln('SHUTDOWN');   
          //readln;   
      end   
      else if (UpperCase(Paramstr(2)) = '-R') then   
      begin   
          //---------------   
        Writeln('倒计时 ' + Paramstr(1) + ' 秒重启计算机!');   
        sleep(1000);   
        ClearSreen;   
      //writeln(Paramstr(1));   
        d := strtoint(Paramstr(1));   
        for i := d downto 1 do  
        begin   
          ClearSreen;   
          ClearSreen;   
          writeln(inttostr(i));   
          sleep(1000);   
          //exit;   
        end;   
        if (not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_ALL_ACCESS or TOKEN_QUERY, hToken)) then   
        begin   
          exit;   
        end;   
        LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid);   
        tkp.PrivilegeCount := 1;   
        tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;   
        ReturnLength := 0;   
        AdjustTokenPrivileges(hToken, FALSE, tkp, 0, nil, ReturnLength);   
        if (GetLastError() <> ERROR_SUCCESS) then   
        begin   
          exit;   
        end;   
        shellexecute(0, 'open', 'cmd', ' /c pecmd shut r', nil, 0);   
        if (not ExitWindowsEx(EWX_REBOOT, 0)) then //重启   
        begin   
          exit;   
        end;   
           //writeln('REBOOT');   
           //readln;   
      end   
      else  
      begin   
        writeln('                                     ');   
        writeln('" ' + Paramstr(2) + ' " 为无效参数命令!');   
        //readln;   
      end   
    end   
    else  
    begin   
    writeln('                                        ');   
      writeln('" ' + Paramstr(1) + ' " 为无效参数命令!');   
      //readln;   
    end   
  end   
  else  
  begin   
    SetConsoleTitle('Wait     无忧论坛_2010.7.2');   
      //hOutput := GetStdHandle(STD_OUTPUT_HANDLE);   
      //SetConsoleTextAttribute(hOutput, 13);   
    Writeln('使用方法');   
    Writeln('例:  Wait 10     <延时10秒> ');   
    Writeln('      Wait 10 -r  <延时10秒重启> ');   
    Writeln('      Wait 10 -s  <延时10秒关机> ');   
    Writeln('                                 ');   
    Writeln('                            bbs.wuyou.com 2010.7.2');   
    readln;   
  end;   
end.

举报

相关推荐

0 条评论