若要使用異步驅動程式通知,裝置驅動程式會實作作業系統在將處理器或記憶體模組動態新增至硬體分割區時所呼叫的回呼函式。 下列程式代碼範例顯示這類回呼函式的原型:
// Prototypes for the asynchronous
// notification callback functions
NTSTATUS
AsyncProcessorCallback(
IN PVOID NotificationStructure,
IN PVOID Context
);
NTSTATUS
AsyncMemoryCallback(
IN PVOID NotificationStructure,
IN PVOID Context
);
裝置驅動程式會藉由呼叫 IoRegisterPlugPlayNotification 函式來註冊異步通知,並針對每一個裝置驅動程式的回呼函式呼叫此函式一次,指定 EventCategoryData 參數為下列其中一個 GUID 的指標:
GUID_DEVICE_PROCESSOR
當處理器動態新增至硬體分割區時,請註冊以通知。
GUID_DEVICE_MEMORY
當記憶體動態新增至硬體分割區時,請註冊以通知。
這些 GUID 定義於頭檔 Poclass.h 中。
下列程式代碼範例示範如何註冊這兩個通知:
PVOID ProcessorNotificationEntry;
PVOID MemoryNotificationEntry;
NTSTATUS Status;
Status =
IoRegisterPlugPlayNotification(
EventCategoryDeviceInterfaceChange,
0,
&GUID_DEVICE_PROCESSOR,
DriverObject,
AsyncProcessorCallback,
NULL,
&ProcessorNotificationEntry
);
Status =
IoRegisterPlugPlayNotification(
EventCategoryDeviceInterfaceChange,
0,
&GUID_DEVICE_MEMORY,
DriverObject,
AsyncMemoryCallback,
NULL,
&MemoryNotificationEntry
);
注意 如果設備驅動器只需要收到有關處理器的通知,則不需要實作記憶體的回呼函式,或註冊有關記憶體的通知。 同樣地,如果設備驅動器只需要收到有關記憶體的通知,它就不需要實作處理器的回呼函式,也不需要註冊處理器的相關通知。
當裝置驅動程式必須停止接收非同步驅動程式通知時,例如在卸載時,它必須呼叫 IoUnregisterPlugPlayNotification 函式來取消註冊每個回呼函式。 下列程式代碼範例示範如何取消註冊回呼函式:
// Unregister for asynchronous notifications
Status =
IoUnregisterPlugPlayNotification(
ProcessorNotificationEntry
);
Status =
IoUnregisterPlugPlayNotification(
MemoryNotificationEntry
);