未装驱动,收不到WM_DEVICECHANGE怎么办?

比如,魅族M9,三星I579和三星I9001等型号的手机,用如下的代码注册的,在它们未安装驱动的情况下,有可能收不到WM_DEVICECHANGE消息。

	//Register message notification
	DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
	ZeroMemory(¬ificationFilter, 0);
	notificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
	notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
	notificationFilter.dbcc_classguid  = GUID_CLASS_USB_DEVICE;
	hDevNotify = RegisterDeviceNotification(hCallerWnd, 
		¬ificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
	if(NULL == hDevNotify)
	{
		return GetLastError();
	}
	notificationFilter.dbcc_classguid  = GUID_CLASS_USBHUB;
	hDevNotify = RegisterDeviceNotification(hCallerWnd, 
		¬ificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
	if(NULL == hDevNotify)
	{
		return GetLastError();
	}

只需用DEVICE_NOTIFY_ALL_INTERFACE_CLASSES替代DEVICE_NOTIFY_WINDOW_HANDLE即可,代码片段如下:

int WINAPI
WinMain (
    __in HINSTANCE hInstance,
    __in_opt HINSTANCE hPrevInstance,
    __in LPSTR lpCmdLine,
    __in int nCmdShow
)
{
	HWND ghMainWnd = CreateDialog(ghInstance,
	     MAKEINTRESOURCE(IDD_MAINDIALOG),
	     NULL,
	     MainDlgProc);
}     
     
INT_PTR CALLBACK
MainDlgProc (
    HWND   hWnd,
    UINT   uMsg,
    WPARAM wParam,
    LPARAM lParam
)
{
    switch (uMsg)
    {
        HANDLE_MSG(hWnd, WM_INITDIALOG,     USBView_OnInitDialog);
        HANDLE_MSG(hWnd, WM_DEVICECHANGE,   USBView_OnDeviceChange);
    }
    return 0;
}

BOOL
USBView_OnInitDialog (
    HWND    hWnd,
    HWND    hWndFocus,
    LPARAM  lParam
)
{
	DEV_BROADCAST_DEVICEINTERFACE   broadcastInterface = {0};

	// Register to receive notification when a USB device is plugged in.
	broadcastInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
	broadcastInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
	gNotifyDevHandle = RegisterDeviceNotification(hWnd,
			&broadcastInterface,
			DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
			//DEVICE_NOTIFY_WINDOW_HANDLE);
			///.....
}

BOOL
USBView_OnDeviceChange (
    HWND  hwnd,
    UINT  uEvent,
    DWORD dwEventData
)
{
	TCHAR temp[200] = {0};
	_stprintf_s(temp, sizeof(temp)/sizeof(TCHAR),
		TEXT("uEvent=%#x, dwEventData=%#x"), uEvent, dwEventData);
	OutputDebugString(temp);
}

OUTPUT框里,输出结果如下:uEvent=0x7, dwEventData=0x7

//C:\WinDDK\7600.16385.1\inc\api\dbt.h
/*
 * Message = WM_DEVICECHANGE
 * wParam  = DBT_DEVNODES_CHANGED
 * lParam  = 0
 *
 *      send when configmg finished a process tree batch. Some devnodes
 *      may have been added or removed. This is used by ring3 people which
 *      need to be refreshed whenever any devnode changed occur (like
 *      device manager). People specific to certain devices should use
 *      DBT_DEVICE* instead.
 */

#define DBT_DEVNODES_CHANGED            0x0007



 

 

你可能感兴趣的:(未装驱动,收不到WM_DEVICECHANGE怎么办?)