// 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
// }
return TRUE; // return TRUE unless you set the focus to a control
}
...
void CTry01Dlg::OnDestroy()
{
// {
if(m_ulSHChangeNotifyRegister)
VERIFY(SHChangeNotifyDeregister(m_ulSHChangeNotifyRegister));
// }
CDialog::OnDestroy();
// TODO: Add your message handler code here
}
// 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 CTry01Dlg::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);
}
}
break;
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);
}
}
break;
//case SHCNE_xxxx: Add other events processing here
if (SUCCEEDED(hr))
{
// Convert the item ID list's binary
// representation into a file system path
char szPath[_MAX_PATH];
if (SHGetPathFromIDList(pidl, szPath))
Result = szPath;
else
ASSERT(FALSE);
}
else
ASSERT(FALSE);
// Assume all folders except folders based on CSIDL_PROGRAMS. This is because the installer
// creates a separate folder for the "project" in which links for all executables will be
// created. Since the project folder is not a system folder, it might have to be created.
if(nFolder == CSIDL_PROGRAMS)
{
CString Path = GetSpecialFolderLocation(nFolder) + ("\\") + LinkFilename;
if(_access(Path, 2) != 0)
{
if(!MakeSurePathExists(Path))
{
CString err;
err.Format(_T("Could not create Start menu directory %s."), Path);
AfxMessageBox(err);
return;
}
}
}
// Get a pointer to the IShellLink interface.
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (PVOID *) &psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Set the path to the shortcut target and add the description.
psl->SetPath(ExePath);
psl->SetWorkingDirectory(WorkingDirectory);
psl->SetDescription(Description);
// Query IShellLink for the IPersistFile interface for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (PVOID *) &ppf);
if (SUCCEEDED(hres))
{
WCHAR wszTemp[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, PathLink, -1, wszTemp, MAX_PATH);
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(wszTemp, TRUE);
ppf->Release();
}
psl->Release();
}
}