注册同步驱动程序通知

若要使用同步驱动程序通知,设备驱动程序将实现操作系统在向硬件分区动态添加新处理器时调用的回调函数。 以下代码示例是此类回调函数的原型:

// Prototype for the synchronous
// notification callback function
VOID
  SyncProcessorCallback(
    IN PVOID CallbackContext,
    IN PKE_PROCESSOR_CHANGE_NOTIFY_CONTEXT ChangeContext,
    IN PNTSTATUS OperationStatus
    );

设备驱动程序通过调用 KeRegisterProcessorChangeCallback 函数注册同步驱动程序通知。 设备驱动程序通常从其 DriverEntry 函数中调用 KeRegisterProcessorChangeCallback 函数。 如果设备驱动程序指定KE_PROCESSOR_CHANGE_ADD_EXISTING标志,则除了在将新处理器添加到硬件分区时调用外,还会立即为硬件分区中当前存在的每个活动处理器调用回调函数。 下面的代码示例演示如何注册同步驱动程序通知:

PVOID CallbackRegistrationHandle;
NTSTATUS CallbackStatus = STATUS_SUCCESS;

// The driver's DriverEntry routine
NTSTATUS  DriverEntry(
    PDRIVER_OBJECT DriverObject,
    PUNICODE_STRING RegistryPath
    )
{
  ...

  // Register the callback function
  CallbackRegistrationHandle =
    KeRegisterProcessorChangeCallback(
      SyncProcessorCallback,
      &CallbackStatus,
      KE_PROCESSOR_CHANGE_ADD_EXISTING
      );

  // Check the result
  if (CallbackRegistrationHandle == NULL)
  {
    // Perform any necessary cleanup
    ...

    // Check the callback status
    if (CallbackStatus != STATUS_SUCCESS)
    {
      // Return the error status from the callback function
      return CallbackStatus;
    }
    else
    {
      // Return a generic error status
      return STATUS_UNSUCCESSFUL;
    }
  }

  ...

  return STATUS_SUCCESS;
}

当设备驱动程序必须停止接收同步驱动程序通知(例如卸载时),它必须通过调用 KeDeregisterProcessorChangeCallback 函数取消注册回调函数。 设备驱动程序通常从其 Unload 函数中调用 KeDeregisterProcessorChangeCallback 函数。 下面的代码示例演示如何注销回调函数:

// The driver's Unload routine
VOID
  Unload(
    IN PDRIVER_OBJECT DriverObject
    );
{
  ...

  // Unregister the callback function
  KeDeregisterProcessorChangeCallback(
    CallbackRegistrationHandle
    );

  ...
}