关于关机(50分)

  • 主题发起人 主题发起人 fire.bruin
  • 开始时间 开始时间
F

fire.bruin

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了个CONSOLE应用程序,应用程序里有ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE or EWX_POWEROFF,0)语句,但关不了机!但是做成windows窗体应用程序就可以关机,怎么回事?<br><br>program closepc;<br><br>{$APPTYPE CONSOLE}<br><br>uses<br> &nbsp;SysUtils,<br> &nbsp;Windows;<br><br>var<br> &nbsp;i &nbsp; &nbsp; &nbsp; : integer;<br><br>begin<br> &nbsp;i:=0;<br> &nbsp;while true do<br> &nbsp;begin<br> &nbsp; &nbsp; if i&gt;2000 then &nbsp; ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE or EWX_POWEROFF,0) <br> &nbsp; &nbsp; else i:=i+1<br> &nbsp;end;<br>end.<br><br><br>基本就是这样的!
 
又想干坏事!
 
WinNT以上关机需要权限<br>procedure TForm1.AdjustToken;{NT内核电脑关机需要通过本函数获取特权}<br>var<br> &nbsp;hdlProcessHandle: Cardinal;<br> &nbsp;hdlTokenHandle: Cardinal;<br> &nbsp;tmpLuid: Int64;<br> &nbsp;tkp: TOKEN_PRIVILEGES;<br> &nbsp;tkpNewButIgnored: TOKEN_PRIVILEGES;<br> &nbsp;lBufferNeeded: Cardinal;<br> &nbsp;Privilege: array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br> &nbsp;hdlProcessHandle := GetCurrentProcess;<br> &nbsp;OpenProcessToken(hdlProcessHandle,<br> &nbsp;(TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY),hdlTokenHandle);<br> &nbsp;LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br> &nbsp;Privilege[0].Luid := tmpLuid;<br> &nbsp;Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br> &nbsp;tkp.PrivilegeCount := 1; // One privilege to set<br> &nbsp;tkp.Privileges[0] := Privilege[0];<br> &nbsp;AdjustTokenPrivileges(hdlTokenHandle,False,tkp,Sizeof(tkpNewButIgnored),<br> &nbsp;tkpNewButIgnored,lBufferNeeded);<br>end;<br><br> &nbsp; &nbsp;AdjustToken;<br> &nbsp; &nbsp;ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF,0);
 
program myreboot; <br><br>{$APPTYPE CONSOLE} <br>uses <br> SysUtils,Windows; <br><br>function My_ExitWindows(M_1Reboot_2Power: integer): Boolean; //重启(M:=1).关机(M:=2) <br>var <br>hToken: THANDLE; <br>hProc: THANDLE; <br>mLUID: TLargeInteger; <br>mPriv, mNewPriv: TOKEN_PRIVILEGES; <br>mBufferLength: DWord; <br>begin <br>Result:=false; <br>if not (M_1Reboot_2Power in [1, 2]) then exit; <br>hProc := GetCurrentProcess(); <br>OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken); <br>LookupPrivilegeValue('', 'SeShutdownPrivilege', mLUID); <br>mPriv.PrivilegeCount := 1; <br>mPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED; <br>mPriv.Privileges[0].Luid := mLUID; <br>AdjustTokenPrivileges(hToken, False, mPriv, (4 + (12 * mPriv.PrivilegeCount)), mNewPriv, mBufferLength); <br>GetLastError; <br>case M_1Reboot_2Power of <br>1: Result := ExitWindowsEx(EWX_FORCE+EWX_REBOOT,0); <br>2: Result := ExitWindowsEx(EWX_FORCE+EWX_POWEROFF, 0); <br>end; <br>end; <br><br>//===================== <br>procedure AdjustToken;//改变进程权限 <br>var <br> hdlProcessHandle : Cardinal; <br> hdlTokenHandle : Cardinal; <br> tmpLuid : Int64; <br> tkpPrivilegeCount : Int64; <br> tkp : TOKEN_PRIVILEGES; <br> tkpNewButIgnored : TOKEN_PRIVILEGES; <br> lBufferNeeded : Cardinal; <br> Privilege : array[0..0] of _LUID_AND_ATTRIBUTES; <br>begin <br> &nbsp; &nbsp; &nbsp; &nbsp;hdlProcessHandle := GetCurrentProcess; <br> &nbsp; &nbsp; &nbsp; &nbsp;OpenProcessToken(hdlProcessHandle, <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hdlTokenHandle); <br><br> &nbsp; &nbsp; &nbsp; &nbsp;// Get the LUID for shutdown privilege. <br> &nbsp; &nbsp; &nbsp; &nbsp;LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid); <br> &nbsp; &nbsp; &nbsp; &nbsp;Privilege[0].Luid := tmpLuid; <br> &nbsp; &nbsp; &nbsp; &nbsp;Privilege[0].Attributes := SE_PRIVILEGE_ENABLED; <br> &nbsp; &nbsp; &nbsp; &nbsp;tkp.PrivilegeCount := 1; &nbsp; // One privilege to set <br> &nbsp; &nbsp; &nbsp; &nbsp;tkp.Privileges[0] := Privilege[0]; <br> &nbsp; &nbsp; &nbsp; &nbsp;// Enable the shutdown privilege in the access token of this <br> &nbsp; &nbsp; &nbsp; &nbsp;// process. <br> &nbsp; &nbsp; &nbsp; &nbsp;AdjustTokenPrivileges(hdlTokenHandle, <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;False, <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp, <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Sizeof(tkpNewButIgnored), <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkpNewButIgnored, <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lBufferNeeded); <br><br>end; <br><br><br>begin <br> { TODO -oUser -cConsole Main : Insert code here } <br> &nbsp; &nbsp;AdjustToken;//改变进程权限 <br> &nbsp; &nbsp; ExitWindowsEx((EWX_REBOOT OR EWX_FORCE), $FFFF); <br>// My_ExitWindows(1); <br>end.
 
忘了写一句,我的程序是在WIN98下运行!不存在提升权限的问题!
 
试试我说的那个行吗?
 

Similar threads

S
回复
0
查看
896
SUNSTONE的Delphi笔记
S
S
回复
0
查看
873
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部