Réception des événements de modification de stratégie

Le LSA fournit des fonctions que vous pouvez utiliser pour recevoir une notification en cas de modification de la stratégie sur le système local.

Pour recevoir une notification, créez un nouvel objet d’événement en appelant la fonction CreateEvent , puis appelez la fonction LsaRegisterPolicyChangeNotification . Votre application peut ensuite appeler une fonction Wait telle que WaitForSingleObject, WaitForSingleObjectExou RegisterWaitForSingleObject pour attendre que l’événement se produise. La fonction Wait retourne lorsque l’événement se produit ou lorsque le délai d’attente expire. En règle générale, les événements de notification sont utilisés dans les applications multithread, dans lesquelles un thread attend un événement, tandis que les autres threads continuent le traitement.

Lorsque votre application n’a plus besoin de recevoir des notifications, elle doit appeler LsaUnregisterPolicyChangeNotification , puis appeler CloseHandle pour libérer le handle d’objet d’événement.

L’exemple suivant montre comment une application à thread unique peut recevoir des événements de notification lorsque la stratégie d’audit du système change.

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

Pour plus d’informations sur les objets d’événement, les fonctions Wait et la synchronisation, consultez utilisation des objets d’événement.