?
Solved

How to programatically when a device is inserted in USB drive

Posted on 2006-04-07
9
Medium Priority
?
2,294 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:amit_chuks
  • 3
  • 3
6 Comments
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 16403437
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16406333
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16406346
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16406454
I didn't forget the code, just didn't bother posting it as it is available in the help.
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16406676
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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16407019
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question