Share via


ポリシー変更イベントの受信

LSA には、ローカル システムのポリシーに変更がある場合に通知を受信するために使用できる機能が用意されています。

通知を受信するには、 CreateEvent 関数を呼び出して新しいイベント オブジェクトを作成し、 LsaRegisterPolicyChangeNotification 関数を呼び出します。 その後、アプリケーションは WaitForSingleObjectWaitForSingleObjectExRegisterWaitForSingleObject などの待機関数を呼び出して、イベントが発生するのを待機できます。 wait 関数は、イベントが発生したとき、またはタイムアウト期間が経過したときにを返します。 通常、通知イベントはマルチスレッド アプリケーションで使用されます。このアプリケーションでは、1 つのスレッドがイベントを待機し、他のスレッドは処理を続行します。

アプリケーションが通知を受信する必要がなくなったら、 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);
}

イベント オブジェクト、待機関数、および同期の詳細については、「 イベント オブジェクトの使用」を参照してください。