Condividi tramite


funzione CM_Unregister_Notification (cfgmgr32.h)

Usa UnregisterDeviceNotification anziché CM_Unregister_Notification se il codice è destinato a Windows 7 o versioni precedenti di Windows.

La funzione CM_Unregister_Notification chiude l'handle HCMNOTIFICATION specificato.

Sintassi

CMAPI CONFIGRET CM_Unregister_Notification(
  [in] HCMNOTIFICATION NotifyContext
);

Parametri

[in] NotifyContext

Handle HCMNOTIFICATION restituito dalla funzione CM_Register_Notification .

Valore restituito

Se l'operazione ha esito positivo, la funzione restituisce CR_SUCCESS. In caso contrario, restituisce uno dei codici di errore con prefisso CR_ definiti in Cfgmgr32.h.

Commenti

Non chiamare CM_Unregister_Notification da un callback di notifica. In questo modo può verificarsi un deadlock perché CM_Unregister_Notification attende il completamento dei callback in sospeso.

Se invece si vuole annullare la registrazione dal callback delle notifiche, è necessario eseguire questa operazione in modo asincrono. La sequenza seguente illustra un modo per eseguire questa operazione:

  1. Allocare una struttura di contesto da usare con le notifiche. Includere un puntatore a una struttura di lavoro del pool di thread (PTP_WORK) e qualsiasi altra informazione da passare al callback delle notifiche.
  2. Chiamare CreateThreadpoolWork. Specificare una funzione di callback che chiama CM_Unregister_Notification. Aggiungere la struttura di lavoro restituita alla struttura del contesto allocata in precedenza.
  3. Chiamare CM_Register_Notification e specificare la struttura di contesto come parametro pContext .
  4. Eseguire operazioni, ricevere notifiche e così via.
  5. Chiamare SubmitThreadpoolWork dall'interno del callback di notifica, fornendo il puntatore a una struttura di lavoro del pool di thread (PTP_WORK) archiviata nella struttura del contesto.
  6. Quando viene eseguito il thread di threadpool, l'elemento di lavoro chiama CM_Unregister_Notification.
  7. Chiamare CloseThreadpoolWork per rilasciare l'oggetto di lavoro.
Se la struttura del contesto è stata completata, non dimenticare di rilasciare le risorse e liberare la struttura.
Attenzione Non liberare la struttura del contesto fino a quando l'elemento di lavoro non ha chiamato CM_Unregister_Notification. È comunque possibile ricevere notifiche dopo l'invio dell'elemento di lavoro del pool di thread e prima che l'elemento di lavoro chiami CM_Unregister_Notification.
 

Esempio

Nell'esempio seguente viene illustrato come annullare la registrazione dal callback delle notifiche, come descritto nella sezione Osservazioni.

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

Requisiti

Requisito Valore
Client minimo supportato Disponibile in Microsoft Windows 8 e versioni successive di Windows.
Piattaforma di destinazione Universale
Intestazione cfgmgr32.h (include Cfgmgr32.h)
Libreria Cfgmgr32.lib; OneCoreUAP.lib in Windows 10
DLL CfgMgr32.dll

Vedi anche

UnregisterDeviceNotification