/* shutdown.c - Logs the user off and optionally shutdown the system. Copyright (C) 1999-2002 Luis C. Castro Skertchly This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include OSVERSIONINFO info; HANDLE hAccessToken; TOKEN_PRIVILEGES tkp; UINT uFlags; LPVOID lpMsgBuf; int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { if (!strcmp(lpCmdLine, "/?") || !strcmp(lpCmdLine, "-h") || !strcmp(lpCmdLine, "-H")) { MessageBox(NULL, "logoff [-f] [poweroff | reboot | shutdown]", "Usage", MB_OK); return 0; } info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&info); if (info.dwPlatformId == VER_PLATFORM_WIN32_NT) { if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hAccessToken)) { MessageBox(NULL, "Cannot open Access Token", "Error", MB_ICONERROR | MB_APPLMODAL | MB_OK); return 0; } LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. AdjustTokenPrivileges(hAccessToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) { MessageBox(NULL, "Cannot AdjustTokenPrivileges()", "Error", MB_ICONERROR | MB_APPLMODAL | MB_OK); return 0; } } uFlags = EWX_LOGOFF; if (strstr(lpCmdLine, "-f")) uFlags|= EWX_FORCE; if (strstr(lpCmdLine, "poweroff")) uFlags|= EWX_POWEROFF; if (strstr(lpCmdLine, "reboot")) uFlags|= EWX_REBOOT; if (strstr(lpCmdLine, "shutdown")) uFlags|= EWX_SHUTDOWN; if (!ExitWindowsEx(uFlags, 0)) { FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ARGUMENT_ARRAY, NULL, GetLastError(), 0, (LPSTR) &lpMsgBuf, 0, NULL); MessageBox(NULL, lpMsgBuf, "Error", MB_ICONERROR | MB_APPLMODAL | MB_OK); return GetLastError(); } return 0; }