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 ;}