//hibernate and standby
int CAutoPowerOnShutdownDlg::SetPower(BOOL bSuspend,BOOL bForce)
{
TOKEN_PRIVILEGES tp;
HANDLE hToken;
LUID luid;
LPTSTR MachineName=NULL;
//為當前進程獲得關閉系統休眠系統的特權
// 取得当前进程的[Token](标识)句柄,(TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY) == 0x0028
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken ))
{
return RTN_ERROR;
}
// 取得关闭系统的[LUID](本地唯一的标识符)值
if(!LookupPrivilegeValue(MachineName, SE_SHUTDOWN_NAME, &luid))
{
return RTN_ERROR;
}
// 设置特权数组的元素个数
tp.PrivilegeCount = 1;
//設置特權數組的luid
tp.Privileges[0].Luid = luid;
// 设置[LUID]的属性值
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// 为当前进程取得关闭系统的特权
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),NULL, NULL );
SetSystemPowerState(bSuspend,bForce);
return 0;
}