//
//  Copyright (c) 2001, Reson, Inc. All Rights Reserved.
//
//  Filename:   MiscUtils.cpp
//
//  Project:    6046.
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:      

#include "StdAfx.h"
#include "MiscUtils.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

EXPORT_DLL void ShutdownWindows( BOOL bReboot )
{        
    ManageDllState_m();

    // Get access to shutdown privilege

    HANDLE           hToken;
    TOKEN_PRIVILEGES sPrivilegeToken;

    // Get the current process token handle so we can get shutdown privilege.  

    if ( OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
    {
        // Get the LUID for shutdown privilege.

        if ( LookupPrivilegeValue( NULL, SE_SHUTDOWN_NAME, &sPrivilegeToken.Privileges[ 0 ].Luid) == 0 )
        {
            TRACE( "ShutdownWindows(), Can't lookup privilege value\n" );
        }
        else 
        {  
            // One privilege to set.

            sPrivilegeToken.PrivilegeCount = 1;
            sPrivilegeToken.Privileges[ 0 ].Attributes = SE_PRIVILEGE_ENABLED;

            // Get shutdown privilege for this process.  

            AdjustTokenPrivileges( hToken, FALSE, &sPrivilegeToken, 0, (PTOKEN_PRIVILEGES) NULL, 0 );

            // Display the shutdown dialog box and start the time-out countdown.  

            InitiateSystemShutdown( NULL,                                               // Shut down local computer
                                        _T( "Press Escape to terminate shutdown." ),    // Message to user
                                            3,                                          // Time-out period (seconds)
                                                TRUE,                                   // Don't ask user to close apps
                                                    bReboot );                          // Reboot after shutdown
        }
    }
}
