Usando a API de energia do dispositivo
Use os elementos de programação Device Power para gerenciar o desempenho dos dispositivos enquanto o sistema está em estado de suspensão.
Abrir e fechar a lista de dispositivos
Abrir e fechar a lista de dispositivos é um processo dispendioso em termos de tempo de CPU. O DevicePowerOpen e funções de DevicePowerClose que fazem isso só são necessárias se o aplicativo precisar consultar a lista de dispositivos várias vezes.
Se DevicePowerOpen for chamado, DevicePowerClose deverá ser chamado quando as consultas da lista de dispositivos estiverem concluídas. DevicePowerEnumDevices e DevicePowerSetDeviceState não fecharão a lista de dispositivos se esta tiver sido explicitamente aberta por DevicePowerOpen.
Enumerando dispositivos
A funçãoDevicePowerEnumDevices deteta se a lista de dispositivos está aberta e, se não, a abre. DevicePowerEnumDevices enumera os dispositivos com base nos critérios de pesquisa especificados. Se a lista de dispositivos foi aberta pela função, ela é fechada antes que a função retorne.
Ativar e desativar um dispositivo para não ativar o sistema
A função DevicePowerSetDeviceState, semelhante a DevicePowerEnumDevices, deteta se a lista de dispositivos está aberta e, se não, a abre. DevicePowerSetDeviceState define os critérios especificados para o dispositivo especificado. Se a lista de dispositivos foi aberta pela função, ela é fechada antes que a função retorne.
Código de exemplo
O exemplo a seguir demonstra o uso da API Device Power.
#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;
}
Neste exemplo, a lista de dispositivos é aberta uma vez e fechada uma vez. Se as funções DevicePowerOpen e DevicePowerClose não fossem chamadas, a lista de dispositivos teria sido aberta e fechada por cada chamada para DevicePowerEnumDevices e DevicePowerSetDeviceState. Usando DevicePowerOpen e DevicePowerClose evitamos abrir a lista de dispositivos duas vezes desnecessariamente.