typedef enum
{
POWERMODE_HIBERNATE,
POWERMODE_SUSPEND,
POWERMODE_REBOOT,
POWERMODE_POWER_OFF
}FESDK_POWERMODE_ENUM;
.........................
CPowerManager::CPowerManager()
: pSetSuspendState(0)
{
m_handle = ::LoadLibrary(_T("Powrprof.dll"));
if (m_handle)
pSetSuspendState = (tSetSuspendState) ::GetProcAddress(m_handle,"SetSuspendState");
}
CPowerManager::~CPowerManager()
{
::FreeLibrary(m_handle);
pSetSuspendState = 0;
}
bool CPowerManager::SetPowerMode( const FESDK_POWERMODE_ENUM & powerMode )
{
HANDLE hToken = NULL;
TOKEN_PRIVILEGES tkp = {0};
if (!::OpenProcessToken(::GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false;
::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
::CloseHandle(hToken);
if (::GetLastError() != ERROR_SUCCESS)
return false;
switch(powerMode)
{
case POWERMODE_HIBERNATE:
{
if(pSetSuspendState)
{
return pSetSuspendState(TRUE,FALSE,FALSE) != 0;
}
}
break;
case POWERMODE_SUSPEND:
{
if(pSetSuspendState)
{
return pSetSuspendState(FALSE,FALSE,FALSE) != 0;
}
}
break;
case POWERMODE_REBOOT:
{
return ::ExitWindowsEx(EWX_REBOOT, SHTDN_REASON_MAJOR_APPLICATION) != 0;
}
break;
default:
{
return ::ExitWindowsEx(EWX_POWEROFF, SHTDN_REASON_MAJOR_APPLICATION) != 0;
}
break;
}
return false;
}
Il vero modo per spegnere il PC da C/C++.
Tutte le altre versioni sono scorciatoie