| 1795 | |
| 1796 | |
| 1797 | bool Util_Shutdown(int nFlag) |
| 1798 | // Shutdown or logoff the system. |
| 1799 | // Returns false if the function could not get the rights to shutdown. |
| 1800 | { |
| 1801 | /* |
| 1802 | flags can be a combination of: |
| 1803 | #define EWX_LOGOFF 0 |
| 1804 | #define EWX_SHUTDOWN 0x00000001 |
| 1805 | #define EWX_REBOOT 0x00000002 |
| 1806 | #define EWX_FORCE 0x00000004 |
| 1807 | #define EWX_POWEROFF 0x00000008 */ |
| 1808 | |
| 1809 | HANDLE hToken; |
| 1810 | TOKEN_PRIVILEGES tkp; |
| 1811 | |
| 1812 | // Get a token for this process. |
| 1813 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) |
| 1814 | return false; // Don't have the rights |
| 1815 | |
| 1816 | // Get the LUID for the shutdown privilege. |
| 1817 | LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); |
| 1818 | |
| 1819 | tkp.PrivilegeCount = 1; /* one privilege to set */ |
| 1820 | tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 1821 | |
| 1822 | // Get the shutdown privilege for this process. |
| 1823 | AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); |
| 1824 | |
| 1825 | // Cannot test the return value of AdjustTokenPrivileges. |
| 1826 | if (GetLastError() != ERROR_SUCCESS) |
| 1827 | return false; // Don't have the rights |
| 1828 | |
| 1829 | // ExitWindows |
| 1830 | if (ExitWindowsEx(nFlag, 0)) |
| 1831 | return true; |
| 1832 | else |
| 1833 | return false; |
| 1834 | |
| 1835 | } |
| 1836 | |
| 1837 | |
| 1838 | |