How to programatically when a device is inserted in USB drive

My application needs to know when ever a user inserts a USB device in the USB port.
I am using VC++ 6.0.
It would be helpfull to know where I can find more info or code snippet.
amit_chuksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Have a look at
CWnd::OnDeviceChange

specifically  DBT_DEVICEARRIVAL

The system broadcasts the DBT_DEVICEARRIVAL device event when a device or piece of media has been inserted and becomes available.



excerpt from help files.
Detecting Media Insertion or Removal
Windows sends all top-level windows a set of default WM_DEVICECHANGE messages when new devices or media (such as a CD or DVD) are added and become available, and when existing devices or media are removed. You do not need to register to receive these default messages. See the Remarks section in RegisterDeviceNotification for details on which messages are sent by default. The messages in the code example below are among the default messages.

Each WM_DEVICECHANGE message has an associated event that describes the change, and a structure that provides detailed information about the change. The structure consists of an event-independent header, DEV_BROADCAST_HDR, followed by event-dependent members. The event-dependent members describe the device to which the event applies. To use this structure, applications must first determine the event type and the device type. Then, they can use the correct structure to take appropriate action.


When the user inserts a new CD or DVD into a drive, applications receive a WM_DEVICECHANGE message with a DBT_DEVICEARRIVAL event. The application must check the event to ensure that the type of device arriving is a volume (the dbch_devicetype member is DBT_DEVTYP_VOLUME) and that the change affects the media (the dbcv_flags member is DBTF_MEDIA).

When the user removes a CD or DVD from a drive, applications receive a WM_DEVICECHANGE message with a DBT_DEVICEREMOVECOMPLETE event. Again, the application must check the event to ensure that the device being removed is a volume and that the change affects the media.

The following code demonstrates how to check for insertion or removal of a CD or DVD.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mahesh1402Commented:
oops it seems AndyAinscow forgot to give code .....here it is :

check for insertion or removal of a CD or DVD.

#include <windows.h>
#include <dbt.h>

void Main_OnDeviceChange (HWND hwnd, WPARAM wParam, LPARAM lParam);
char FirstDriveFromMask (ULONG unitmask);  //prototype

/*------------------------------------------------------------------
   Main_OnDeviceChange (hwnd, wParam, lParam)

   Description
      Handles WM_DEVICECHANGE messages sent to the application's
      top-level window.
--------------------------------------------------------------------*/

void Main_OnDeviceChange (HWND hwnd, WPARAM wParam, LPARAM lParam)
{
   PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
   char szMsg[80];

   switch(wParam)
   {
      case DBT_DEVICEARRIVAL:
         // Check whether a CD or DVD was inserted into a drive.
         if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
         {
            PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

            if (lpdbv -> dbcv_flags & DBTF_MEDIA)
            {
               wsprintf (szMsg, "Drive %c: Media has arrived.\n",
                         FirstDriveFromMask(lpdbv ->dbcv_unitmask));

               MessageBox (hwnd, szMsg, "WM_DEVICECHANGE", MB_OK);
            }
         }
         break;

      case DBT_DEVICEREMOVECOMPLETE:
         // Check whether a CD or DVD was removed from a drive.
         if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
         {
            PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

            if (lpdbv -> dbcv_flags & DBTF_MEDIA)
            {
               wsprintf (szMsg, "Drive %c: Media was removed.\n",
                         FirstDriveFromMask(lpdbv ->dbcv_unitmask));

               MessageBox (hwnd, szMsg, "WM_DEVICECHANGE", MB_OK);
            }
         }
         break;

      default:
           /*
              Process other WM_DEVICECHANGE notifications for other
              devices or reasons.
           */
            ;
   }
}

/*------------------------------------------------------------------
   FirstDriveFromMask (unitmask)

   Description
      Finds the first valid drive letter from a mask of drive letters.
      The mask must be in the format bit 0 = A, bit 1 = B, bit 3 = C,
      etc. A valid drive letter is defined when the corresponding bit
      is set to 1.

   Returns the first drive letter that was found.
--------------------------------------------------------------------*/

char FirstDriveFromMask (ULONG unitmask)
{
   char i;

   for (i = 0; i < 26; ++i)
   {
      if (unitmask & 0x1)
         break;
      unitmask = unitmask >> 1;
   }

   return (i + 'A');
}



MAHESH
0
mahesh1402Commented:
Also remember sometimes if you not receiving DBT_DEVICEARRIVAL ( may be with Win2k OS ) then you may look for DBT_DEVNODES_CHANGED message.

Have a look at this too :

http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_21690183.html
http://www.codeproject.com/dotnet/devicevolumemonitor.asp

MAHESH

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

AndyAinscowFreelance programmer / ConsultantCommented:
I didn't forget the code, just didn't bother posting it as it is available in the help.
0
mahesh1402Commented:
ok I just thought so by reading last line of your comment 'The following code demonstrates how to check for insertion or removal of a CD or DVD'.

MAHESH
0
AndyAinscowFreelance programmer / ConsultantCommented:
That was part of the excerpt from help - I left it in to give an indecation there was code snippets in the help topic.  (Maybe I should have made it clearer - but hindsight is very good for telling you what you should have done ;-) )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.