Compartilhar via


Using the State and Notifications Broker in Native Code

Windows Mobile SupportedWindows Embedded CE Supported

9/8/2008

Estado e o agente de notificação podem ser usados para monitor qualquer chave Registro no sistema. O snapi.h arquivo de cabeçalho contém definições Registro para todas as notificações de de base que são fornecidas pelo sistema. Para cada notificação, snapi.h contém a raiz Registro - HKEY_CURRENT_USER ou HKEY_LOCAL_MACHINE, o caminho Registro sob a raiz e o valor do Registro no caminho que contém o valor da notificação.

Para exemplo de notificação ActiveApplication, localizado em HKEY_CURRENT_USER\System\State\ "Active Application" o Registro tem a seguinte definições:

#define SN_ACTIVEAPPLICATION_ROOT HKEY_CURRENT_USER
#define SN_ACTIVEAPPLICATION_PATH TEXT("System\\State\\Shell")
#define SN_ACTIVEAPPLICATION_VALUE TEXT("Active Application")

Algumas notificações também têm um máscara de bits definido. Para a notificação PhoneNoSim tem o seguinte exemplo definições:

#define SN_PHONENOSIM_ROOT HKEY_LOCAL_MACHINE
#define SN_PHONENOSIM_PATH TEXT("System\\State\\Phone")
#define SN_PHONENOSIM_VALUE TEXT("Status")
#define SN_PHONENOSIM_BITMASK 2

O máscara de bits indica qual bit ou bits contêm o estado valor da notificação. Ao registrar a notificação, é importante definir a máscara de bits na estrutura de NOTIFICATIONCONDITION. Quando receber a notificação, este máscara de bits é usado novamente para aspecto no somente os bits que contém o valor que foi alterado.

Exemplo de código

O seguinte exemplo de código demonstra como usar bitmasks ao registrar uma notificação e ao receber o evento notificação.

Observação

Para fazer o seguinte exemplo de código mais fácil de ler, verificação de segurança e manipulação de erro não estão incluídos.Esta exemplo de código não deve ser usado em uma configuração versão a menos que ele foi modificado para incluí-las.

#include <regext.h>
#include <snapi.h>

// Register the window notification
HRESULT RegisterWindows()
{
  HRESULT hr;
  NOTIFICATIONCONDITION nc;

  // Register the phone call calling notification.
  // By setting dwMask = SN_PHONECALLCALLING_BITMASK, we will get a notification only when there is a 
  // change in the bit indicating whether the phone is currently attempting to connect an outgoing call.

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

  hr = RegistryNotifyCallback(
                SN_PHONECALLCALLING_ROOT,     // registry root to monitor
                SN_PHONECALLCALLING_PATH,     // registry path to monitor
                SN_PHONECALLCALLING_VALUE,    // registry value to monitor
                PhoneCallCallingCallback,     // callback to be called when bit changes
                0,       
                &nc,
                &g_NotifyPhoneCallCalling
                );

  return hr;
}


// Callback function that is called when the bit indicating a call is being made changes
void PhoneCallCallingCallback(HREGNOTIFY hNotify, DWORD dwUserData, const PBYTE pData, const UINT cbData)
{
  DWORD PhoneCallCallingStatus;
  PDWORD pdwData;
  TCHAR PhoneCallCallingStr[STATUS_STRING_LEN];

  StringCchCopy(PhoneCallCallingStr, STATUS_STRING_LEN, L"");

  // get the changed value
  pdwData = (DWORD *)pData;
  PhoneCallCallingStatus = *pdwData;

  // only look at the value of the bit we are interested in
  if (PhoneCallCallingStatus & SN_PHONECALLCALLING_BITMASK)
  {
    StringCchCopy(PhoneCallCallingStr, STATUS_STRING_LEN, L"Attempting to connect an outgoing call");
  }
  else 
  {
    StringCchCopy(PhoneCallCallingStr, STATUS_STRING_LEN, L"Not connecting");
  }

  // TODO: use PhoneCallCallingStr as needed

}

O estado bateria e notificações intensidade bateria usar a palavra baixa da configuração Registro e palavra superior para determinar seus valores. Consulte o aplicativo do estado da bateria no SDK para um exemplo sobre como usar esses bitmasks. A tabela abaixo descreve como interpretar esses valores.

Notificação Descrição Máscara de bits Valor

PowerBatteryBackupState

De backup atual estado bateria (de exemplo, ele é baixo e carregando). Essa enumeração permite que um combinação bit a bit de seus valores membro. O real combinações retornado depende de implementação driver bateria. Consulte o snapi.h cabeçalho para definições do estado níveis.

0x0000FFFF

0 Normal

1 Ausente

2 Charging

Baixa 4

8 Crítica

PowerBatteryBackupStrength

Restante de backup bateria nível.

0xFFFF0000

0 VeryLow

21 Low

41 Médio

61 Alta

81 VeryHigh

PowerBatteryState

Estado bateria atual (de exemplo, ele é baixo e carregando). Essa enumeração permite que um combinação bit a bit de seus valores membro. O real combinações retornado depende de implementação driver bateria. Consulte o snapi.h cabeçalho para definições do estado níveis.

0x0000FFFF

0 Normal

1 Ausente

2 Charging

Baixa 4

8 Crítica

PowerBatteryStrength

Bateria restante nível.

0xFFFF0000

0 VeryLow

21 Low

41 Médio

61 Alta

81 VeryHigh

See Also

Reference

State and Notifications Broker Reference

Other Resources

State and Notifications Broker