Sleep in Windows 2000/XP with VC ++

Source: Internet
Author: User
It is easy to implement the sleep function. You only need to execute the following sleep function setpower () in any place you want to execute. In fact, sleep is easy, just use the API: setsystempowerstate, just like shutdown, logout, and restart, you only need to use exitwindowex (), but these Apis can be used directly under 98, but not in 2000/XP, because user permissions are involved here, you cannot perform these operations without permissions. Therefore, you must first obtain the permissions. The following code is provided:

# Define rtn_error 13

Void Perr (lptstr szapi, DWORD dwlasterror) // a function called during sleep
//// Record errors encountered during 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,
& Dwbyteswritten,
Null );
Localfree (messagebuffer );
}
}


 

***

Int setpower () has // Main Functions
{

Token_privileges TP;
Handle htoken;
Luid;

Lptstr machinename = NULL;

If (! Openprocesstoken (getcurrentprocess (), ////////////////////////////
Token_adjust_privileges,
& Htoken ))
{
Perr ("openprocesstoken", getlasterror ());
Return rtn_error;
}

If (! Lookupprivilegevalue (machinename, se_shutdown_name, & luid ))

{
Perr ("lookupprivilegevalue", getlasterror ());
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); // here, is to get the permission //////////////////////

Setsystempowerstate (false, true );
Return 0;

}

In this way, you can call setpower (); to sleep when you need to sleep, but note that you must turn on the sleep support of advanced power management.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.