如何使用DELPHI来实现WINDOWS自动关机?(100分)

  • 主题发起人 主题发起人 hdl18
  • 开始时间 开始时间
我用过ExitWindowsEx,但有几个参数不能用!即没有作用!<br>如:EWX_LOGOFF
 
如为win98,调用exitwindowsex即可,但nt则不行!!!<br>我写了一个winnt服务,可按配制文件重启机器,并给管理员发信!<br>配制文件如下:<br>Frequency=0<br>Hour=5<br>Min=15<br>Port=80<br>Host=192.168.67.44<br>MailHost=mail.263.net.cn<br>MailPort=25<br>UserID=sharkorzyf<br>Subject=Reboot computer warning<br>FromAddress=sharkorzyf@263.net.cn<br>FromName=263card<br>ReplyTo=sharkorzyf@263.net.cn<br>ToAddress=sharkorzyf@263.net.cn<br><br>程序如下:<br><br>unit main;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,<br>&nbsp; ScktComp,Psock, NMsmtp;<br><br>{$DEFINE SENDMAIL}<br>{$DEFINE REBOOT}<br>type<br>&nbsp; TRestartService = class(TService)<br>&nbsp; &nbsp; NMSMTP1: TNMSMTP;<br>&nbsp; &nbsp; procedure ServiceStart(Sender: TService; var Started: Boolean);<br>&nbsp; &nbsp; procedure ServiceCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure ServiceStop(Sender: TService; var Stopped: Boolean);<br>&nbsp; &nbsp; procedure ServicePause(Sender: TService; var Paused: Boolean);<br>&nbsp; &nbsp; procedure ServiceContinue(Sender: TService; var Continued: Boolean);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; function GetServiceController: TServiceController; override;<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>&nbsp; TRestartThread=class(TThread) &nbsp;{定义线程类}<br>&nbsp; &nbsp; protected<br>&nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF SENDMAIL}<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure restartsendmail;<br>&nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; procedure execute;override;<br>&nbsp; end;<br><br>var<br>&nbsp; RestartService: TRestartService;<br><br>implementation<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; Root,Host,s:string;<br>&nbsp; &nbsp; &nbsp; &nbsp; RestartThread:TRestartThread;<br>&nbsp; &nbsp; &nbsp; &nbsp; UnConnectCount:integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; Frequency,Hour,Min,Port:integer;<br><br>{$R *.DFM}<br><br>{自定义函数}<br>procedure savelog(ss:string);<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; f:TextFile;<br>&nbsp; &nbsp; &nbsp; &nbsp; r:string;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; r:=Root+'restart_log.txt';<br>&nbsp; &nbsp; &nbsp; &nbsp; AssignFile(f,r);<br>&nbsp; &nbsp; &nbsp; &nbsp; if(FileExists(r))then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Append(f);<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rewrite(f);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Writeln(f,ss);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Flush(f); &nbsp;{Ensures that the text was actually written to file.}<br>&nbsp; &nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CloseFile(f);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end;<br>{$IFDEF REBOOT}<br>procedure reboot_computer;<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; hToken:THandle;<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp : TOKEN_PRIVILEGES;<br>&nbsp; &nbsp; &nbsp; &nbsp; ReturnLength : DWord;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; {Get a token for this process.<br>&nbsp; &nbsp; &nbsp; &nbsp; 即:初始化hToken。}<br>&nbsp; &nbsp; &nbsp; &nbsp; {About Win API GetCurrentProcess:<br>&nbsp; &nbsp; &nbsp; &nbsp; The GetCurrentProcess function returns a pseudohandle<br>&nbsp; &nbsp; &nbsp; &nbsp; for the current process.}<br>&nbsp; &nbsp; &nbsp; &nbsp; {About Win API OpenProcessToken:<br>&nbsp; &nbsp; &nbsp; &nbsp; If the function succeeds, the return value is nonzero.<br>&nbsp; &nbsp; &nbsp; &nbsp; If the function fails, the return value is zero.}<br>&nbsp; &nbsp; &nbsp; &nbsp; if (not OpenProcessToken(GetCurrentProcess(),<br>&nbsp; &nbsp; &nbsp; &nbsp; TOKEN_ADJUST_PRIVILEGES or &nbsp;{Required to change the privileges specified in an access token.}<br>&nbsp; &nbsp; &nbsp; &nbsp; TOKEN_ALL_ACCESS or &nbsp;{Combines the STANDARD_RIGHTS_REQUIRED standard access rights and all individual access rights for tokens.}<br>&nbsp; &nbsp; &nbsp; &nbsp; TOKEN_QUERY, hToken))then &nbsp;{Required to query the contents of an access token.}<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('Error OpenProcessToken');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; {Get the LUID for the shutdown privilege.}<br>&nbsp; &nbsp; &nbsp; &nbsp; {About Win API LookupPrivilegeValue:<br>&nbsp; &nbsp; &nbsp; &nbsp; The LookupPrivilegeValue function retrieves the locally<br>&nbsp; &nbsp; &nbsp; &nbsp; unique identifier (LUID) used on a specified system to<br>&nbsp; &nbsp; &nbsp; &nbsp; locally represent the specified privilege name.}<br>&nbsp; &nbsp; &nbsp; &nbsp; LookupPrivilegeValue(nil, 'SeShutdownPrivilege',tkp.Privileges[0].Luid);<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.PrivilegeCount := 1; &nbsp;{one privilege to set}<br>&nbsp; &nbsp; &nbsp; &nbsp; tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED; &nbsp;{SE_PRIVILEGE_ENABLED : The privilege is enabled.}<br>&nbsp; &nbsp; &nbsp; &nbsp; ReturnLength :=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; {Get the shutdown privilege for this process.}<br>&nbsp; &nbsp; &nbsp; &nbsp; AdjustTokenPrivileges(hToken, FALSE, tkp, 0,nil, ReturnLength);<br>&nbsp; &nbsp; &nbsp; &nbsp; {Can not test the return value of AdjustTokenPrivileges.}<br>&nbsp; &nbsp; &nbsp; &nbsp; if (GetLastError() &lt;&gt; ERROR_SUCCESS) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('Error AdjustTokenPrivileges');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; {Reboot the system and force all applications to close.}<br>&nbsp; &nbsp; &nbsp; &nbsp; if (not ExitWindowsEx(EWX_REBOOT, 0)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('Error ExitWindowsEx');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end;<br>{$ENDIF}<br><br>{TRestartThread}<br>{$IFDEF SENDMAIL}<br>procedure TRestartThread.restartsendmail;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RestartService.NMSMTP1.PostMessage.Body.Text:='Reboot at '+s;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RestartService.NMSMTP1.Connect;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RestartService.NMSMTP1.SendMail;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RestartService.NMSMTP1.Disconnect;<br>&nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RestartService.NMSMTP1.Abort;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('SendMail error!');<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end;<br>{$ENDIF}<br><br>procedure TRestartThread.execute; &nbsp;{覆盖父类的execute方法}<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; PowerSock:TPowerSock;<br>&nbsp; &nbsp; &nbsp; &nbsp; time_m:integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; i:integer;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; PowerSock:=TPowerSock.Create(nil);<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PowerSock.Host:=Host;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PowerSock.Port:=Port;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sleep(30000); &nbsp;{睡眠30秒}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while not terminated do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sleep(30000); &nbsp;{睡眠30秒}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PowerSock.Connect;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UnConnectCount:=0; &nbsp;{一旦WebServer响应,则连续不响应次数清零。}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(UnConnectCount);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PowerSock.Disconnect;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time_m:=Trunc(Frac(Now)*24*60);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {连续10分钟WebServer不响应重启。}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(UnConnectCount=20)then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('连续10分钟WebServer不响应重启');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s:=DateTimetoStr(Now);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('Reboot at '+s);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('UnConnectCount= '+InttoStr(UnConnectCount));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UnConnectCount:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF SENDMAIL}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Synchronize(restartsendmail);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF REBOOT}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reboot_computer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {到达指定重启的时间重启}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if((Frequency=0)and(time_m=(Hour*60+Min)))then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('到达指定重启的时间重启');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s:=DateTimetoStr(Now);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('Reboot at '+s);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UnConnectCount:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF SENDMAIL}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Synchronize(restartsendmail);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF REBOOT}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reboot_computer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {按每天指定的重启次数重启}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(Frequency&gt;0)and(Frequency&lt;=24)then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i:=1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(i&lt;=Frequency)do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((Trunc((24 div Frequency)*60*i)mod(Trunc(24*60)))=time_m)then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s:=DateTimetoStr(Now);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('Reboot at '+s);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UnConnectCount:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF SENDMAIL}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Synchronize(restartsendmail);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF REBOOT}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reboot_computer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(i);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PowerSock.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>end;<br><br>{TRestartService}<br>procedure ServiceController(CtrlCode: DWord); stdcall;<br>begin<br>&nbsp; RestartService.Controller(CtrlCode);<br>end;<br><br>function TRestartService.GetServiceController: TServiceController;<br>begin<br>&nbsp; Result := ServiceController;<br>end;<br><br>procedure TRestartService.ServiceStart(Sender: TService;<br>&nbsp; var Started: Boolean);<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; l:TStringList;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; savelog('/****************************************************/');<br>&nbsp; &nbsp; &nbsp; &nbsp; savelog('The Restart service start at '+DateTimeToStr(Now));<br>&nbsp; &nbsp; &nbsp; &nbsp; l:=TStringList.Create;<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if FileExists(Root+'config.txt')then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l.LoadFromFile(Root+'config.txt');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {每天重启的次数}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Frequency:=StrtoIntDef(l.Values['Frequency'],0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {默认重启时间为5:15}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hour:=StrtoIntDef(l.Values['Hour'],5);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Min:=StrtoIntDef(l.Values['Min'],15);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {默认端口为80}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Port:=StrtoIntDef(l.Values['Port'],80);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Host:=l.Values['Host'];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {默认Host为127.0.0.1}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(Host='')then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Host:='127.0.0.1';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF SENDMAIL}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.ClearParameters;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.ClearParams:=FALSE;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {指定邮件字体}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.Charset:='GB2312';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {指定超时时间}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.TimeOut:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {指定SMTP服务器端口号}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.Port:=StrtoIntDef(l.Values['MailPort'],25);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {指定SMTP服务器地址}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.Host:=l.Values['MailHost'];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {指定邮件的主题}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.PostMessage.Subject:=l.Values['Subject'];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {指定发件人的E-Mail地址}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.PostMessage.FromAddress:=l.Values['FromAddress'];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {指定发件人的名称}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.PostMessage.FromName:=l.Values['FromName'];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {指定回复地址}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.PostMessage.ReplyTo:=l.Values['ReplyTo'];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {指定SMTP服务器用户名}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.UserID:=l.Values['UserID'];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {指定收信人的地址}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NMSMTP1.PostMessage.ToAddress.Text:=l.Values['ToAddress'];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('Config.txt not exist or Config.txt error!');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; l.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; RestartThread:=TRestartThread.Create(FALSE);<br>&nbsp; &nbsp; &nbsp; &nbsp; Started:=TRUE;<br>end;<br><br>procedure TRestartService.ServiceCreate(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; UnConnectCount:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; Frequency:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; Root:=ExtractFilePath(ParamStr(0));<br>end;<br><br>procedure TRestartService.ServiceStop(Sender: TService;<br>&nbsp; var Stopped: Boolean);<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; RestartThread.Terminate; &nbsp;{线程被终止}<br>&nbsp; &nbsp; &nbsp; &nbsp; Stopped:=TRUE;<br>end;<br><br>procedure TRestartService.ServicePause(Sender: TService;<br>&nbsp; var Paused: Boolean);<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; RestartThread.Suspend; &nbsp;{线程被挂起}<br>&nbsp; &nbsp; &nbsp; &nbsp; Paused:=TRUE;<br>end;<br><br>procedure TRestartService.ServiceContinue(Sender: TService;<br>&nbsp; var Continued: Boolean);<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; RestartThread.Resume; &nbsp;{线程被唤醒}<br>&nbsp; &nbsp; &nbsp; &nbsp; Continued:=TRUE;<br>end;<br><br>end.<br><br><br>
 
faint !<br>怎么不到原来的问题里面检索一下?<br>搞的浪费许多银子!
 
大家来了,有些问题可以兼并了。
 
我编一个程序希望到一定时间,在没有人控制下关闭所有正在运行的程序,关机<br>为此我调用ExitWindowsEx API函数来实现,但有很多问题<br>&nbsp; 1 但有很多问题如果有DOS程序在运行,系统不会关机他回出一提示框要求关掉DOS程序<br>2才能关机如果有DELPHI等程序运行且没有存盘,也不会关机,因为他要询问你是否存盘<br>本人愿奉上100分!!
 
exitwindowex(EWX_FORCE or EWX_POWEROFF,0)<br>warning: there are danger of losting data.<br>search before you ask, hehe
 
吸引人的100分呀!!^-^
 
对于wzpok的问题,我想可以这样解决:<br>1、 当检测到“出一提示框要求关掉DOS程序”时,我们可以模拟按键:<br>&nbsp; &nbsp;keybd_event( VK_RETURN, MapVirtualKey( VK_RETURN, 0 ), 0 , 0 )<br>2、同样,当检测到“询问你是否存盘”<br>&nbsp; keybd_event( VK_tab, MapVirtualKey( VK_TAB, 0 ), 0 , 0 );<br>&nbsp; keybd_event( VK_RETURN, MapVirtualKey( VK_RETURN, 0 ), 0 , 0 );<br>&nbsp;
 
时间太久,强制结束。 &nbsp; &nbsp; wjiachun<br>
 
时间太久,强制结束。 &nbsp; &nbsp; wjiachun<br>
 
接受答案了.
 
后退
顶部