在win2000实现关机的api函数是什么?(50分)

  • 主题发起人 主题发起人 kevinmj
  • 开始时间 开始时间
K

kevinmj

Unregistered / Unconfirmed
GUEST, unregistred user!
我用ExitWindowsEx(EWX_Force or EWX_ShutDown,0)在win98可以实现关机,<br>但在win2000不可以,谁知道啊?
 
我居然想用 ExitWindowsEx() 来骗分。<br><br>快逃!
 
win2000和nt的关机机制和win98不一样<br>必须调用AdjustTokenPrivileges使se_shutdown_name有效<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; procedure DoShutdown;<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; &nbsp;logoff: boolean = false;<br>&nbsp; &nbsp;reboot: boolean = false;<br>&nbsp; &nbsp;warn: boolean = false;<br>&nbsp; &nbsp;downQuick: boolean = false;<br>&nbsp; &nbsp;cancelShutdown: boolean = false;<br>&nbsp; &nbsp;powerOff: boolean = false;<br>&nbsp; &nbsp;timeDelay: integer = 0;<br><br><br>implementation<br><br>{$R *.DFM}<br><br>function HasParam(Opt: Char): Boolean;<br>var <br>&nbsp; &nbsp;x: integer; <br>begin <br>&nbsp; &nbsp; &nbsp;result := false; <br>&nbsp; &nbsp; &nbsp;for x := 1 to paramCount do <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (paramstr(x) = '-'+opt) or (paramstr(x) = '/'+opt) then result := true; <br>end; <br><br>function GetErrorString: String; <br>var <br>&nbsp; &nbsp;lz: Cardinal; <br>&nbsp; &nbsp;err: array[0..512] of Char; <br>begin <br>&nbsp; &nbsp; &nbsp;lz := GetLastError; <br>&nbsp; &nbsp; &nbsp;FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, lz, 0, @err, 512, nil);<br>&nbsp; &nbsp; &nbsp;result := string(err);<br>end; <br><br>procedure Tform1.DoShutdown;<br>var <br>&nbsp; &nbsp;rl,flgs: Cardinal; <br>&nbsp; &nbsp;hToken: Cardinal; <br>&nbsp; &nbsp;tkp: TOKEN_PRIVILEGES; <br>begin <br>&nbsp; &nbsp; &nbsp;flgs := 0; <br>&nbsp; &nbsp; &nbsp;if downQuick then flgs := flgs or EWX_FORCE; <br>&nbsp; &nbsp; &nbsp;if not reboot then flgs := flgs or EWX_SHUTDOWN; <br>&nbsp; &nbsp; &nbsp;if reboot then flgs := flgs or EWX_REBOOT; <br>&nbsp; &nbsp; &nbsp;if poweroff and (not reboot) then flgs := flgs or EWX_POWEROFF; <br>&nbsp; &nbsp; &nbsp;if logoff then flgs := (flgs and (not (EWX_REBOOT or EWX_SHUTDOWN or EWX_POWEROFF))) or EWX_LOGOFF; <br>&nbsp; &nbsp; &nbsp;if Win32Platform = VER_PLATFORM_WIN32_NT then begin <br>&nbsp; &nbsp; &nbsp; &nbsp; if not OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.add('Cannot open process token. ['+GetErrorString+']')<br>&nbsp; &nbsp; &nbsp; &nbsp; else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) then begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.PrivilegeCount := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if GetLastError &lt;&gt; ERROR_SUCCESS then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.add('Error adjusting process privileges.'); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end else memo1.lines.add('Cannot find privilege value. ['+GetErrorString+']');<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if CancelShutdown then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if AbortSystemShutdown(nil) = False then <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add('Cannot abort. [/'+GetErrorString+'/]')<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.add('Cancelled./')<br>&nbsp; &nbsp; &nbsp; &nbsp; else begin <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if InitiateSystemShutdown(nil, nil, timeDelay, downQuick, Reboot) = False then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add('Cannot go down. [/'+GetErrorString+'/]')<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.add('Shutting down!/'); <br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end <br>&nbsp; &nbsp; &nbsp;else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExitWindowsEx(flgs, 0); <br>&nbsp; &nbsp; &nbsp;end; <br>end; <br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp;memo1.lines.add('Shutdown v0.3 for Win32 (similar to the Linux version)');<br>&nbsp; &nbsp; &nbsp;memo1.lines.add('(c) 2000 NeuralAbyss Software. All Rights Reserved.');<br>&nbsp; &nbsp; &nbsp;if HasParam('?') or (ParamCount=0) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add('Usage: &nbsp; &nbsp;shutdown [-akrhfnc] [-t secs]');<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-k: &nbsp; &nbsp; &nbsp;don''t really shutdown, only warn.');<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-r: &nbsp; &nbsp; &nbsp;reboot after shutdown.');<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-h: &nbsp; &nbsp; &nbsp;halt after shutdown.');<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-p: &nbsp; &nbsp; &nbsp;power off after shutdown');<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-l: &nbsp; &nbsp; &nbsp;log off only');<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-n: &nbsp; &nbsp; &nbsp;kill apps that don''t want to die.');<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-c: &nbsp; &nbsp; &nbsp;cancel a running shutdown.');<br>&nbsp; &nbsp; &nbsp;end else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if HasParam('k') then warn := true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if HasParam('r') then reboot := true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if HasParam('h') and reboot then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add('Error: Cannot specify -r and -h parameters together!');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if HasParam('h') then reboot := false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if HasParam('n') then downQuick := true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if HasParam('c') then cancelShutdown := true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if HasParam('p') then powerOff := true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if HasParam('l') then logoff := true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DoShutdown;<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reboot := true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoShutdown;<br><br>end;<br>end.<br><br>
 
哇塞,太酷了.楼上的兄弟真行.
 
&lt;b&gt;&lt;i&gt;&lt;font size=8 face=宋体&gt;&lt;font color=#FF0000&gt;威猛!&lt;/font&gt;&lt;/i&gt;&lt;/b&gt;
 
接受答案了.
 
后退
顶部