How to detect when a device is added to the system?

I've had this problem for a while now but I have to remedy it as I must tackle it now. I'm trying to detect when a device is connected to the system in windows 7 and I receive the WM_DEVICECHANGE just fine, however the message in wParam is always DBT_DEVNODES_CHANGED. Never a DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE. I have had no luck googling thus far so I'm hoping someone will point me in the right direction before I find something in a few hours time :) Thanks for ya time.


The documentation states that the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE events should be received by default. The application can register to receive more detailed events by calling the RegisterDeviceNotification function. Hopefully you will receive the events you need after registering your app. Be sure to call UnregisterDeviceNotification upon closedown.

RegisterDeviceNotification reference:

P/Invoke Signature:

UnregisterDeviceNotification reference:

P/Invoke Signature:

Thanks very much Fletcher. I have succeded in doing this for Qt4. Qt4(.6.3) users can add the #include files as normal but you will need to add some #defines to your .h file:

#define _WIN32_WINNT 0x0501
#define _WIN32_WINDOWS 0x0501
#define WINVER 0x0501

so that the windows data types like DEV_BROADCAST_HEADER can be recognised. Also the version number I guess depends on your windows platform but not sure (I use win 7 atm). Also don't forget to add the libraries to your .pro files LIBS += "C:\Qt\2010.04\mingw\lib\<library_file>" if you installed in the default position that is.

Need Your Help

Difference in connecting mysql functions?

php mysql

Can anyone tell the difference between the php functions mysql_connect(),mysql_pconnect() and mysqli_connect()? So that it will be a reference for all with this question.

Hide element based on value of other elements

javascript jquery html

I am trying to hide a table based on the value of two fields, so that if field2 is equal to field1 the table is hidden.