Share via


Notification Through Callback

4/8/2010

An application can use the RegistryNotifyCallback function to register a callback that is called when changes occur in the Bluetooth registry value under the HKEY_LOCAL_MACHINE\System\State\Hardware key. When the state of the Bluetooth radio changes, the callback notifies the caller about the new state. The pCondition parameter points to the NOTIFICATIONCONDITION structure that stores the the registration criteria.

For information about the callback prototype, see REGISTRYNOTIFYCALLBACK.

To stop receiving notifications through the callback, call the RegistryCloseNotification function and pass the handle returned by RegistryNotifyCallback as a parameter.

The following code example shows how to register for notifications using RegistryNotifyCallback.

Note

Error handling has been omitted in this topic for clarity.

//  FUNCTION: HRESULT RegisterBluetoothCallback()
//  PURPOSE: 
//    - Registers a notification request through callback, BluetoothStateCallback. 
//    - The callback is called when Bluetooth radio state changes.

HRESULT RegisterBluetoothCallback()
{
  #define SN_BLUETOOTHPOWERBSTATE_ROOT    HKEY_LOCAL_MACHINE
  #define SN_BLUETOOTHPOWERBSTATE_PATH    TEXT("System\\State\\Hardware")
  #define SN_BLUETOOTHPOWERBSTATE_VALUE   TEXT("Bluetooth")
  #define SN_BLUETOOTHPOWERBSTATE_BITMASK 3

  HRESULT  hr        = NULL;
  NOTIFICATIONCONDITION  nc;
  HREGNOTIFY  g_hNotify_Callback = NULL;   // Handles to notifications through callback

  nc.ctComparisonType = REG_CT_ANYCHANGE;
  nc.dwMask           = SN_BLUETOOTHPOWERBSTATE_BITMASK;
  nc.TargetValue.dw   = 0;

   // Register Bluetooth radio state notification.
  hr = RegistryNotifyCallback(
                SN_BLUETOOTHPOWERBSTATE_ROOT,
                SN_BLUETOOTHPOWERBSTATE_PATH,
                SN_BLUETOOTHPOWERBSTATE_VALUE,
                BluetoothStateCallback,          // This notification uses a callback.
                0,       
                &nc,
                &g_hNotify_Callback
                );

  if (FAILED(hr))
  {
    perform error handling;
    return hr;
  }
  return S_OK;
}

See Also

Concepts

Detecting Power State Changes By Using State and Notifications Broker
Notification Through Windows Messages