通过注册监听设备通知:
BOOL CDeviceNotifyMgr::RegisterAuidoNotify(HWND hWnd)
{
if (!gpRegisterDeviceNotification || !gpUnregisterDeviceNotification) {
if (!getFuntions()) return FALSE;
}
DEV_BROADCAST_DEVICEINTERFACE filterData;
ZeroMemory(&filterData, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
filterData.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
filterData.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
filterData.dbcc_classguid = AM_KSCATEGORY_AUDIO;
m_hAudioNotify = gpRegisterDeviceNotification(hWnd, &filterData, DEVICE_NOTIFY_WINDOW_HANDLE);
if (m_hAudioNotify == NULL) {
return FALSE;
}
return TRUE;
}
视频设备:\
BOOL CDeviceNotifyMgr::RegisterVideoNotify(HWND hWnd)
{
if (!gpRegisterDeviceNotification || !gpUnregisterDeviceNotification) {
if (!getFuntions()) return FALSE;
}
DEV_BROADCAST_DEVICEINTERFACE filterData;
ZeroMemory(&filterData, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
filterData.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
filterData.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
filterData.dbcc_classguid = AM_KSCATEGORY_VIDEO;
m_hVideoNotify = gpRegisterDeviceNotification(hWnd, &filterData, DEVICE_NOTIFY_WINDOW_HANDLE);
if (m_hVideoNotify == NULL) {
return FALSE;
}
return TRUE;
}
消息处理放到WindowProc中处理, 系统发送WM_DEVICECHANGE, 不会走到PretranslateMessage.
demo如下: WIN10下运行正常.
https://code.csdn.net/w839687571/testwindowdevicenotify/tree/master