Implement system shutdown on the operating system of the NT core

xiaoxiao2021-03-06  112

Many times, we may need to perform a shutdown computer or restart your computer, but in the operating system of the Winnt kernel, we cannot only call simple API functions to complete this. This is not a complex problem, but sometimes it will "difficult", if so, now solve this problem method.

*********************************************************** *******************************

Specific use example:

Log out the current user => EXITWIN32SYS (EWX_FORCE OR EWX_LOGOFF);

Restart the computer => EXIXIN32SYS (EWX_FORCE OR EWX_REBOOT);

Turn off the computer => EXIXIN32SYS (EWX_FORCE OR EWX_Poweroff);

*********************************************************** *******************************

Specific code:

Uses

WINDOWS;

Function GetSyStypes: Boolean; // & Get Operating System Type &

Function setprivilege (sprivilegen): boolean; // & set permissions &

Procedure EXITWIN32SYS (iflags: integer); // & Executes to log out, exit or restart the system

IMPLEMentation

Function GetSyStypes: Boolean;

VAR

TOSVERSIONFO;

Begin

Result: = FALSE;

Ver.dwosveionsInfosize: = sizeof (tosversioninfo);

IF getversionex (ver) THEN

IF Ver.dwplatformID = VER_PLATFORM_WIN32_WINDOWS THEN

Result: = TRUE

Else

Result: = FALSE;

END;

Function setPrivilege (SprivileGename: Ansistring; Benable: Boolean): boolean

VAR

TPPREV, TP: TTOKENPRIVILEGES;

TOKEN: THANDLE;

DWRETLEN: DWORD;

Begin

Result: = FALSE;

OpenProcessToken (getCurrentProcess, Token_Adjust_Privileges OR

Token_Query, Tokeen;

Tp.privilegecount: = 1;

If LookuppprivileGeValue (Nil, Pansichar (SprivileGename), Tp.Privileges [0] .luid) THEN

Begin

IF Benable Then

Tp.privileges [0] .attributes: = SE_PRIVILE_ENABED

Else

Tp.privileges [0] .attributes: = 0;

Dwretlen: = 0;

Result: = AdjustTokenPrivileges (Token, False, TP, SizeOf (TPPREV), TPPREV, DWRETLEN;

END;

CloseHandle (TOKEN);

END;

Procedure EXITWIN32SYS (iflags: integer); Begin

IF getsystypes then

EXITWINDOWSEX (iflags, 0)

Else

IF setprivilege ('SESHUTDOWNPRIVILE', TRUE) THEN

IF not exitwindowsex (iflags, 0) THEN

SetPrivilege ('SESHUTDOWNPRIVILEGE', FALSE);

END;

转载请注明原文地址:https://www.9cbs.com/read-101768.html

New Post(0)