Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC

Detecting when drives are added or removed

Rate me:
Please Sign up or sign in to vote.
4.30/5 (22 votes)
4 Jul 2007CPOL1 min read 112.8K   3.2K   57   37
Dealing with the WM_DEVICECHANGE message to detect volumes being added or removed.

Screenshot - Drive_Detect.jpg

Introduction

This is possibly my smallest article, and just shows a method of detecting when drives are added to or removed from the system. What you do with that information is up to you!

Background

I was answering a question from Akin Ocal yesterday, when Matthew Faithful came up with a much deeper answer than mine.

In a fit of pique / curiosity, I thought I'd come up with a different way of doing what he described, using Windows messages to do the heavy lifting. As I had to do a bit of digging from barely remembered details, I thought I'd share the work with others.

Using the code

I created a blank MFC dialog project, and added a handler for WM_DEVICECHANGE. The MFC handler structure exists, but it could not be added with the Class Wizard, so I typed it in manually.

C++
ON_WM_DEVICECHANGE ()

I then added the actual handler, which does nothing except log the fact that a drive has been added or removed.

C++
BOOL CDriveDetectDlg::OnDeviceChange( UINT nEventType, DWORD dwData )
{
    BOOL bReturn = CWnd::OnDeviceChange (nEventType, dwData);

    DEV_BROADCAST_VOLUME *volume = (DEV_BROADCAST_VOLUME *)dwData;
    CString log;

    if (nEventType == DBT_DEVICEARRIVAL)
    {
        if (volume->dbcv_devicetype == DBT_DEVTYP_VOLUME)
        {
            for (int n = 0; n < 32; n++)
            {
                if (IsBitSet (volume->dbcv_unitmask, n))
                {
                    log.Format ("Drive %c: Inserted\n", n + 'A');
                    m_DetectLog.AddString (log);
                }
            }
        }
    }

    if (nEventType == DBT_DEVICEREMOVECOMPLETE)
    {
        if (volume->dbcv_devicetype == DBT_DEVTYP_VOLUME)
        {
            for (int n = 0; n < 32; n++)
            {
                if (IsBitSet (volume->dbcv_unitmask, n))
                {
                    log.Format ("Drive %c: Removed\n", n + 'A');
                    m_DetectLog.AddString (log);
                }
            }
        }
    }

    return bReturn;
}

As you can see, there's not a vast amount of code for the job.

Updates

  • 27/6/2007 - Little drive icons added next to the drives being added / removed. The credit for steering me in the right direction (and providing some code for me to steal^h^h^h^h^hreuse) goes to dgendreau.
  • 20/6/2007 - Original version posted.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
I have now moved to Sweden for love, and recently married a lovely Swede.


-----------------
I started programming on BBC micros (6502) when I was six and never quite stopped even while I was supposed to be studying physics and uni.

