註冊代理程式更新
使用者模式應用程式會呼叫 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);
如需RegisterDeviceNotification和UnregisterDeviceNotification函式的詳細資訊,請參閱Microsoft Windows SDK檔。