Använda Enhetens ström-API

Använd programmeringselementen Device Power för att hantera hur enheterna fungerar när systemet är i viloläge.

Öppna och stänga enhetslistan

Att öppna och stänga enhetslistan är en kostsam process när det gäller CPU-tid. Funktionerna DevicePowerOpen och DevicePowerClose som gör detta är bara nödvändiga om programmet behöver köra frågor mot enhetslistan flera gånger.

Om DevicePowerOpen anropas måste DevicePowerClose anropas när enhetslistans sökningar är slutförda. DevicePowerEnumDevices och DevicePowerSetDeviceState stänger inte enhetslistan om den uttryckligen har öppnats av DevicePowerOpen.

Räkna upp enheter

Funktionen DevicePowerEnumDevices identifierar om enhetslistan är öppen och om inte öppnar den. DevicePowerEnumDevices räknar upp enheterna baserat på de angivna sökvillkoren. Om enhetslistan öppnades av funktionen stängs den innan funktionen returneras.

Aktivera och inaktivera en enhet från att väcka systemet

Funktionen DevicePowerSetDeviceState, som liknar DevicePowerEnumDevices, identifierar om enhetslistan är öppen och öppnar den om den inte är det. DevicePowerSetDeviceState anger sedan de angivna kriterierna för den angivna enheten. Om enhetslistan öppnades av funktionen stängs den innan funktionen returneras.

Exempelkod

I följande exempel visas hur enhets-POWER API används.

#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;
}

I det här exemplet öppnas enhetslistan en gång och stängs en gång. Om funktionerna DevicePowerOpen och DevicePowerClose inte hade anropats, skulle enhetslistan ha öppnats och stängts av varje anrop till DevicePowerEnumDevices och DevicePowerSetDeviceState. Genom att använda DevicePowerOpen och DevicePowerClose sparade vi att öppna enhetslistan två onödiga gånger.