free hit counter

Friday, February 03, 2006

listening for device removal / insertion

It's not necessarily a SetupDixxx function as I had thought.

RegisterDeviceNotification is the magic word.

class WinDeviceInsertionListeningApp : public QApplication
{
Q_OBJECT
const int VICE_NOTIFY_ALL_INTERFACE_CLASSES = 0x00000004;
const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;

public:
WinDeviceInsertionListeningApp( int & argc, char ** argv )
: QApplication (argc, argv)
{
}

void registerDeviceNotification(QWidget* mainWindow)
{
static /*const*/ GUID GUID_DEVINTERFACE_USB_DEVICE =
{ 0xA5DCBF10L, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };

DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;

ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;

HANDLE result= RegisterDeviceNotification(mainWindow->winId(),
&NotificationFilter,
DEVICE_NOTIFY_ALL_INTERFACE_CLASSES|DEVICE_NOTIFY_WINDOW_HANDLE);

if (result == NULL)
cout << "Can't register for notification!" << endl;

}

bool winEventFilter(MSG * msg, long * result )
{
bool functionResult= false;

static int i=0;

if (msg->message == WM_DEVICECHANGE ) //
{
if (msg->wParam == DBT_DEVICEARRIVAL)
cout << endl << "device insert!" << endl;

if (msg->wParam == DBT_DEVICEREMOVECOMPLETE)
{
cout << endl << "device removal!" << endl;
exit(0);
}
}
else
{
i++;
if ((i %100) == 0)
cout << "." << endl;

}

return functionResult;
}
};


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/registerdevicenotification.asp
http://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/Q_20971945.html

http://www.codeguru.com/forum/archive/index.php/t-331907.html


This is a great usenet posting:
Google Groups : microsoft.public.windowsxp.device_driver.dev

And a fun artical about interfacing to a temp sensor by using the generic HID driver (includes usage of device notifications:
http://www.edn.com/article/CA243218.html

0 Comments:

Post a Comment

<< Home