função CM_Unregister_Notification (cfgmgr32.h)
Use UnregisterDeviceNotification em vez de CM_Unregister_Notification se o código for direcionado ao Windows 7 ou versões anteriores do Windows.
A função CM_Unregister_Notification fecha o identificador HCMNOTIFICATION especificado.
Sintaxe
CMAPI CONFIGRET CM_Unregister_Notification(
[in] HCMNOTIFICATION NotifyContext
);
Parâmetros
[in] NotifyContext
O identificador HCMNOTIFICATION retornado pela função CM_Register_Notification .
Retornar valor
Se a operação for bem-sucedida, a função retornará CR_SUCCESS. Caso contrário, ele retornará um dos códigos de erro prefixados por CR_ definidos em Cfgmgr32.h.
Comentários
Não chame CM_Unregister_Notification de um retorno de chamada de notificação. Isso pode causar um deadlock porque CM_Unregister_Notification aguarda a conclusão dos retornos de chamada pendentes.
Em vez disso, se você quiser cancelar o registro do retorno de chamada de notificação, deverá fazê-lo de forma assíncrona. A sequência a seguir mostra uma maneira de fazer isso:
- Aloque uma estrutura de contexto a ser usada com suas notificações. Inclua um ponteiro para uma estrutura de trabalho de threadpool (PTP_WORK) e qualquer outra informação que você gostaria de passar para o retorno de chamada de notificação.
- Chame CreateThreadpoolWork. Forneça uma função de retorno de chamada que chama CM_Unregister_Notification. Adicione a estrutura de trabalho retornada à estrutura de contexto alocada anteriormente.
- Chame CM_Register_Notification e forneça a estrutura de contexto como o parâmetro pContext .
- Trabalhe, obtenha notificações etc.
- Chame SubmitThreadpoolWork de dentro do retorno de chamada de notificação, fornecendo o ponteiro para uma estrutura de trabalho de threadpool (PTP_WORK) armazenada em sua estrutura de contexto.
- Quando o thread thread do threadpool é executado, o item de trabalho chama CM_Unregister_Notification.
- Chame CloseThreadpoolWork para liberar o objeto de trabalho.
Exemplos
O exemplo a seguir mostra como cancelar o registro do retorno de chamada de notificação, conforme descrito na seção Comentários.
typedef struct _CALLBACK_CONTEXT {
BOOL bUnregister;
PTP_WORK pWork;
HCMNOTIFICATION hNotify;
CRITICAL_SECTION lock;
} CALLBACK_CONTEXT, *PCALLBACK_CONTEXT;
DWORD
WINAPI
EventCallback(
__in HCMNOTIFICATION hNotification,
__in PVOID Context,
__in CM_NOTIFY_ACTION Action,
__in PCM_NOTIFY_EVENT_DATA EventData,
__in DWORD EventDataSize
)
{
PCALLBACK_CONTEXT pCallbackContext = (PCALLBACK_CONTEXT)Context;
// unregister from the callback
EnterCriticalSection(&(pCallbackContext->lock));
// in case this callback fires before the registration call returns, make sure the notification handle is properly set
Context->hNotify = hNotification;
if (!pCallbackContext->bUnregister) {
pCallbackContext->bUnregister = TRUE;
SubmitThreadpoolWork(pCallbackContext->pWork);
}
LeaveCriticalSection(&(pCallbackContext->lock));
return ERROR_SUCCESS;
};
VOID
CALLBACK
WorkCallback(
_Inout_ PTP_CALLBACK_INSTANCE Instance,
_Inout_opt_ PVOID Context,
_Inout_ PTP_WORK pWork
)
{
PCALLBACK_CONTEXT pCallbackContext = (PCALLBACK_CONTEXT)Context;
CM_Unregister_Notification(pCallbackContext->hNotify);
}
VOID NotificationFunction()
{
CONFIGRET cr = CR_SUCCESS;
HRESULT hr = S_OK;
CM_NOTIFY_FILTER NotifyFilter = { 0 };
BOOL bShouldUnregister = FALSE;
PCALLBACK_CONTEXT context;
context = (PCALLBACK_CONTEXT)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(CALLBACK_CONTEXT));
if (context == NULL) {
goto end;
}
InitializeCriticalSection(&(context->lock));
NotifyFilter.cbSize = sizeof(NotifyFilter);
NotifyFilter.Flags = 0;
NotifyFilter.FilterType = CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE;
NotifyFilter.Reserved = 0;
hr = StringCchCopy(NotifyFilter.u.DeviceInstance.InstanceId,
MAX_DEVICE_ID_LEN,
TEST_DEVICE_INSTANCE_ID);
if (FAILED(hr)) {
goto end;
}
context->pWork = CreateThreadpoolWork(WorkCallback, context, NULL);
if (context->pWork == NULL) {
goto end;
}
cr = CM_Register_Notification(&NotifyFilter,
context,
EventCallback,
&context->hNotify);
if (cr != CR_SUCCESS) {
goto end;
}
// ... do work here ...
EnterCriticalSection(&(context->lock));
if (!context->bUnregister) {
// unregister not from the callback
bShouldUnregister = TRUE;
context->bUnregister = TRUE;
}
LeaveCriticalSection(&(context->lock));
if (bShouldUnregister) {
cr = CM_Unregister_Notification(context->hNotify);
if (cr != CR_SUCCESS) {
goto end;
}
} else {
// if the callback is the one performing the unregister, wait for the threadpool work item to complete the unregister
WaitForThreadpoolWorkCallbacks(context->pWork, FALSE);
}
end:
if (context != NULL) {
if (context->pWork != NULL) {
CloseThreadpoolWork(context->pWork);
}
DeleteCriticalSection(&(context->lock));
HeapFree(GetProcessHeap(), 0, context);
}
return;
}
Requisitos
Requisito | Valor |
---|---|
Cliente mínimo com suporte | Disponível no Microsoft Windows 8 e versões posteriores do Windows. |
Plataforma de Destino | Universal |
Cabeçalho | cfgmgr32.h (inclua Cfgmgr32.h) |
Biblioteca | Cfgmgr32.lib; OneCoreUAP.lib no Windows 10 |
DLL | CfgMgr32.dll |