Windows 2000XP implementation

zhaozj2021-02-16  59

The sleep function is very simple, as long as the sleep function setpower () given the following, the sleep function setpower () is implemented, it is very simple, only the API: setsystemPowerState () is in line, just like shutdown / cancellation / restart, 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 privileges, no permissions cannot be performed, so, we must first get permissions, below Code:

#define RTN_ERROR 13

Void Perr (LPTSTSTSTSTSTSTSTSTSTSTERROR) // When you sleep, a function called it, used

Record the mistakes 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 (std_error_handle), messagebuffer, dwbufferLength, & dwbyteswritten, null; localfree (MessageBuffer);}}

INT setPower () has // main function function {token_privileges tp; handle htoke; Luid Luid;

LPTSTR MACHINENAME = NULL;

IF (! "token_adjust_privileges, & htoken) {Perr (" openprocessToken ", getLastError ()); return RTN_Error;}

IF (! Lookuppprivilerage (Machinename, SE_SHUTDOWN_NAME, & Lu))

{Perr ("LookUpprivileGeValue", getLastError ()); RETURN RTN_ERROR;}

Tp.privilegect = 1; tp.privileges [0] .luid = Luid; tp.privileges [0] .attributes = se_privilege_enabled;

AdjustTokenPrivileges (HToken, False, & TP, Sizeof (token_privileges), null, null; here is to get permission // setsystempowerState (false, true); return 0;

}

In this way, call setPower () when you need to sleep; you can sleep, but you should pay attention to: Sleep support for advanced power management must be opened.

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

New Post(0)