#include <windows.h>
#include <winbase.h>
#include <iostream>
// ========================================================================== //
// From https://stackoverflow.com/a/1505971/4083221
// Compile using: g++ -o shutdown shutdown.cpp -lws2_32
// Then run .\shutdown.exe
// ========================================================================== //
// system shutdown
// nSDType: 0 - Shutdown the system
//          1 - Shutdown the system and turn off the power (if supported)
//          2 - Shutdown the system and then restart the system
void SystemShutdown(UINT nSDType)
{
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;

    ::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
    ::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

    tkp.PrivilegeCount = 1; // set 1 privilege
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    // get the shutdown privilege for this process
    ::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);

    switch (nSDType)
    {
    case 0:
        ::ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
        break;
    case 1:
        ::ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0);
        break;
    case 2:
        ::ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0);
        break;
    }
}
// ========================================================================== //
int main()
{
    std::cout << "Executing shutdown command..." << std::endl;

    SystemShutdown(1);

    return 0;
}
// ========================================================================== //