Click here to Skip to main content
15,899,313 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 113.7K   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

 
GeneralNot bad! Pin
Jon Feider25-Jun-07 13:44
Jon Feider25-Jun-07 13:44 
GeneralRe: Not bad! Pin
Iain Clarke, Warrior Programmer25-Jun-07 22:20
Iain Clarke, Warrior Programmer25-Jun-07 22:20 
GeneralRe: Not bad! Pin
Jon Feider25-Jun-07 22:59
Jon Feider25-Jun-07 22:59 
GeneralRe: Not bad! Pin
Iain Clarke, Warrior Programmer25-Jun-07 23:29
Iain Clarke, Warrior Programmer25-Jun-07 23:29 
GeneralEasy way to use explorer icons. Pin
dgendreau20-Jun-07 11:08
dgendreau20-Jun-07 11:08 
GeneralRe: Easy way to use explorer icons. Pin
Iain Clarke, Warrior Programmer20-Jun-07 22:40
Iain Clarke, Warrior Programmer20-Jun-07 22:40 
GeneralRe: Easy way to use explorer icons. Pin
dgendreau21-Jun-07 7:49
dgendreau21-Jun-07 7:49 
GeneralRe: Easy way to use explorer icons. Pin
dgendreau21-Jun-07 8:09
dgendreau21-Jun-07 8:09 
Okay. I traced the code in my directory picker.

A slight correction of my code description. It attaches to your application's local shell icon cache, not the global icon cache.

The behavior you are seeing is correct. Your local imagelist will grow as you call GetShellIcon on different paths. If drives have the same icon, it just returns the same subscript. So the reason you only see 4-5 icons is because you havent called GetShellIcon for other drives, folders or network paths yet.

I also have another helper function that can get the icons for special shell folders like Desktop, Recycle Bin, My Documents, Network Neighborhood etc, but you have to use PIDLs instead of pathnames to mess with that.

-Dan G.
GeneralRe: Easy way to use explorer icons. Pin
Iain Clarke, Warrior Programmer21-Jun-07 21:56
Iain Clarke, Warrior Programmer21-Jun-07 21:56 
GeneralRe: Easy way to use explorer icons. Pin
AlexEvans26-Jun-07 19:24
AlexEvans26-Jun-07 19:24 
GeneralNice Pin
DaTxomin20-Jun-07 0:10
DaTxomin20-Jun-07 0:10 

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.