Click here to Skip to main content
Licence CPOL
First Posted 19 Jun 2007
Views 50,809
Downloads 1,093
Bookmarked 51 times

Detecting when drives are added or removed

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.

ON_WM_DEVICECHANGE ()

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

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)

About the Author

Iain Clarke, Warrior Programmer

Software Developer (Senior)

Sweden Sweden

Member

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!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMissing file PinmemberXaRoP7:08 4 Jul '07  
GeneralRe: Missing file PinmemberIain Clarke10:35 4 Jul '07  
GeneralRe: Missing file PinmemberIain Clarke22:21 4 Jul '07  
QuestionWhat about insert devices PinmemberVC Spark2:28 3 Jul '07  
AnswerRe: What about insert devices PinmemberIain Clarke4:16 3 Jul '07  
GeneralRe: What about insert devices PinmemberVC Spark2:32 4 Jul '07  
GeneralNot bad! PinmemberJon Feider13:44 25 Jun '07  
Short, sweet, and seems to work well with DVD drives and card readers. It doesn't detect my camera, but I suppose Vista's image import wizard (gotta love+hate it) could be getting in the way somehow.
GeneralRe: Not bad! PinmemberIain Clarke22:20 25 Jun '07  
GeneralRe: Not bad! PinmemberJon Feider22:59 25 Jun '07  
GeneralRe: Not bad! PinmemberIain Clarke23:29 25 Jun '07  
GeneralEasy way to use explorer icons. Pinmemberdgendreau11:08 20 Jun '07  
GeneralRe: Easy way to use explorer icons. PinmemberIain Clarke22:40 20 Jun '07  
GeneralRe: Easy way to use explorer icons. Pinmemberdgendreau7:49 21 Jun '07  
GeneralRe: Easy way to use explorer icons. Pinmemberdgendreau8:09 21 Jun '07  
GeneralRe: Easy way to use explorer icons. PinmemberIain Clarke21:56 21 Jun '07  
GeneralRe: Easy way to use explorer icons. PinmemberAlexEvans19:24 26 Jun '07  
GeneralNice PinmemberDaTxomin0:10 20 Jun '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 5 Jul 2007
Article Copyright 2007 by Iain Clarke, Warrior Programmer
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid