다음을 통해 공유


정책 변경 이벤트 수신

LSA는 로컬 시스템에 정책이 변경될 때 알림을 받는 데 사용할 수 있는 함수를 제공합니다.

알림을 받으려면 CreateEvent 함수를 호출하여 새 이벤트 개체를 만든 다음 LsaRegisterPolicyChangeNotification 함수를 호출합니다. 그런 다음, 애플리케이션은 WaitForSingleObject, WaitForSingleObjectEx 또는 RegisterWaitForSingleObject 와 같은 대기 함수를 호출하여 이벤트가 발생할 때까지 기다릴 수 있습니다. 대기 함수는 이벤트가 발생하거나 제한 시간이 만료되는 시기를 반환합니다. 일반적으로 알림 이벤트는 다중 스레드 애플리케이션에서 사용되며, 한 스레드는 이벤트를 대기하고 다른 스레드는 처리를 계속합니다.

애플리케이션이 더 이상 알림을 받을 필요가 없는 경우 LsaUnregisterPolicyChangeNotification 을 호출한 다음 CloseHandle 을 호출하여 이벤트 개체 핸들을 해제해야 합니다.

다음 예제에서는 시스템의 감사 정책이 변경될 때 단일 스레드 애플리케이션이 알림 이벤트를 수신하는 방법을 보여 줍니다.

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

이벤트 개체, 대기 함수 및 동기화에 대한 자세한 내용은 이벤트 개체 사용을 참조하세요.