// You can see that the C write WIN32 APIS Console program is very convenient.
#include
#pragma HDRSTOP
Bool SetCurrentPrivilege (LPCTSTSTR Privilege, Bool BenablePrivilege); Void DisplayError (DWORD ERR);
INT Main (int Argc, char * argv []) {INT I, ErrorS = 0, local = 0, timeout = -1, force = 0, reboot = 0, OOPS = 0; char * remote = null, * message = NULL; BOOL RC;
IF (argc == 1) {usage: if (errors) Putchar ('/ n'); PUTS ("Reboot {-l | -s servername} [-m /" message / "] [-t timeout] [ f] [-r] [-oops] / n "); PUTS (" -l reboot local machine "); PUTS (" -s servername reboot named computer); PUTS ("-t timeout show a logoff dialog for < Timeout> Seconds; 0 Suppresses it "); PUTS (" -m / "message /" Display / "Message /" In That Dialog "); PUTS (" -f force apps closed "; PUTS (" -r reboot after Shutdown "); PUTS (" -oops Aborts a Previous Shutdown Request (Only -L, -S Count)); Return 1;}
For (i = 1; i Argv [I]; Switch (* Argv [i]) {CASE 'L': IF (Remote! = Null || Local! = 0) {PUTS ("Only One of -l and -s May BE USED. "); errors;} else local; break; case 's': IF (Remote! = Null || local! = 0) {PUTS (" Only One of -l and -s may be used. " ); errors;} else if (i == argc - 1) {PUTS ("-s must be followed by the name of the server to shut down."); errors;} else { i; Remote = argv [i];} Break; Case 'T': IF (Timeout! = -1) {PUTS ("Only One Timeout May BE Used."); Errors;} else if (i == argc - 1) {PUTS ("-t must be followed by a timeout."); errors;} else { i; timeout = ATOI (Argv [i]);} Break; Case 'm': IF (Message ! = Null) {PUTS ("Only One Message May BE Used."); Errors;} else if (i == argc - 1) {PUTS ("-m Must Be Followed by the message Text."); Errors; } Else { i; message = argv [i];} Break; case 'f': IF (force) {PUTS ("-f may only be used overce."); Errors;} else force Break; Case 'R': IF (Reboot) {PUTS ("-r May Only BE Used Once."); Erroot;} Else Reboot; Break; Case 'O': Oops; Break; DEFAULT: Printf ("/"% s / "is not a valid switch./n" , Argv [I] - 1); Errors; Break;}} IF (Local == 0 && Remote == NULL) {PUTS ("One of -l and -s must be used."); ErrorS } IF (! OOPS & TIMEOUT == -1) // no timeout used {puts ("Anti-goof feature: you must Specify a timeout (0 seconds is ok)"); errors;} IF (errors) goto usage; IF (local) {if (! setCurrentPrivilege (se_shutdown_name, true) {DisplayError (getLastError ()); return 2;}} IF ("/ NTRYING TO ABORT THE SHUTDOWN OF% S ...", Remote? Remote: "this computer"); rc = AbortsystemShutdown (Remote);} else {printf ("/ NTRYING TO SHUT DOWN % s IN% D Seconds ... ", Remote? Remote:" this Computer ", timeout; rc = initiatesystemshutdown (Remote, Message, (DWORD) Timeout, Force, Reboot; PUTCHAR ('/ n'); IF (! rc) {DisplayError (getLastError ()); return 1;} Return 0;} BOOL SetCurrentPrivilege (LPCTSTR Privilege, BOOL bEnablePrivilege) {HANDLE hToken; LUID luid; TOKEN_PRIVILEGES tp, tpPrevious; DWORD cbPrevious = sizeof (TOKEN_PRIVILEGES); BOOL bSuccess = FALSE; IF (! Lookuppprivilerage (Null, Privilege, & Luid) Return False; IF (! openprocess (), token_query | token_adjust_privileges, & htokeen) Return False; Tp.privilegect = 1; tp.privileges [0] .luid = Luid; Tp.privileges [0] .attributes = 0; AdjustTokenprivileges (HToken, False, & TP, Sizeof (Token_Privileges), & TppRevious, & Cbprevious IF (getLastError () == Error_suCcess) {tpprevious.privilegect = 1; tpprevious.privileges [0] .luid = LuID; if (bEnablePrivilege) tpPrevious.Privileges [0] .Attributes | = (SE_PRIVILEGE_ENABLED); else tpPrevious.Privileges [0] .Attributes & = ~ (SE_PRIVILEGE_ENABLED); AdjustTokenPrivileges (hToken, FALSE, & tpPrevious, cbPrevious, NULL, NULL); IF (getLastError () == Error_Success) bsuccess = true; CloseHandle (HTOKEN); Return bsuCcess;} Void DisplayError (DWORD ERR) {char msgbuf [4096]; FormatMessage (format_message_from_system, null, err, makelangid (lang_neutral, sublang_default), msgbuf, sizeof (msgbuf), null; printf ("Error% D:% S / N", err, msgbuf);}