Главная страница | назад





Article #17621: Shut down windows programmatically

 Question and Answer Database

FAQ2621C.txt   Shut down windows programmatically
Category   :Windows API
Platform    :All
Product    :C++Builder  3.x

Question:
How do I programmatically shutdown windows?


Answer:
As such:

bool WINAPI SystemRestart (DWORD mode)
{   
    BOOL ret;
    OSVERSIONINFO vi;

    vi.dwOSVersionInfoSize = sizeof(vi);
    GetVersionEx (&vi);
    if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
      HANDLE hToken;
      TOKEN_PRIVILEGES tp;

      ret = FALSE;
      if (OpenProcessToken (GetCurrentProcess (),             // open access privilege list.
          TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
        tp.PrivilegeCount = 1;
        LookupPrivilegeValue (NULL, SE_SHUTDOWN_NAME,       // Ask the "shutdown" LUID
                              &tp.Privileges[0].Luid);
        tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // enable it
        AdjustTokenPrivileges (hToken, FALSE, &tp, 0, NULL, 0);
        ret = (GetLastError () == ERROR_SUCCESS);
      }
    }
    else
      ret = true;

    return (ret ? ExitWindowsEx (mode, 0) : false);
}

7/2/98 10:32:32 AM
 

Last Modified: 01-SEP-99