One of Windows Programming Languages: shutdown, restart, and logout

Source: Internet
Author: User

Learning API is to learn to use system functions.

# Include "stdafx. H "# include <tchar. h> # include <windows. h> bool enableshutdownpriv () // This function is the {handle htoken = NULL; token_privileges tkp = {0}; // open the permission token of the current process if (! Openprocesstoken (getcurrentprocess (), token_adjust_privileges | token_query, & htoken) {// openprocesstoken (): Open the permission token of the current process, getcurrentprocess (): process Handle // The second parameter is the token of the current permission, return to the third parameter return false;} If (! Lookupprivilegevalue (null, se_shutdown_name, & tkp. privileges [0]. luid) {// lookupprivilegevalue (): identity for obtaining local permissions // se_shutdown_name: name of the permission to be obtained, which permissions do you have? // after obtaining the permission, use tkp. privileges [0]. luid returns closehandle (htoken); Return false;} tkp. privilegecount = 1; tkp. privileges [0]. attributes = se_privilege_enabled; If (! Adjusttokenprivileges (htoken, false, & tkp, sizeof (token_privileges), null, null) {// improve the access token permission closehandle (htoken); Return false;} return true ;} bool resetwindows (DWORD dwflags, bool bforce) // functions for logout, restart, and shutdown. {// Check the parameter. // dwflags indicates the operation you want to perform, whether to shut down or restart. // bforce indicates whether to force other processes to end, or if (dwflags! = Ewx_logoff & dwflags! = Ewx_reboot & dwflags! = Ewx_shutdown) {return false;} // get the OS version. The function is getversionex () // osversioninfo (earlier version) osversioninfoex (new version) osversioninfo osvi = {0 }; // define the osversioninfo object and clear the osvi. dwosversioninfosize = sizeof (osversioninfo); // obtain the object size if (! Getversionex (& osvi) {// pass osvi to the getversionex () return false;} If (osvi. dwplatformid = ver_platform_win32_nt) {// The result is that the system of the NT type is enableshutdownpriv (); // This function is used to increase the permissions of the current process. // The shutdown, restart, and other operations can be performed after the permission is upgraded.} dwflags |=( bforce! = False )? Ewx_force: ewx_forceifhung; return exitwindowsex (dwflags, 0); // pass dwflags in} int apientry _ twinmain (hinstance, hinstance hprevinstance, lptstr lpcmdline, int ncmdshow) {If (_ tcscmp (lpcmdline, _ T ("/R") = 0) {resetwindows (ewx_reboot, false);} else if (_ tcscmp (lpcmdline, _ T ("/s") = 0) {resetwindows (ewx_shutdown, false);} else if (_ tcscmp (lpcmdline, _ T ("/L ")) = 0) {resetwindows (ewx_logoff, false);} return 0 ;}

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.