Home Articles Books Downloads FAQs Tips

Q: Shutdown Windows


Answer

Call the ExitWindowsEx API function.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    ExitWindowsEx(EWX_SHUTDOWN, 0);
}

Note: The second argument to ExitWindowsEx is ignored by the OS. The first argument controls how the function behaves. The possible values are listed below. You can combine commands using a bitwise OR.

EWX_FORCE       Forces programs to close without sending WM_QUERYENDSESSION
                and WM_ENDSESSION messages to notify them that Windows is
                shutting down. With any of the other flags, programs have the
                power to abort the shut down.
EWX_LOGOFF      Closes all programs and then logs the user off.
EWX_POWEROFF    Powers down the computer if it supports it.
EWX_REBOOT      Reboots the machine.
EWX_SHUTDOWN    Shuts the machine off without restarting it.

Note: The ExitWindowsEx function starts the shut down process and then returns to the calling function. The shut down completes sometime after the function returns. If you don't specify the EWX_FORCE flag, Windows will send messages to each running program to ask permision to complete the shutdown.

Note: ExitWindowsEX returns TRUE on success and FALSE if an error occurred.

Note: EWX_LOGOFF has some problems on Windows 95. For more details, consult Microsoft knowledge base article Q149690.

Note: On Windows NT, you must ensure that your program has the proper security rights to shutdown the system before you call ExitWindowsEx. Your program must have the SE_SHUTDOWN_NAME privilege.



Copyright © 1997-2000 by Harold Howe.
All rights reserved.