I've been working for ~13 years writing software for machine control and data analysis. I now work on financial transaction transformation software, for a Software company in Gamlastan, Stockholm.
Look at my articles to see my excellent coding skills. I'm modest too!

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey18-Feb-12 0:02
professionalManoj Kumar Choubey18-Feb-12 0:02 
QuestionThis code does exactly what I need but the problem is with the MFC components used in it. I need to write the code in Embarcadero c++ builder, can anybody provide suggestions about how to modify the code? Pin
lilyNaz25-Oct-11 20:15
lilyNaz25-Oct-11 20:15 
AnswerRe: This code does exactly what I need but the problem is with the MFC components used in it. I need to write the code in Embarcadero c++ builder, can anybody provide suggestions about how to modify the code? Pin
Iain Clarke, Warrior Programmer12-Dec-11 4:27
Iain Clarke, Warrior Programmer12-Dec-11 4:27 
GeneralShame : copy from MSDN ... Pin
kilt8-Oct-09 23:32
kilt8-Oct-09 23:32 
GeneralRe: Shame : copy from MSDN ... Pin
Iain Clarke, Warrior Programmer13-Oct-09 4:31
Iain Clarke, Warrior Programmer13-Oct-09 4:31 
GeneralRe: Shame : copy from MSDN ... Pin
pnswdv5-Aug-14 5:11
pnswdv5-Aug-14 5:11 
QuestionFound a bug on Win2000 Pin
wangk070529-Dec-07 2:54
wangk070529-Dec-07 2:54 
GeneralRe: Found a bug on Win2000 Pin
Iain Clarke, Warrior Programmer1-Feb-08 3:55
Iain Clarke, Warrior Programmer1-Feb-08 3:55 
GeneralDBT_DEVICEQUERYREMOVE support Pin
64BitWho12-Oct-07 1:13
64BitWho12-Oct-07 1:13 
GeneralRe: DBT_DEVICEQUERYREMOVE support Pin
Iain Clarke, Warrior Programmer12-Oct-07 4:12
Iain Clarke, Warrior Programmer12-Oct-07 4:12 
GeneralRe: DBT_DEVICEQUERYREMOVE support Pin
64BitWho12-Oct-07 4:48
64BitWho12-Oct-07 4:48 
GeneralRe: DBT_DEVICEQUERYREMOVE support Pin
64BitWho13-Oct-07 4:23
64BitWho13-Oct-07 4:23 
My bad - Registering for notification (DBT_DEVICEQUERYREMOVE or any other) will only return a DBT_DEVTYP_HANDLE or DBT_DEVTYP_DEVICEINTERFACE, and NOT a DBT_DEVTYP_VOLUME as shown in my handler. That's why I was not able to get the drive letter out of the 'mask'.

--
Omne Ignotum Pro Magnifico

QuestionRe: DBT_DEVICEQUERYREMOVE support Pin
Vijay_Vijay4-Sep-08 2:10
Vijay_Vijay4-Sep-08 2:10 
GeneralRe: DBT_DEVICEQUERYREMOVE support Pin
Vijay_Vijay4-Sep-08 2:20
Vijay_Vijay4-Sep-08 2:20 
GeneralRe: DBT_DEVICEQUERYREMOVE support Pin
Iain Clarke, Warrior Programmer7-Sep-08 22:40
Iain Clarke, Warrior Programmer7-Sep-08 22:40 
GeneralRe: DBT_DEVICEQUERYREMOVE support Pin
Iain Clarke, Warrior Programmer7-Sep-08 22:43
Iain Clarke, Warrior Programmer7-Sep-08 22:43 
GeneralRe: DBT_DEVICEQUERYREMOVE support Pin
Vijay_Vijay7-Sep-08 23:52
Vijay_Vijay7-Sep-08 23:52 
JokeRe: DBT_DEVICEQUERYREMOVE support Pin
Member 229365217-Sep-09 18:23
Member 229365217-Sep-09 18:23 
QuestionWM_DEVICECHANGE Ok! But what about Linux? Pin
PeterShilkin12-Jul-07 1:11
PeterShilkin12-Jul-07 1:11 
AnswerRe: WM_DEVICECHANGE Ok! But what about Linux? Pin
Iain Clarke, Warrior Programmer14-Jul-07 3:53
Iain Clarke, Warrior Programmer14-Jul-07 3:53 
GeneralMissing file Pin
XaRoP4-Jul-07 7:08
XaRoP4-Jul-07 7:08 
GeneralRe: Missing file Pin
Iain Clarke, Warrior Programmer4-Jul-07 10:35
Iain Clarke, Warrior Programmer4-Jul-07 10:35 
GeneralRe: Missing file Pin
Iain Clarke, Warrior Programmer4-Jul-07 22:21
Iain Clarke, Warrior Programmer4-Jul-07 22:21 
QuestionWhat about insert devices Pin
VC Spark3-Jul-07 2:28
VC Spark3-Jul-07 2:28 
AnswerRe: What about insert devices Pin
Iain Clarke, Warrior Programmer3-Jul-07 4:16
Iain Clarke, Warrior Programmer3-Jul-07 4:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.