win7休眠、待机api
win7休眠、待機(jī)api
通過c++讓windows進(jìn)入休眠或者待機(jī)狀態(tài)。 
 xp、win7下用SetSystemPowerState函數(shù),vista及之后的版本使用 SetSuspendState函數(shù)。
xp、win7:
SetSystemPowerState
BOOL WINAPI SetSystemPowerState(_In_ BOOL fSuspend,_In_ BOOL fForce );Parameters  
 fSuspend [in] 
 If this parameter is TRUE, the system is suspended. If the parameter is FALSE, the system hibernates. 
 參數(shù)值為真則待機(jī),參數(shù)值為假則休眠。 
 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.
休眠與待機(jī)的區(qū)別 
 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.
待機(jī)是關(guān)閉電源(暫且將Suspends翻譯為待機(jī))。那么言下之意休眠就不是關(guān)閉電源了。那休眠是什么狀態(tài)呢?硬盤關(guān)閉,風(fēng)扇關(guān)閉,但是內(nèi)存還上著電呢,用戶點(diǎn)擊鼠標(biāo)或者鍵盤之后電腦就會自動恢復(fù)了。
而在待機(jī)狀態(tài)下,點(diǎn)擊鼠標(biāo)鍵盤是不管用的,必須重新點(diǎn)開機(jī)按鈕才行。開機(jī)后進(jìn)入之前保留的狀態(tài)。
臆想是上面那樣的,而MicroSoft做了更細(xì)致的劃分。
詳細(xì)介紹如下
| Working | S0 | The 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 idle | Some 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. | 
| Sleep | S1 S2 S3 | The 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. | 
| Hibernate | S4 | The 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 Off | S5 | The system appears to be off. This state is comprised of a full shutdown and boot cycle. | 
| Mechanical Off | G3 | The 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.
調(diào)用此api需要特權(quán),通過AdjustTokenPrivileges來獲取特權(quán)。
示例
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);總結(jié)
以上是生活随笔為你收集整理的win7休眠、待机api的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: SLAM入坑之一:用realsense
- 下一篇: Java中访问修饰符public、pri
