Beranda > Delphi > Shut down / reboot Windows 2000 / XP / NT

Shut down / reboot Windows 2000 / XP / NT

Under NT-based platforms, there are TOKEN PRIVILEGES. In these privileges, you have one for Shutdown allowance (“SeShutDownPrivilege”). So, the reason why you can’t shutdown or reboot Win 2K by code (EWX_SHUTDOWN) is that this privilege isn’t enabled. Here is a short snippet that shutdowns Win2K…

You might want to detect which kind of platform runs this code using the Get Windows version sample code.

~~~~~~~~~~~~~~~~~~~~~~~~~
function WindowsExit(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;

Usage:

//Shutdown
WindowsExit(EWX_POWEROFF or EWX_FORCE) ;

//Reboot Windows
WindowsExit(EWX_REBOOT or EWX_FORCE) ;

Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: