Realize sleep and shutdown under 982000XP

xiaoxiao2021-03-05  26

[Introduction] The sleep function is very simple, as long as the sleep function setpower () is implemented as follows, in fact, sleep is simple, only API: setsystempowerstate () is in line, such as shutdown / logout / restart Same, only EXITWINDOWEX (), but these APIs can be used directly in 98, but they cannot be used in 2000 / XP, because this is involved in user permissions, no permissions cannot be performed, so, we must first take privileges. The code is given below: It is very simple to implement the sleep function, as long as the sleep function setpower () is implemented in any way you want to execute, it is very simple, but you only need API: setsystemPowerState (), such as shutdown / logout / Restart, only exitwindowex (), but these APIs can be used directly under 98, but they cannot be used in 2000 / XP, because this is involved in user rights, no permissions cannot be made, so, first to get Permissions, here is given below: #define RTN_ERROR 13 Void Perr (LPTSTR SZAPI, DWORD DWLASTERROR) // When you call a function to record the error encountered in sleep {lptstr messagebuffer; DWORD DWBUFFERLENGTH; FPRINTF (stderr, "!% s error (rc =% lu) / n", szAPI, dwLastError); if (dwBufferLength = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError, LANG_NEUTRAL, (LPTSTR) & MessageBuffer, 0, NULL)) {DWORD dwBytesWritten; WriteFile (GetStdHandle (std_error_handle), MessageBuffer, DwbufferLength, & DWBYTESWRITEN, NULL; LOCALFREE (MESSAGEBUFER); }} INT SetPower () there // main function of the function {TOKEN_PRIVILEGES tp; HANDLE hToken; LUID luid; LPTSTR MachineName = NULL; (! OpenProcessToken (GetCurrentProcess (), from here TOKEN_ADJUST_PRIVILEGES, & hToken)) if {PERR ( "OpenProcessToken", GetLastError ()); return RTN_ERROR;} if) {PERR ( "LookupPrivilegeValue", GetLastError ()) (LookupPrivilegeValue (MachineName, SE_SHUTDOWN_NAME, & luid!); return RTN_ERROR;} tp.PrivilegeCount = 1; tp.Privileges [0]. Luid = Luid; Tp.Privileges [0] .attributes = se_privilege_enabled; AdjustTokenprivileges (Htoken, False, & TP, SIZEOF (Token_Privileges), NULL, NULL

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

New Post(0)