Shutdown XP

Top  Previous  Next

                               

!**************************************

! Name: Shut Down Windows XP

! Description:This code shuts down Windows XP. I think this is the only working code up to date...:)

! By: Aseerha

!

!This code is copyrighted and has! limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1654&lngWId=7!for details.!**************************************

 

// To shut down Windows XP, you need to create something which is called a "token"

// and here's the function to do so:

 

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;

 

// To shutdown, just call this command from any procedure:

WindowsExit(EWX_POWEROFF or EWX_FORCE) ;