Partager via


Accessing low-level Device Information

4/19/2010

The State and Notifications Broker is an architecture and API set that provides access to low-level device information. From battery levels to the currently playing Media Player track, the State and Notifications Broker brings together disparate device status into an easy-to-query format, and also provides callback methods to determine when something has changed.

In this example, a callback function is defined which will be called when the device's battery level reaches a critically low level:

void AdjustPowerConsumption(HREGNOTIFY hNotify, DWORD dwUserData, const PBYTE pData, const UINT cbData);
// Register to be notified of changes to the eighth bit
// in SN_POWERBATTERYSTATE_VALUE. The eighth bit is set to one when 
// the battery is critically low (and set to zero and when it is not).
HRESULT RegistryNotifyCallbackExample()
{
    NOTIFICATIONCONDITION nc;
    HRESULT hr         = S_OK;
    HREGNOTIFY hNotify = NULL;
    // Initialize the notification structure.
    // The mask for the eighth bit.
    nc.dwMask = 0x8;
    // Receive a notification whenever that bit toggles.
    nc.ctComparisonType = REG_CT_ANYCHANGE;
    // dw is ignored for REG_CT_ANYCHANGE.
    nc.TargetValue.dw = 0;
    
    hr = RegistryNotifyCallback(SN_POWERBATTERYSTATE_ROOT, 
                                SN_POWERBATTERYSTATE_PATH, 
                                SN_POWERBATTERYSTATE_VALUE, 
                                AdjustPowerConsumption,
                                0, 
                                &nc, 
                                &hNotify);
    // Close the notification using RegistryCloseNotification when done.
    // Note that it is alright to call RegistryCloseNotification from the callback function.
    // hr = RegistryCloseNotification(hNotify);
    return hr;
}
void AdjustPowerConsumption(HREGNOTIFY hNotify, DWORD dwUserData, const PBYTE pData, const UINT cbData)
{
// The callback function
}

For the complete example, see the topic RegistryNotifyCallback.

Although much of this information is also stored in the Registry, this approach makes code much easier to maintain, and considerably simpler to monitor for changes.

The State and Notification Broken can be used from both managed and native code.

Reference

See Also

Concepts

Programmatically Customizing a Device