TYZhang 的代码没错,你所说的没反应,只是这条语句(cmdLine := 'c:/windows/RUNDLL32.EXE USER.EXE,EXITWINDOWS')关不了机而已.
那你按我下的试试吧(我在2000下调试通过的.)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin,ShellAPI;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
SpinEdit1: TSpinEdit;
Label4: TLabel;
Button1: TButton;
Label5: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
function ControlWin2K(timer:integer): Boolean;
{ Private declarations }
public
{ Public declarations }
end;
const
DllName = 'Advapi32.dll';
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.ControlWin2K(timer:integer): Boolean;
var
GetDll: HWND;
InitiateSystemShutdownExA: function(
lpMachineName, lpMessage: string;
dwTimeout: DWord;
bForceAppsClosed, bRebootAfterShutdown: Boolean;
dwReason: DWord
): Integer; stdcall;
begin
GetDll := LoadLibrary(DllName);
if GetDll = 0 then
raise Exception.Create('加载Advapi32.dll出错,程序异常终止!');
@InitiateSystemShutdownExA := GetProcAddress(GetDll,'InitiateSystemShutdownExA');
if @InitiateSystemShutdownExA <> nil then
begin
InitiateSystemShutdownExA('127.0.0.1', 'hi', timer,
True, True, 0); //这里的timer就是定时关机的时间(秒为单).
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Present: TDateTime;
Year, Month, Day, Hour, Min, Sec, MSec: Word;
//x:integer;
begin
Present:= Now;
DecodeDate(Present, Year, Month, Day);//可以分解出年,月,日
Label1.Caption := '今天是' + IntToStr(Year)+'年'+IntToStr(Month)+
'月'+IntToStr(Day) + '日 ' ;
DecodeTime(Present, Hour, Min, Sec, MSec);//分解出小时,分钟,秒,毫秒
Label2.Caption := '现在的时间是' +IntToStr(Hour) +'时'+IntToStr(Min) + '分 '+
IntToStr(sec)+'秒'+IntToStr(Msec)+'毫秒';
tag:=Hour*60+Min; //Tag是Form的一个属性,不用重新声明。
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Present: TDateTime;
Hour, Min, Sec, MSec: Word; //小时,分钟,秒,毫秒
t:String;
y:integer;
z:integer;
begin
AbortSystemShutdown('127.0.0.1');
Present:= Now;
DecodeTime(Present, Hour, Min, Sec, MSec);//分解出小时,分钟,秒,毫秒
t:=FloatToStr(spinedit1.value); //取得Tspinedit1中用户设置的分钟数
z:=tag+StrToint(t); //取得spinedit1中用户设置的分钟数
Label5.Caption := '当前时刻距离0:00时刻'+IntToStr(Hour*60+Min)+'分钟'+'你已设定距离0:00时刻' +IntToStr(z) +'分钟后关机';
//Label5.Caption := '现在的时间是' +IntToStr(Hour) +'时'+IntToStr(Min) + '分 ';
y:=0;
while y<z do
begin
Present:= Now;
DecodeTime(Present, Hour, Min, Sec, MSec); //分解出小时,分钟,秒,毫秒
y:=Hour*60+Min; //当前系统时间距离0:00的分钟数
Application.ProcessMessages;
if Application.Terminated then Exit;
end;
//cmdLine := 'c:/windows/notepad.exe'; //跳出循环,打开记事本程序
// cmdLine := 'c:/windows/RUNDLL32.EXE USER.EXE,EXITWINDOWS'; //跳出循环,关机
ControlWin2K(1);
end;
end.
注:你也可以去掉你那部份循环,直接用那个函数就行了.给定timer值,就会按时关机了.