休眠调用函数
SetSuspendState function
Suspends the system by shutting power down. Depending on the Hibernate parameter, the system either enters a suspend (sleep) state or hibernation (S4).
SyntaxIf this parameter is TRUE, the system hibernates. If the parameter is FALSE, the system is suspended.
ForceCritical [in]This parameter has no effect.
If this parameter is TRUE, the system disables all wake events. If the parameter is FALSE, any system wake events remain enabled.
Return valueIf the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
RemarksThe calling process must have the SE_SHUTDOWN_NAME privilege. To enable the SE_SHUTDOWN_NAME privilege, use the AdjustTokenPrivileges function. For more information, see Changing Privileges in a Token.
An application may use SetSuspendState to transition the system from the working state to the standby (sleep), or optionally, hibernate (S4) state. This function is similar to the SetSystemPowerState function.
For more information on using PowrProf.h, see Power Schemes. For information about events that can wake the system, see System Wake-up Events.
RequirementsMinimum supported client
Windows XP [desktop apps only]Minimum supported server
Windows Server 2003 [desktop apps only]Header
PowrProf.hLibrary
PowrProf.libDLL
PowrProf.dll
SetSystemPowerState function
[SetSystemPowerState is available for use in the operating systems specified in the Requirements section. It may be altered or unavailable in subsequent versions. Applications written for Windows Vista and later should useSetSuspendState instead.]
Suspends the system by shutting power down. Depending on the ForceFlag parameter, the function either suspends operation immediately or requests permission from all applications and device drivers before doing so.
Syntax C++
ParametersfSuspend [in]
If this parameter is TRUE, the system is suspended. If the parameter is FALSE, the system hibernates.
fForce [in]This parameter has no effect.
If power has been suspended and subsequently restored, the return value is nonzero.
If the system was not suspended, the return value is zero. To get extended error information, call GetLastError.
RemarksThe calling process must have the SE_SHUTDOWN_NAMEprivilege. To enable the SE_SHUTDOWN_NAME privilege, use the AdjustTokenPrivileges function. For more information, see Changing Privileges in a Token.
If any application or driver denies permission to suspend operation, the function broadcasts aPBT_APMQUERYSUSPENDFAILED event to each application and driver. If power is suspended, this function returns only after system operation is resumed and relatedWM_POWERBROADCAST messages have been broadcast to all applications and drivers.
This function is similar to the SetSuspendState function.
To compile an application that uses this function, define the _WIN32_WINNT macro as 0x0400 or later. For more information, see Using the Windows Headers.
RequirementsMinimum supported client
Windows XP [desktop apps only]Minimum supported server
Windows Server 2003 [desktop apps only]Header
WinBase.h (include Windows.h)Library
Kernel32.libDLL
Kernel32.dll实现休眠功能很简单,只要在任意想要执行的地方执行如下给出的休眠函数SetPower()就行,其实休眠很简单,只需要API:SetSystemPowerState()就行,就如关机/注销/重启一样,只需要ExitWindowEx(),但这些API可以直接用在98下,却不能用在2000/XP中,因为这里牵涉到了用户权限,没有权限是不能进行这些操作的,所以,首先要取得权限,下面给出代码:
#define RTN_ERROR 13
void PERR(LPTSTR szAPI, DWORD dwLastError) //休眠时调用到的一个函数,用来
记录休眠中遇到的错误
{
LPTSTR MessageBuffer;
DWORD dwBufferLength;
fprintf(stderr,"%s error! (rc=%lu)\n", szAPI, dwLastError);
if(dwBufferLength=FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwLastError,
LANG_NEUTRAL,
(LPTSTR) &MessageBuffer,
0,
NULL))
{
DWORD dwBytesWritten;
WriteFile(GetStdHandle(STD_ERROR_HANDLE),
MessageBuffer,
dwBufferLength,
&dwBytesWritten,
NULL);
LocalFree(MessageBuffer);
}
}
INT SetPower()有//主要功能函数
{
TOKEN_PRIVILEGES tp;
HANDLE hToken;
LUID luid;
LPTSTR MachineName=NULL;
if(!OpenProcessToken(GetCurrentProcess(), 从这里
TOKEN_ADJUST_PRIVILEGES,
&hToken ))
{
PERR("OpenProcessToken", GetLastError() );
return RTN_ERROR;
}
if(!LookupPrivilegeValue(MachineName, SE_SHUTDOWN_NAME, &luid))
{
PERR("LookupPrivilegeValue", GetLastError() );
return RTN_ERROR;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
NULL, NULL ); 到这里,是取得权限//
SetSystemPowerState(FALSE,TRUE);
return 0;
}
这样,在任意需要休眠的时候调用SetPower();即可休眠,但是有一点要注意:必须打开了高级电源管理的休眠支持。
下面这段代码简洁,没有错误记录功能。
void CPage1::OnXiuMian()
{
if(MessageBox(“确实要休眠吗?”,”关机程序”,MB_YESNO|MB_DEFBUTTON2|MB_ICONQUESTION)==IDYES)
{
static HANDLE hToken;
static TOKEN_PRIVILEGES tp;
static LUID luid;
if(::OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
&hToken))
{
::LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
tp.PrivilegeCount=1;
tp.Privileges[0].Luid =luid;
tp.Privileges[0].Attributes =SE_PRIVILEGE_ENABLED;
::AdjustTokenPrivileges(hToken,false,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL);
}
::SetSystemPowerState(false,true);
}
}