Search results for 'SHCNE_MEDIAINESERTED'. 1 post(s) found.
Using the shell to receive notification of removable media being inserted or removed

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!
Implementation
You will need the following includes and definition:Together with a member variable to release the request later:
Nullify the member variable in your constructor, then register to be notified by the shell (perhaps in OnInitDialog for example):ULONG m_ulSHChangeNotifyRegister;
Add a message handler to your message map for the notification message:CMyWnd::CMyWnd()
{
ulSHChangeNotifyRegister = NULL;
}
BOOL CMyWnd::OnInitDialog()
{
CWnd::OnInitDialog();
// Request notification from shell of media insertion -
// allows us to detect removable media or a multimedia card
HWND hWnd = GetSafeHwnd();
LPITEMIDLIST ppidl;
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,
// Hwnd to receive notification
SHCNE_DISKEVENTS,
// Event types of interest (sources)
SHCNE_MEDIAINSERTED|SHCNE_MEDIAREMOVED,
// Events of interest - use
// SHCNE_ALLEVENTS for all events
WM_USER_MEDIACHANGED,
// Notification message to be sent
// upon the event
1,
// Number of entries in the pfsne array
&shCNE); // Array of SHChangeNotifyEntry structures that
// contain the notifications. This array should
// always be set to one when calling SHChnageNotifyRegister
// or SHChangeNotifyDeregister will not work properly.
ASSERT(m_ulSHChangeNotifyRegister != 0); // Shell notification failed
}
else
ASSERT(FALSE); // Failed to get desktop location
}
Together with the corresponding declaration:ON_MESSAGE(WM_USER_MEDIACHANGED, OnMediaChanged)
Then deal with the message as desired:afx_msg LRESULT OnMediaChanged(WPARAM, LPARAM);
Finally deregister your request when no longer required:// 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.
} SHNOTIFYSTRUCT;
LRESULT CMyWnd::OnMediaChanged(WPARAM wParam, LPARAM lParam)
{
SHNOTIFYSTRUCT *shns = (SHNOTIFYSTRUCT *)wParam;
CString strPath, strMsg;
switch(lParam)
{
case SHCNE_MEDIAINSERTED: // media inserted event
{
strPath = GetPathFromPIDL(shns->dwItem1);
if(!strPath.IsEmpty())
{
// Process strPath as required, for now a simple message box
strMsg.Format("Media inserted into %s", strPath);
AfxMessageBox(strMsg);
}
}
case SHCNE_MEDIAREMOVED: // media removed event
{
strPath = GetPathFromPIDL(shns->dwItem1);
if(!strPath.IsEmpty())
{
// Process strPath as required, for now a simple message box
strMsg.Format("Media removed from %s", strPath);
AfxMessageBox(strMsg);
}
}
//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;
}
void CMyWnd::OnDestroy()
{
if(m_ulSHChangeNotifyRegister)
VERIFY(SHChangeNotifyDeregister(m_ulSHChangeNotifyRegister));
CWnd::OnDestroy();
}
Another posts included in "C, C++"
| UDP Send and Receive Using CAsyncSocket (0) | 2007/08/28 |
| Creating and Using a CAsyncSocket Object to use CAsyncSocket (0) | 2007/08/28 |
| MFC based World Wide Web HTTP Server Source Code (0) | 2007/08/28 |
| Fast and Good Keyboard/Mouse Test without the message handler (0) | 2007/08/27 |
| Simple CGI Programming in C (0) | 2007/08/27 |
| C++ CGI Example working on Apache (0) | 2007/08/27 |
Trackback : Cannot send a trackbact to this post.
-
Subject different money making ideas
2010/01/29 03:09
moneyideas
-
Subject different money making ideas
2010/01/29 12:19
moneyideas
-
Subject different money making ideas
2010/01/31 16:44
moneyideas
Prev

Rss Feed