[Qt-interest] USB Device Notifications (PDEV_BRAODCAST_PORT)

Wm. G. Urquhart wgu at wurquhart.co.uk
Sat May 1 17:11:08 CEST 2010


Hi List,

I code that detects the insertion of USB devices and in particular COMM 
Ports.

When a new port is detected I want to display in the status bar of my UI 
the 'friendly' name for this device. However, when I do this all I get 
is a single character 'C'. But the native windows application displays 
'COM5' as I would expect.

I've checked the project settings for UNICODE but there is nothing in 
the Preprocessor Definitions set for this so I'm a bit confused to say 
the least.

Can anyone shed some light on this please?

QT Code that doesn't work:

bool USBEraser::winEvent(MSG * msg, long * result)
{
	if (msg->message == WM_DEVICECHANGE)
	{
		PDEV_BROADCAST_HDR pDevHdr = (PDEV_BROADCAST_HDR) msg->lParam ;
		char name[256] = {0} ;
		switch(msg->wParam)
		{
			case DBT_DEVICEARRIVAL:
				{
					if (pDevHdr->dbch_devicetype == DBT_DEVTYP_PORT)
					{
						PDEV_BROADCAST_PORT pPort = (PDEV_BROADCAST_PORT) pDevHdr ;
						strncpy_s(name, 255, pPort->dbcp_name, 255) ;
						ui.statusBar->showMessage(tr("Device Name : %1").arg(name), 0) ;
					}
					else
					{
						::QMessageBox::information(this, "New Device", "Not interested in 
it") ;
					}
				}
				break ;
			case DBT_DEVICEREMOVECOMPLETE:
				{
					if (pDevHdr->dbch_devicetype == DBT_DEVTYP_PORT)
					{
						PDEV_BROADCAST_PORT pPort = (PDEV_BROADCAST_PORT) pDevHdr ;
						strncpy_s(name, 255, pPort->dbcp_name, 255) ;
						ui.statusBar->showMessage(tr("%1 was removed.").arg(name)) ;
						
					}
				}
				break ;
		}
		*result = TRUE ;
		return(true);
	}
	return(false);
}

Windows Code that works:

		...
		PDEV_BROADCAST_HDR pDevHdr = (PDEV_BROADCAST_HDR) lParam ;
		char name[256] = {0} ;
		switch(wParam)
		{
			case DBT_DEVICEARRIVAL :
				{
					//MessageBox (hWnd, "Device arrived", "Info", MB_OK);
					if (pDevHdr->dbch_devicetype == DBT_DEVTYP_PORT)
					{
						PDEV_BROADCAST_PORT pPort = (PDEV_BROADCAST_PORT) pDevHdr ;
						strncpy_s(name, 255, pPort->dbcp_name, 255) ;
						
						MessageBox (hWnd, name, "Inserted", MB_OK);
					}
					else
					{
						MessageBox (hWnd, "Don't care about this device.", "Info", MB_OK);
					}
				}
				break ;
			case DBT_DEVICEREMOVECOMPLETE:
				{
					if (pDevHdr->dbch_devicetype == DBT_DEVTYP_PORT)
					{
						PDEV_BROADCAST_PORT pPort = (PDEV_BROADCAST_PORT) pDevHdr ;
						strncpy_s(name, 255, pPort->dbcp_name, 255) ;
						MessageBox (hWnd, name, "Removed", MB_OK);
					}
				}
				break ;
			}
		}



More information about the Qt-interest-old mailing list