注册应用程序通知

用户模式应用程序调用 RegisterDeviceNotification 函数来注册自身,以在将处理器或内存模块动态添加到硬件分区时收到通知。 应用程序调用 RegisterDeviceNotification 函数两次,一次用于注册处理器事件通知,另一次调用注册内存事件通知。 应用程序在注册这些事件的通知时指定以下 GUID 之一:

GUID_DEVICE_PROCESSOR
注册应用程序,以在将处理器动态添加到硬件分区时收到通知。

GUID_DEVICE_MEMORY
注册应用程序,以在内存动态添加到硬件分区时收到通知。

这些 GUID 在头文件 Poclass.h 中定义。

下面的代码示例演示如何注册这两个通知:

HWND hWnd;
DEV_BROADCAST_DEVICEINTERFACE ProcessorFilter;
DEV_BROADCAST_DEVICEINTERFACE MemoryFilter;
HDEVNOTIFY ProcessorNotifyHandle;
HDEVNOTIFY MemoryNotifyHandle;

// The following example assumes that hWnd already
// contains a handle to the application window that
// is to receive the WM_DEVICECHANGE messages.

// Initialize the filter for processor event notification
ZeroMemory(
  &ProcessorFilter,
  sizeof(ProcessorFilter)
  );
ProcessorFilter.dbcc_size =
  sizeof(DEV_BROADCAST_DEVICEINTERFACE);
ProcessorFilter.dbcc_devicetype =
  DBT_DEVTYP_DEVICEINTERFACE;
ProcessorFilter.dbcc_classguid =
  GUID_DEVICE_PROCESSOR;

// Register the application window to receive
// WM_DEVICECHANGE messages for processor events.
ProcessorNotifyHandle =
  RegisterDeviceNotification(
    hWnd,
    &ProcessorFilter,
    DEVICE_NOTIFY_WINDOW_HANDLE
    );

// Initialize the filter for memory event notification
ZeroMemory(
  &MemoryFilter,
  sizeof(MemoryFilter)
  );
MemoryFilter.dbcc_size =
  sizeof(DEV_BROADCAST_DEVICEINTERFACE);
MemoryFilter.dbcc_devicetype =
  DBT_DEVTYP_DEVICEINTERFACE;
MemoryFilter.dbcc_classguid =
  GUID_DEVICE_MEMORY;

// Register the application's window to receive
// WM_DEVICECHANGE messages for memory events.
MemoryNotifyHandle =
  RegisterDeviceNotification(
    hWnd,
    &MemoryFilter,
    DEVICE_NOTIFY_WINDOW_HANDLE
    );

注意 如果应用程序只需要收到有关处理器的通知,则它不必注册内存事件通知。 同样,如果应用程序只需要收到有关内存的通知,则它不必注册处理器事件的通知。

当应用程序不再需要接收处理器或内存事件的通知时,它可以通过调用 UnregisterDeviceNotification 函数取消注册窗口,使其从接收这些事件的WM_DEVICECHANGE消息。 以下代码示例演示如何取消注册应用程序通知:

// Unregister the application window from receiving
// WM_DEVICECHANGE messages for processor events.
UnregisterDeviceNotification(ProcessorNotifyHandle);

// Unregister the application window from receiving
// WM_DEVICECHANGE messages for memory events.
UnregisterDeviceNotification(MemoryNotifyHandle);

有关 RegisterDeviceNotificationUnregisterDeviceNotification 函数的详细信息,请参阅 Microsoft Windows SDK 文档。