请问如何做一个定时关机的程序 ( 积分: 100 )

  • 主题发起人 主题发起人 luoyaowen
  • 开始时间 开始时间
L

luoyaowen

Unregistered / Unconfirmed
GUEST, unregistred user!
我的想法是这样<br>一个窗口中,选择你要关机的时间,到时间后会自动关机(包括显示器),重启,注销等功能
 
我的想法是这样<br>一个窗口中,选择你要关机的时间,到时间后会自动关机(包括显示器),重启,注销等功能
 
现在显示器在主机关机的情况都会关机的吧!<br>如果在主机开机状态下可以用如下命令开关显示器:<br>//True时唤醒<br>procedure SwitchMonitor(Open:Boolean);//开关显示器<br>begin<br>SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER,IfThen(Open,-1,2));<br>end;<br><br>要进行关机.在Windows9X下使用API命令:ExitWindowsEx就可以了.<br>在NT系列下则要先获得权限.然后才能关机.<br>如下函数所示<br><br>function SetPrivilege(const sPrivilegeName: string; bEnabled: boolean): boolean;<br>var<br> &nbsp;TPPrev, TP: TTokenPrivileges;<br> &nbsp;Token: THandle;<br> &nbsp;dwRetLen: DWord;<br>begin<br> &nbsp;Result := False;<br> &nbsp;OpenProcessToken(GetCurrentProcess,<br> &nbsp; &nbsp;TOKEN_ADJUST_PRIVILEGES<br> &nbsp; &nbsp;or TOKEN_QUERY,<br> &nbsp; &nbsp;Token);<br> &nbsp;TP.PrivilegeCount := 1;<br> &nbsp;if (LookupPrivilegeValue(nil,<br> &nbsp; &nbsp;PChar(sPrivilegeName),<br> &nbsp; &nbsp;TP.Privileges[0].LUID)<br> &nbsp; &nbsp;) then<br> &nbsp;begin<br> &nbsp; &nbsp;TP.Privileges[0].Attributes := IfThen(bEnabled, SE_PRIVILEGE_ENABLED, 0);<br> &nbsp; &nbsp;dwRetLen := 0;<br> &nbsp; &nbsp;Result := AdjustTokenPrivileges(Token, False,TP, SizeOf(TPPrev),TPPrev,dwRetLen);<br> &nbsp;end;<br> &nbsp;CloseHandle(Token);<br>end;<br><br>function WinExitInNT(iFlags: Integer): Boolean;<br>begin<br> &nbsp;Result := True;<br> &nbsp;if (SetPrivilege('SeShutdownPrivilege', True)) then<br> &nbsp;begin<br> &nbsp; &nbsp;if (not ExitWindowsEx(iFlags, 0)) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Result := False;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;SetPrivilege('SeShutdownPrivilege', False)<br> &nbsp;end<br> &nbsp;else // handle errors...<br> &nbsp; &nbsp;Result := False;<br>end;
 
if datetimetostr(now)=你所定时间 then<br> ExitWindowsEx(EWX_SHUTDOWN and EWX_POWEROFF ,1);
 
在win2k或winXP下定制计划任务执行shutdown.exe
 
一个记时器TIMER<br>还需要下面的函数<br>下面的函数在98,2000,XP下都有效,你自己试试吧.<br>procedure ShutDown(const tpye: integer);<br>const<br> &nbsp;SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';<br>var<br> &nbsp;hToken: THandle;<br> &nbsp;tkp: TTokenPrivileges;<br> &nbsp;tkpo: TTokenPrivileges;<br> &nbsp;zero: DWORD;<br>function GetVersion: string;<br>var<br> &nbsp;VersionInfo: TOSVersionInfo;<br> &nbsp;OSName: string;<br>begin<br> &nbsp;VersionInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);<br> &nbsp;if Windows.GetVersionEx(VersionInfo) then<br> &nbsp;begin<br> &nbsp; &nbsp;with VersionInfo do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;case dwPlatformId of<br> &nbsp; &nbsp; &nbsp; &nbsp;VER_PLATFORM_WIN32s: OSName := 'Win32s';<br> &nbsp; &nbsp; &nbsp; &nbsp;VER_PLATFORM_WIN32_WINDOWS: OSName := 'Windows 95';<br> &nbsp; &nbsp; &nbsp; &nbsp;VER_PLATFORM_WIN32_NT: OSName := 'Windows NT';<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;Result := OSName;<br> &nbsp; &nbsp;end;<br> &nbsp;end<br> &nbsp;else<br> &nbsp; &nbsp;Result := '';<br>end;<br>begin<br> &nbsp;if Pos('Windows NT', GetVersion) = 1 then<br> &nbsp;begin<br> &nbsp; &nbsp;zero := 0;<br> &nbsp; &nbsp;if not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;MessageBox(0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK);<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;if not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;MessageBox(0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK);<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;if not LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;MessageBox(0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK);<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;tkp.PrivilegeCount := 1;<br> &nbsp; &nbsp;tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br> &nbsp; &nbsp;AdjustTokenPrivileges(hToken, False, tkp, SizeOf(TTokenPrivileges), tkpo, zero);<br> &nbsp; &nbsp;if Boolean(GetLastError()) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;MessageBox(0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK);<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;ExitWindowsEx(tpye, 0);<br>end;<br>//tpye定义如下<br>{<br><br> &nbsp;EWX_LOGOFF = 0;<br><br> &nbsp;EWX_SHUTDOWN = 1;<br><br> &nbsp;EWX_REBOOT = 2;<br><br> &nbsp;EWX_FORCE = 4;<br><br> &nbsp;EWX_POWEROFF = 8;<br><br> &nbsp;EWX_FORCEIFHUNG = $10;<br>}
 
后退
顶部