Programming for shutdown, standby, and sleep

Source: Internet
Author: User

The initiatesystemshutdown () function is mainly used in the program.

Cstring cpage1: showerror ()
{
Lpvoid lpmsgbuf;
: Formatmessage (
Format_message_allocate_buffer |
Format_message_from_system |
Format_message_ignore_inserts,
Null,
: Getlasterror (),
Makelangid (lang_neutral, sublang_default), // default language
(Lptstr) & lpmsgbuf,
0,
Null
);

Cstring strmsg (lpctstr) lpmsgbuf );
: Localfree (lpmsgbuf );
 
Return strmsg;
}

Shutdown:

Void cpage1: onshutdown ()
{

Getdlgitem (idc_state)-> setwindowtext ("an attempt is being made to shut down the computer. Please wait ...");

Static handle htoken;
Static token_privileges TP;
Static luid;
If (: openprocesstoken (getcurrentprocess (),
Token_adjust_privileges | token_query,
& Htoken ))
{
: Lookupprivilegevalue (null, se_shutdown_name, & luid );
TP. privilegecount = 1;
TP. Privileges [0]. luid = luid;
TP. Privileges [0]. Attributes = se_privilege_enabled;
: Adjusttokenprivileges (htoken, false, & TP, sizeof (token_privileges), null, null );
}

Updatedata (true );
Cstring STR;
Str. Format ("// % s", m_strname );
 
If (! : Initiatesystemshutdown (
(Lptstr) (lpctstr) STR,
"/N sorry, I want to shut down! ",
M_ntime,
M_balert = 0? True: false,
M_ntype = 0? False: True
))
{
Getdlgitem (idc_state)-> setwindowtext (this-> showerror ());
}
Else
Getdlgitem (idc_state)-> setwindowtext ("shutdown successful ");

}

Cancel shutdown:

Void cpage1: onabort ()
{


Getdlgitem (idc_state)-> setwindowtext ("an attempt is being made to cancel shutdown. Please wait ...");

Updatedata (true );
Cstring STR;
Str. Format ("// % s", m_strname );

If (! : Abortsystemshutdown (lptstr) (lpctstr) Str ))
{
Getdlgitem (idc_state)-> setwindowtext (this-> showerror ());
}
Else
Getdlgitem (idc_state)-> setwindowtext ("canceled successfully ");
}

Sleep:

Void cpage1: onxiu ()
{
If (MessageBox ("do you want to sleep? "," Shutdown program ", mb_yesno | mb_defbutton2 | mb_iconquestion) = idyes)
{
Static handle htoken;
Static token_privileges TP;
Static luid;
If (: openprocesstoken (getcurrentprocess (),
Token_adjust_privileges | token_query,
& Htoken ))
{
: Lookupprivilegevalue (null, se_shutdown_name, & luid );
TP. privilegecount = 1;
TP. Privileges [0]. luid = luid;
TP. Privileges [0]. Attributes = se_privilege_enabled;
: Adjusttokenprivileges (htoken, false, & TP, sizeof (token_privileges), null, null );
}
: Setsystempowerstate (false, true );
}
}

For details, see the source code.

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.