Thursday, January 29, 2009

[VC++] Programmatically shutdown, restart or logoff

If you are in need of shutting down, restart or log off you system using your VC++ code, then here are two APIs which will do exactly what you think !
ExitWindowsEx()
InitiateSystemShutdownEx()
Following section demonstrate ExitWindowsEx() function for shutdown, restart & log off using ExitWindowsEx,

Shutdown
// Shutdown forcefully
if( ExitWindowsEx( EWX_SHUTDOWN EWX_FORCE, SHTDN_REASON_MAJOR_OPERATINGSYSTEM ))
{
// Successfully requested for shutdown
}

Restart
// Restart forcefully
if( ExitWindowsEx( EWX_REBOOT EWX_FORCE, SHTDN_REASON_MAJOR_OPERATINGSYSTEM ))
{
// Successfully requested for reboot
}

Log Off
// Logging off forcefully
if( ExitWindowsEx( EWX_LOGOFF EWX_FORCE, SHTDN_REASON_MINOR_HUNG ))
{
// Successfully requested for loging off
}

EWX_FORCE flag is responsible for forceful operation request. Second parameter of ExitWindowsEx() specify the reason of exiting windows, check here for more flags/options. If you need a much user friendly Shutdown/Restart/Log off operation with a timer and a dialog box that prompts the user to log off you can go for InitiateSystemShutdownEx().

No comments: