Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using Raymond Chen's advice, and someone else's technique (can't remember where I found it) I present a function that will find the Recycle Bin directory on a drive. The function cycles through the directories in the root directory looking at hidden and/or system directories. When it finds one, it checks the child subdirectories looking for one that has CLSID_Recycle Bin.</p> <p>Note that I've included two GetFolderCLSID functions below. Raymond Chen's is the simpler one, but it doesn't work on Windows 2000. The other implementation is longer, but appears to work everywhere.</p> <p>Call like: CString recycleDir = FindRecycleBinOnDrive(L"C:\");</p> <pre><code>CString FindRecycleBinOnDrive(LPCWSTR path) { CString search; search.Format(L"%c:\\*", path[0]); WIN32_FIND_DATA fd = {0}; HANDLE fHandle = FindFirstFile(search, &amp;fd); while(INVALID_HANDLE_VALUE != fHandle) { if(FILE_ATTRIBUTE_DIRECTORY == (fd.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY)) //only check directories { if(0 != (fd.dwFileAttributes &amp; (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) //only check hidden and/or system directories { //the recycle bin directory itself won't be marked, but a SID-specific child directory will, so now look at them CString childSearch; childSearch.Format(L"%c:\\%s\\*", path[0], fd.cFileName); WIN32_FIND_DATA childFD = {0}; HANDLE childHandle = FindFirstFile(childSearch, &amp;childFD); while(INVALID_HANDLE_VALUE != childHandle) { if((FILE_ATTRIBUTE_DIRECTORY == (childFD.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY)) &amp;&amp; //only check directories (childFD.cFileName[0] != L'.')) //don't check . and .. dirs { CString fullPath; fullPath.Format(L"%c:\\%s\\%s", path[0], fd.cFileName, childFD.cFileName); CLSID id = {0}; HRESULT hr = GetFolderCLSID(fullPath, id); if(SUCCEEDED(hr)) { if(IsEqualGUID(CLSID_RecycleBin, id)) { FindClose(childHandle); FindClose(fHandle); //return the parent (recycle bin) directory fullPath.Format(L"%c:\\%s", path[0], fd.cFileName); return fullPath; } } else { Log(logERROR, L"GetFolderCLSID returned %08X for %s", hr, fullPath); } } if(FALSE == FindNextFile(childHandle, &amp;childFD)) { FindClose(childHandle); childHandle = INVALID_HANDLE_VALUE; } } } } if(FALSE == FindNextFile(fHandle, &amp;fd)) { FindClose(fHandle); fHandle = INVALID_HANDLE_VALUE; } } _ASSERT(0); return L""; } //Works on Windows 2000, and even as Local System account HRESULT GetFolderCLSID(LPCWSTR path, CLSID&amp; pathCLSID) { LPMALLOC pMalloc = NULL; HRESULT hr = 0; if (SUCCEEDED(hr = SHGetMalloc(&amp;pMalloc))) { LPSHELLFOLDER pshfDesktop = NULL; if (SUCCEEDED(hr = SHGetDesktopFolder(&amp;pshfDesktop))) { LPITEMIDLIST pidl = NULL; DWORD dwAttributes = SFGAO_FOLDER; if (SUCCEEDED(hr = pshfDesktop-&gt;ParseDisplayName(NULL, NULL, (LPWSTR)path, NULL, &amp;pidl, &amp;dwAttributes))) { LPPERSIST pPersist = NULL; if (SUCCEEDED(hr = pshfDesktop-&gt;BindToObject(pidl, NULL, IID_IPersist, (LPVOID *) &amp;pPersist))) { hr = pPersist-&gt;GetClassID(&amp;pathCLSID); pPersist-&gt;Release(); } pMalloc-&gt;Free(pidl); } pshfDesktop-&gt;Release(); } pMalloc-&gt;Release(); } return hr; } //Not supported on Windows 2000 since SHParseDisplayName wasn't implemented then //HRESULT GetFolderCLSID(LPCWSTR pszPath, CLSID&amp; pathCLSID) //{ // SHDESCRIPTIONID did = {0}; // HRESULT hr = 0; // LPITEMIDLIST pidl = NULL; // if (SUCCEEDED(hr = SHParseDisplayName(pszPath, NULL, &amp;pidl, 0, NULL))) //not supported by Windows 2000 // { // IShellFolder *psf = NULL; // LPCITEMIDLIST pidlChild = NULL; // if (SUCCEEDED(hr = SHBindToParent(pidl, IID_IShellFolder, (void**)&amp;psf, &amp;pidlChild))) // { // hr = SHGetDataFromIDList(psf, pidlChild, SHGDFIL_DESCRIPTIONID, &amp;did, sizeof(did)); // psf-&gt;Release(); // pathCLSID = did.clsid; // } // CoTaskMemFree(pidl); // } // return hr; //} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload