注册 登录
编程论坛 Delphi论坛

第二天的关机程序实现不了

s912360101 发布于 2011-09-10 10:35, 546 次点击
有个朋友拜托我写了一个程序,第二天的凌晨2点关机的,可是第二天的时候我去单位的机房一看,居然没有关机,下面是代码,白天的时候成功了,GOOGLE和BAIDU了,也没有什么好的结果,哎~```
程序代码:

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Timer2: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
  private
    procedure GetPrivilege;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.GetPrivilege;
var
  NewState:       TTokenPrivileges;
  lpLuid:         Int64;
  ReturnLength:   DWord;
  ToKenHandle:    Cardinal;
begin
  OpenProcessToken(GetCurrentProcess,
                   TOKEN_ADJUST_PRIVILEGES
                   OR TOKEN_ALL_ACCESS
                   OR STANDARD_RIGHTS_REQUIRED
                   OR TOKEN_QUERY,ToKenHandle);
  LookupPrivilegeValue(nil,'SeShutdownPrivilege',lpLuid);
  NewState.PrivilegeCount:=1;
  NewState.Privileges[0].Luid:=lpLuid;
  NewState.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
  ReturnLength:=0;
  AdjustTokenPrivileges(ToKenHandle,False,NewState,0,nil,ReturnLength);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
   Form1.Hide;
   Timer2.Enabled:=True;

end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
    if TimetoStr(Time)='2:15:00' then
    begin
      GetPrivilege;
      ExitWindowsEx(EWX_SHUTDOWN OR EWX_POWEROFF, 0);
    end;
end;
end.

4 回复
#2
s9123601012011-09-10 10:37
太丟人,,,自己
#3
yuutian2011-09-12 21:30
呵呵,知道问题了
#4
冰蟾子2011-09-13 14:42
没测试关机的代码,不过如果你的电脑日期格式为hh:mm:ss话的
不如把
if TimetoStr(Time)='2:15:00' then  //改为if Time = StrToTime('2:15:00') then试试
希望对你有帮助
#5
ddkk1232011-12-03 20:04
改为:
  If FormatDateTime('HH:MM:SS',Time)='02:15:00' Then
1