디바이스 전원 프로그래밍 요소를 사용하여 시스템이 절전 상태인 동안 디바이스가 수행하는 방식을 관리합니다.
디바이스 목록 열기 및 닫기
디바이스 목록을 열고 닫는 것은 CPU 시간 측면에서 비용이 많이 드는 프로세스입니다. DevicePowerOpen 및 DevicePowerClose 함수는 애플리케이션이 디바이스 목록을 여러 번 쿼리해야 하는 경우에만 필요합니다.
DevicePowerOpen가 호출되는 경우, 디바이스 목록 쿼리가 완료되면 DevicePowerClose을(를) 호출해야 합니다. devicePowerEnumDevices및 DevicePowerSetDeviceStatedevicePowerOpen 명시적으로 열린 경우 디바이스 목록을닫지 않습니다.
디바이스 열거
DevicePowerEnumDevices 함수는 디바이스 목록이 열려 있는지 여부를 감지하고 그렇지 않은 경우 엽니다. DevicePowerEnumDevices 지정된 검색 조건에 따라 디바이스를 열거합니다. 함수에서 디바이스 목록을 연 경우 함수가 반환되기 전에 닫힙니다.
디바이스가 시스템을 깨우도록 설정 및 설정 해제하기
DevicePowerSetDeviceState 함수는 DevicePowerEnumDevices유사하게 디바이스 목록이 열려 있는지 여부를 감지하고 그렇지 않은 경우 엽니다. DevicePowerSetDeviceState 지정된 디바이스에 대해 지정된 조건을 설정합니다. 함수에서 디바이스 목록을 연 경우 함수가 반환되기 전에 닫힙니다.
예제 코드
다음 예제에서는 Device Power API를 사용하는 방법을 보여 줍니다.
#define _WIN32_WINNT 0x0600
#include <Windows.h>
#include <PowrProf.h>
#include <stdio.h>
#include <tchar.h>
#pragma comment(lib, "PowrProf.lib")
int __cdecl main()
{
// Define and initialize our return variables.
LPWSTR pRetBuf = NULL;
ULONG bufSize = MAX_PATH * sizeof(WCHAR);
ULONG uIndex = 0;
BOOLEAN bRet = FALSE;
// Open the device list, querying all devices
if (!DevicePowerOpen(0))
{
printf("ERROR: The device database failed to initialize.\n");
return FALSE;
}
// Enumerate the device list, searching for devices that support
// waking from either the S1 or S2 sleep state and are currently
// present in the system, and not devices that have drivers
// installed but are not currently attached to the system, such as
// in a laptop docking station.
pRetBuf = (LPWSTR)LocalAlloc(LPTR, bufSize);
while (NULL != pRetBuf &&
0 != (bRet = DevicePowerEnumDevices(uIndex,
DEVICEPOWER_FILTER_DEVICES_PRESENT,
PDCAP_WAKE_FROM_S1_SUPPORTED|PDCAP_WAKE_FROM_S2_SUPPORTED,
(PBYTE)pRetBuf,
&bufSize)))
{
printf("Device name: %S\n", pRetBuf);
// For the devices we found that have support for waking from
// S1 and S2 sleep states, disable them from waking the system.
bRet = (0 != DevicePowerSetDeviceState((LPCWSTR)pRetBuf,
DEVICEPOWER_CLEAR_WAKEENABLED,
NULL));
if (0 != bRet)
{
printf("Warning: Failed to set device state.\n");
}
uIndex++;
}
// Close the device list.
DevicePowerClose();
if (pRetBuf!= NULL) LocalFree(pRetBuf);
pRetBuf = NULL;
return TRUE;
}
이 예제에서는 디바이스 목록이 한 번 열리고 한 번 닫힙니다. DevicePowerOpen 및 DevicePowerClose 함수가 호출되지 않은 경우, DevicePowerEnumDevices 및 DevicePowerSetDeviceState호출할 때마다 디바이스 목록이 각각 열리고 닫혔을 것입니다. DevicePowerOpen 및 DevicePowerClose를 사용하여 디바이스 목록을 불필요하게 두 번 여는 것을 방지했습니다.