Introduction
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.
It's scarcely rocket science but piecing the various bits of information together to utilise this functionality took far longer than it should have. I was amazed there was no article here on CodeProject covering this, so figured I should rectify that!
Implementation
You will need the following include
s and definition:
#include <shlobj.h>
#define WM_USER_MEDIACHANGED WM_USER+88
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):
CMyWnd::CMyWnd()
{
ulSHChangeNotifyRegister = NULL;
}
BOOL CMyWnd::OnInitDialog()
{
CWnd::OnInitDialog();
HWND hWnd = GetSafeHwnd();
LPITEMIDLIST ppidl;
if(SHGetSpecialFolderLocation(hWnd, CSIDL_DESKTOP, &ppidl) == NOERROR)
{
SHChangeNotifyEntry shCNE;
shCNE.pidl = ppidl;
shCNE.fRecursive = TRUE;
m_ulSHChangeNotifyRegister = SHChangeNotifyRegister(hWnd,
SHCNE_DISKEVENTS,
SHCNE_MEDIAINSERTED|SHCNE_MEDIAREMOVED,
WM_USER_MEDIACHANGED,
1,
&shCNE);
ASSERT(m_ulSHChangeNotifyRegister != 0);
}
else
ASSERT(FALSE);
}
Add a message handler to your message map for the notification message:
ON_MESSAGE(WM_USER_MEDIACHANGED, OnMediaChanged)
Together with the corresponding declaration:
afx_msg LRESULT OnMediaChanged(WPARAM, LPARAM);
Then deal with the message as desired:
typedef struct {
DWORD dwItem1;
DWORD dwItem2;
} SHNOTIFYSTRUCT;
LRESULT CMyWnd::OnMediaChanged(WPARAM wParam, LPARAM lParam)
{
SHNOTIFYSTRUCT *shns = (SHNOTIFYSTRUCT *)wParam;
CString strPath, strMsg;
switch(lParam)
{
case SHCNE_MEDIAINSERTED:
{
strPath = GetPathFromPIDL(shns->dwItem1);
if(!strPath.IsEmpty())
{
strMsg.Format("Media inserted into %s", strPath);
AfxMessageBox(strMsg);
}
}
case SHCNE_MEDIAREMOVED:
{
strPath = GetPathFromPIDL(shns->dwItem1);
if(!strPath.IsEmpty())
{
strMsg.Format("Media removed from %s", strPath);
AfxMessageBox(strMsg);
}
}
}
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()
{
if(m_ulSHChangeNotifyRegister)
VERIFY(SHChangeNotifyDeregister(m_ulSHChangeNotifyRegister));
CWnd::OnDestroy();
}
References
License
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.