Compartir a través de


Recepción de eventos de cambio de directiva

La LSA proporciona funciones que puede usar para recibir notificaciones cuando hay un cambio en la directiva en el sistema local.

Para recibir una notificación, cree un nuevo objeto de evento llamando a la función CreateEvent y, a continuación, llame a la función LsaRegisterPolicyChangeNotification . Después, la aplicación puede llamar a una función de espera como WaitForSingleObject, WaitForSingleObjectEx o RegisterWaitForSingleObject para esperar a que se produzca el evento. La función wait devuelve cuando se produce el evento o cuando expira el período de tiempo de espera. Normalmente, los eventos de notificación se usan en aplicaciones multiproceso, en las que un subproceso espera un evento, mientras que otros subprocesos continúan procesando.

Cuando la aplicación ya no necesite recibir notificaciones, debe llamar a LsaUnregisterPolicyChangeNotification y, a continuación, llamar a CloseHandle para liberar el identificador del objeto de evento.

En el ejemplo siguiente se muestra cómo una aplicación de un solo subproceso puede recibir eventos de notificación cuando cambia la directiva de auditoría del sistema.

#include <windows.h>
#include <stdio.h>

void WaitForPolicyChanges()
{
  HANDLE hEvent;
  NTSTATUS ntsResult;
  DWORD dwResult;

  // Create an event object.
  hEvent = CreateEvent( 
    NULL,  // child processes cannot inherit 
    FALSE, // automatically reset event
    FALSE, // start as a nonsignaled event
    NULL   // do not need a name
  );

  // Check that the event was created.
  if (hEvent == NULL) 
  {
    wprintf(L"Event object creation failed: %d\n",GetLastError());
    return;
  }
  // Register to receive auditing policy change notifications.
  ntsResult = LsaRegisterPolicyChangeNotification(
    PolicyNotifyAuditEventsInformation,
    hEvent
  );
  if (STATUS_SUCCESS != ntsResult)
  {
    wprintf(L"LsaRegisterPolicyChangeNotification failed.\n");
    CloseHandle(hEvent);
    return;
  }

  // Wait for the event to be triggered.
  dwResult = WaitForSingleObject( 
    hEvent, // handle to the event object
    300000  // time-out interval, in milliseconds
  );

  // The wait function returned.
  if (dwResult == WAIT_OBJECT_0)
  {  // received the notification signal
    wprintf(L"Notification received.\n");
  } 
  else 
  {  // received a time-out or error
    wprintf(L"Notification was not received.\n");
  }
  // Unregister for notification.
  LsaUnregisterPolicyChangeNotification(
    PolicyNotifyAuditEventsInformation,
    hEvent
  );

  // Free the event handle.
  CloseHandle(hEvent);
}

Para obtener más información sobre los objetos de evento, las funciones de espera y la sincronización, vea Uso de objetos de evento.