当前位置:   article > 正文

win7休眠、待机api_睡眠原因: application api

睡眠原因: application api

win7休眠、待机api

通过c++让windows进入休眠或者待机状态。
xp、win7下用SetSystemPowerState函数,vista及之后的版本使用 SetSuspendState函数。

xp、win7:

SetSystemPowerState

BOOL WINAPI SetSystemPowerState(
  _In_ BOOL fSuspend,
  _In_ BOOL fForce
);
  • 1
  • 2
  • 3
  • 4
  • 5

Parameters
fSuspend [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.

Return value
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.

休眠与待机的区别
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.

待机是关闭电源(暂且将Suspends翻译为待机)。那么言下之意休眠就不是关闭电源了。那休眠是什么状态呢?硬盘关闭,风扇关闭,但是内存还上着电呢,用户点击鼠标或者键盘之后电脑就会自动恢复了。

而在待机状态下,点击鼠标键盘是不管用的,必须重新点开机按钮才行。开机后进入之前保留的状态。

臆想是上面那样的,而MicroSoft做了更细致的划分。

详细介绍如下

Power stateACPI stateDescription
WorkingS0The system is fully usable. Hardware components that are not in use can save power by entering a lower power state.
Sleep (Modern Standby)S0 low-power idleSome SoC systems support a low-power idle state known as Modern Standby. In this state, the system can very quickly switch from a low-power state to high-power state, so that it can respond quickly to hardware and network events. Systems that support Modern Standby do not use S1-S3.
SleepS1 S2 S3The system appears to be off. Power consumed in these states (S1-S3) is less than S0 and more than S4; S3 consumes less power than S2, and S2 consumes less power than S1. Systems typically support one of these three states, not all three. In these states (S1-S3), volatile memory is kept refreshed to maintain the system state. Some components remain powered so the computer can wake from input from the keyboard, LAN, or a USB device. Hybrid sleep, used on desktops, is where a system uses a hibernation file with S1-S3. The hibernation file saves the system state in case the system loses power while in sleep. Note SoC systems that support modern standby (the low-power idle state) do not use S1-S3.
HibernateS4The system appears to be off. Power consumption is reduced to the lowest level. The system saves the contents of volatile memory to a hibernation file to preserve system state. Some components remain powered so the computer can wake from input from the keyboard, LAN, or a USB device. The working context can be restored if it is stored on nonvolatile media.Fast startup is where the user is logged off before the hibernation file is created. This allows for a smaller hibernation file, more appropriate for systems with less storage capabilities.
Soft OffS5The system appears to be off. This state is comprised of a full shutdown and boot cycle.
Mechanical OffG3The system is completely off and consumes no power. The system returns to the working state only after a full reboot.

Remarks
The 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.
If any application or driver denies permission to suspend operation, the function broadcasts a PBT_APMQUERYSUSPENDFAILED event to each application and driver. If power is suspended, this function returns only after system operation is resumed and related WM_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.

调用此api需要特权,通过AdjustTokenPrivileges来获取特权。

示例

 HANDLE hToken;
 TOKEN_PRIVILEGES tp;
 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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/168001
推荐阅读
相关标签
  

闽ICP备14008679号