shutdown / reboot / logoff Windows 9x/NT/Me/2000/XP?
shutdown / reboot / logoff Windows 9x/NT/Me/2000/XP?
shutdown / reboot / logoff Windows 9x/NT/Me/2000/XP? {1.} function myexitwindows(rebootparam: longword): boolean; var ttokenhd: thandle; ttokenpvg: ttokenprivileges; cbtpprevious: dword; rttokenpvg: ttokenprivileges; pcbtppreviousrequired: dword; tpresult: boolean; const se_shutdown_name = 'seshutdownprivilege'; begin if win32platform = ver_platform_win32_nt then begin tpresult := openprocesstoken(getcurrentprocess(), token_adjust_privileges or token_query, ttokenhd); if tpresult then begin tpresult := lookupprivilegevalue(nil, se_shutdown_name, ttokenpvg.privileges[0].luid); ttokenpvg.privilegecount := 1; ttokenpvg.privileges[0].attributes := se_privilege_enabled; cbtpprevious := sizeof(rttokenpvg); pcbtppreviousrequired := 0; if tpresult then windows.adjusttokenprivileges(ttokenhd, false, ttokenpvg, cbtpprevious, rttokenpvg, pcbtppreviousrequired); end; end; result := exitwindowsex(rebootparam, 0); end; // example to shutdown windows: procedure tform1.button1click(sender: tobject); begin myexitwindows(ewx_poweroff or ewx_force); end; // example to reboot windows: procedure tform1.button1click(sender: tobject); begin myexitwindows(ewx_reboot or ewx_force); end; // parameters for myexitwindows() {************************************************************************} {2. console shutdown demo} program shutdown; {$apptype console} uses sysutils, windows; // shutdown program // (c) 2000 neuralabyss software // www.neuralabyss.com var logoff: boolean = false; reboot: boolean = false; warn: boolean = false; downquick: boolean = false; cancelshutdown: boolean = false; poweroff: boolean = false; timedelay: integer = 0; function hasparam(opt: char): boolean; var x: integer; begin result := false; for x := 1 to paramcount do if (paramstr(x) = '-' + opt) or (paramstr(x) = '/' + opt) then result := true; end; function geterrorstring: string; var lz: cardinal; err: array[0..512] of char; begin lz := getlasterror; formatmessage(format_message_from_system, nil, lz, 0, @err, 512, nil); result := string(err); end; procedure doshutdown; var rl, flgs: cardinal; htoken: cardinal; tkp: token_privileges; begin flgs := 0; if downquick then flgs := flgs or ewx_force; if not reboot then flgs := flgs or ewx_shutdown; if reboot then flgs := flgs or ewx_reboot; if poweroff and (not reboot) then flgs := flgs or ewx_poweroff; if logoff then flgs := (flgs and (not (ewx_reboot or ewx_shutdown or ewx_poweroff))) or ewx_logoff; if win32platform = ver_platform_win32_nt then begin if not openprocesstoken(getcurrentprocess, token_adjust_privileges or token_query, htoken) then writeln('cannot open process token. [' + geterrorstring + ']') else begin if lookupprivilegevalue(nil, 'seshutdownprivilege', tkp.privileges[0].luid) then begin tkp.privileges[0].attributes := se_privilege_enabled; tkp.privilegecount := 1; adjusttokenprivileges(htoken, false, tkp, 0, nil, rl); if getlasterror <> error_success then writeln('error adjusting process privileges.'); end else writeln('cannot find privilege value. [' + geterrorstring + ']'); end; { if cancelshutdown then if abortsystemshutdown(nil) = false then writeln(\'cannot abort. [\' + geterrorstring + \']\') else writeln(\'cancelled.\') else begin if initiatesystemshutdown(nil, nil, timedelay, downquick, reboot) = false then writeln(\'cannot go down. [\' + geterrorstring + \']\') else writeln(\'shutting down!\'); end; } end; // else begin exitwindowsex(flgs, 0); // end; end; begin writeln('shutdown v0.3 for win32 (similar to the linux version)'); writeln('(c) 2000 neuralabyss software. all rights reserved.'); if hasparam('?') or (paramcount = 0) then begin writeln('usage: shutdown [-akrhfnc] [-t secs]'); writeln(' -k: don''t really shutdown, only warn.'); writeln(' -r: reboot after shutdown.'); writeln(' -h: halt after shutdown.'); writeln(' -p: power off after shutdown'); writeln(' -l: log off only'); writeln(' -n: kill apps that don''t want to die.'); writeln(' -c: cancel a running shutdown.'); end else begin if hasparam('k') then warn := true; if hasparam('r') then reboot := true; if hasparam('h') and reboot then begin writeln('error: cannot specify -r and -h parameters together!'); exit; end; if hasparam('h') then reboot := false; if hasparam('n') then downquick := true; if hasparam('c') then cancelshutdown := true; if hasparam('p') then poweroff := true; if hasparam('l') then logoff := true; doshutdown; end; end. // parameters for myexitwindows() ewx_logoff shuts down all processes running in the security context of the process that called the exitwindowsex function. then it logs the user off. alle prozesse des benutzers werden beendet, danach wird der benutzer abgemeldet. ewx_poweroff shuts down the system and turns off the power. the system must support the power-off feature. windows nt/2000/xp: the calling process must have the se_shutdown_name privilege. fдhrt windows herunter und setzt den computer in den standby-modus, sofern von der hardware unterstьtzt. ewx_reboot shuts down the system and then restarts the system. windows nt/2000/xp: the calling process must have the se_shutdown_name privilege. fдhrt windows herunter und startet es neu. ewx_shutdown shuts down the system to a point at which it is safe to turn off the power. all file buffers have been flushed to disk, and all running processes have stopped. if the system supports the power-off feature, the power is also turned off. windows nt/2000/xp: the calling process must have the se_shutdown_name privilege. fдhrt windows herunter. ewx_force forces processes to terminate. when this flag is set, the system does not send the wm_queryendsession and wm_endsession messages. this can cause the applications to lose data. therefore, you should only use this flag in an emergency. die aktiven prozesse werden zwangsweise und ohne rьckfrage beendet. ewx_forceifhung windows 2000/xp: forces processes to terminate if they do not respond to the wm_queryendsession or wm_endsession message. this flag is ignored if ewx_force is used. windows 2000/xp: die aktiven prozesse werden aufgefordert, sich selbst zu beenden und mьssen dies bestдtigen. reagieren sie nicht, werden sie zwangsweise beendet. autor: neuralabyss software homepage: http://www.neuralabyss.com Источник: http://www.swissdelphicenter.ch