Search code examples
windowswinapishutdown

Windows Shutdown - Shutdown command or ExitWindowsEx


I want to know which is the best practice when a piece of software needs to restart, shutdown, logoff or hibernate Windows:

  1. Use ExitWindowsEx API
  2. Use Shutdown command and its parameters

What is the advantages and disadvantages of each approach?

Thanks


Solution

  • I always prefer calling the API (ExitWindowsEx, InitiateSystemShutdown).

    Pro API

    • GetLastError can be used to diagnose errors
    • You get access to the full API, not just the things exposed by a utility
    • You don't have to worry about filesystem redirection and whether the tool exists in the home versions of Windows
    • The documented interface stays the same, command line switches could change or be different in a different language

    Pro external tool

    • Makes some difficult tasks easy to perform (Some things require undocumented API's)
    • Few bugs, and if there are bugs they are not yours ;)