Detect usb insertion and removal

Hi,
I'd like to ask if it is possible to auto detect usb stick insertion and removal in c++? This is possible in c#, but I'd like to make it portable so creating it in c++ maybe a good choice.
If it is possible, how can I do that can you give me a clue?

thanks in advance

regards
gilmar
Just copy-paste the MSDN code which does exactly that (USB device notification sample)
Last edited on
Thanks.
Ok, i just pasted the msdn code sample. I modified it a little bit use it in my sample application and it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void CSampleWM_MessageDlg::Main_OnDeviceChange (HWND hwnd, WPARAM wParam, LPARAM lParam)
{

      PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;      	 
		
      switch(wParam)
      {
      case DBT_DEVICEARRIVAL:
         // See if a CD-ROM or DVD was inserted into a drive.
         m_eRecieveMsgs.SetWindowText("Inserted\r\n");
        break;


     case DBT_DEVICEREMOVECOMPLETE:
        // See if a CD-ROM was removed from a drive.
        m_eRecieveMsgs.SetWindowText("Removed\r\n");
        break;

     default:
	 m_eRecieveMsgs.SetWindowText("Default\r\n"); //IT FALLS ALWAYS HERE.
           }
   }


The problem is when I inserted the cd/dvd the textbox(m_eRecieveMsgs) always print the Default message in the default case. It will not go inside the first case statement of the switch. This happens also when removing the cd/dvd always go through the default case.
The wParam always return a zero value even if it is an insert or remove process, this is why it always falls to the default.

Why is that? Is there something wrong with my code?
Hope you can help me on this.

thanks in advance
gilmar
Last edited on
Topic archived. No new replies allowed.