Click here to Skip to main content
12,700,966 members (29,718 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked

Using the shell to receive notification of removable media being inserted or removed.

, 20 Nov 2002
Rate this:
Please Sign up or sign in to vote.
Uses the poorly documented SHChangeNotifyRegister function to receive notification upon shell events.


Recently I needed to determine when removable media, such as a zip disk or media card, had been inserted by the user.

I initially looked into the WM_DEVICECHANGE broadcast message only to realise this supports CDROMs and little else. Fortunately there is another way via the shell, through the scarcely documented SHChangeNotifyRegister function.

We can register to be informed by the shell when certain events of interest occur. The events are numerous - covering when a drive is added or removed, files are renamed, etc. However of particular interest here are the notifications for media being inserted or removed.

Its scarcely rocket science but piecing the various bits of information together to utilise this functionality took far longer than it should. I was amazed there was no article here on CP covering this, so figured I should rectify that!


You will need the following includes and definition:
#include <shlobj.h>
Together with a member variable to release the request later:
ULONG m_ulSHChangeNotifyRegister;
Nullify the member variable in your constructor, then register to be notified by the shell (perhaps in OnInitDialog for example):
    ulSHChangeNotifyRegister = NULL;

BOOL CMyWnd::OnInitDialog() 

    // Request notification from shell of media insertion -
    // allows us to detect removable media or a multimedia card
    HWND hWnd = GetSafeHwnd();
    if(SHGetSpecialFolderLocation(hWnd, CSIDL_DESKTOP, &ppidl) == NOERROR)
        SHChangeNotifyEntry shCNE;
        shCNE.pidl = ppidl;
        shCNE.fRecursive = TRUE;

        // Returns a positive integer registration identifier (ID).
        // Returns zero if out of memory or in response to invalid parameters.
        m_ulSHChangeNotifyRegister = SHChangeNotifyRegister(hWnd,  <BR>                                          // Hwnd to receive notification
                SHCNE_DISKEVENTS,                          <BR>                                          // Event types of interest (sources)
                SHCNE_MEDIAINSERTED|SHCNE_MEDIAREMOVED,    <BR>                                          // Events of interest - use <BR>                                          // SHCNE_ALLEVENTS for all events
                WM_USER_MEDIACHANGED,     <BR>                                          // Notification message to be sent <BR>                                          // upon the event
                1,                         <BR>                                          // Number of entries in the pfsne array
                &shCNE);  // Array of SHChangeNotifyEntry structures that <BR>                          // contain the notifications. This array should <BR>                          // always be set to one when calling SHChnageNotifyRegister
                          // or SHChangeNotifyDeregister will not work properly.
        ASSERT(m_ulSHChangeNotifyRegister != 0);    // Shell notification failed
        ASSERT(FALSE);    // Failed to get desktop location
Add a message handler to your message map for the notification message:
Together with the corresponding declaration:
afx_msg LRESULT OnMediaChanged(WPARAM, LPARAM);
Then deal with the message as desired:
// The lParam value contains the event SHCNE_xxxx
// The wParam value supplies the SHNOTIFYSTRUCT

typedef struct {
    DWORD dwItem1;    // dwItem1 contains the previous PIDL or name of the folder. 
    DWORD dwItem2;    // dwItem2 contains the new PIDL or name of the folder. 

LRESULT CMyWnd::OnMediaChanged(WPARAM wParam, LPARAM lParam)
    CString strPath, strMsg;

        case SHCNE_MEDIAINSERTED:        // media inserted event
            strPath = GetPathFromPIDL(shns->dwItem1);
                // Process strPath as required, for now a simple message box
                strMsg.Format("Media inserted into %s", strPath);
        case SHCNE_MEDIAREMOVED:        // media removed event
            strPath = GetPathFromPIDL(shns->dwItem1);
                // Process strPath as required, for now a simple message box
                strMsg.Format("Media removed from %s", strPath);
        //case SHCNE_xxxx:  Add other events processing here
    return NULL;

CString CMyWnd::GetPathFromPIDL(DWORD pidl)
    char sPath[MAX_PATH];
    CString strTemp = _T("");
    if(SHGetPathFromIDList((struct _ITEMIDLIST *)pidl, sPath))
        strTemp = sPath;
    return strTemp;
Finally deregister your request when no longer required:
void CMyWnd::OnDestroy() 




This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionCould you give me a full demo source? Pin
VioletPirate19-Oct-11 0:54
memberVioletPirate19-Oct-11 0:54 
GeneralSame message received twice Pin
nanfang24-May-08 10:57
membernanfang24-May-08 10:57 
GeneralTo check whether any USB device is connected Pin
Member #384610121-Feb-07 4:16
memberMember #384610121-Feb-07 4:16 
GeneralRe: To check whether any USB device is connected Pin
bzucko2-Mar-07 0:54
memberbzucko2-Mar-07 0:54 
GeneralConsole App Pin
Beng Heng22-Mar-05 22:53
memberBeng Heng22-Mar-05 22:53 
GeneralRe: Console App Pin
pada445-Apr-06 13:28
memberpada445-Apr-06 13:28 
QuestionNo notification messages for floppy inserted or removed? Pin
Micheal Huang14-Dec-04 23:19
memberMicheal Huang14-Dec-04 23:19 
AnswerRe: No notification messages for floppy inserted or removed? Pin
Obliterator15-Dec-04 1:16
memberObliterator15-Dec-04 1:16 
GeneralRe: No notification messages for floppy inserted or removed? Pin
<font color=#3399ff>ThatsAlok22-Feb-05 20:08
memberThatsAlok22-Feb-05 20:08 
GeneralWhen app not running Pin
bryce2-Jun-04 21:59
memberbryce2-Jun-04 21:59 
GeneralRe: When app not running Pin
Obliterator3-Jun-04 2:12
memberObliterator3-Jun-04 2:12 
GeneralRe: When app not running Pin
bryce3-Jun-04 3:08
memberbryce3-Jun-04 3:08 
GeneralLinker error Pin
Oyvind10-May-04 21:04
memberOyvind10-May-04 21:04 
GeneralRe: Linker error Pin
Obliterator11-May-04 5:24
memberObliterator11-May-04 5:24 
Generalthis doesn't compile Pin
Anonymous23-Apr-04 19:05
sussAnonymous23-Apr-04 19:05 
GeneralRe: this doesn't compile Pin
HakanErd25-Apr-04 11:15
memberHakanErd25-Apr-04 11:15 
GeneralExcept when Autorun is off Pin
MrLeeGriffiths16-May-03 0:43
memberMrLeeGriffiths16-May-03 0:43 
Generalsome remarks Pin
umeca7427-Nov-02 1:57
memberumeca7427-Nov-02 1:57 
GeneralRe: some remarks Pin
Obliterator27-Nov-02 5:29
memberObliterator27-Nov-02 5:29 
GeneralRe: some remarks Pin
umeca7428-Nov-02 1:24
memberumeca7428-Nov-02 1:24 
GeneralI cannot compile this Pin
Alex F24-Nov-02 21:17
memberAlex F24-Nov-02 21:17 
GeneralThe same with SP3 Pin
Alex F24-Nov-02 22:51
memberAlex F24-Nov-02 22:51 
GeneralRe: The same with SP3 Pin
Obliterator25-Nov-02 3:57
memberObliterator25-Nov-02 3:57 
Generalfor reference (slightly off topic) Pin
mgama21-Nov-02 8:19
membermgama21-Nov-02 8:19 
GeneralRe: for reference (slightly off topic) Pin
Obliterator21-Nov-02 16:09
memberObliterator21-Nov-02 16:09 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 21 Nov 2002
Article Copyright 2002 by Obliterator
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid