// INamespaceWalkCB IFACEMETHODIMP FoundItem(IShellFolder *psf, PCUITEMID_CHILD pidl) { HRESULT hr = S_OK; if (_fCountingFiles) { _cFilesTotal++; } else { _cFileCur++; IShellItem2 *psi; hr = SHCreateItemWithParent(NULL, psf, pidl, IID_PPV_ARGS(&psi)); if (SUCCEEDED(hr)) { PWSTR pszName; hr = psi->GetDisplayName(SIGDN_NORMALDISPLAY, &pszName); if (SUCCEEDED(hr)) { _ppd->SetProgress64(_cFileCur, _cFilesTotal); _ppd->SetLine(2, pszName, TRUE, NULL); hr = _ProcessItem(psi); CoTaskMemFree(pszName); } psi->Release(); } } return _ppd->HasUserCancelled() ? HRESULT_FROM_WIN32(ERROR_CANCELLED) : hr; }
// INamespaceWalkCB IFACEMETHODIMP FoundItem(IShellFolder *psf, PCUITEMID_CHILD pidl) { IShellItem *psi; HRESULT hr = SHCreateItemWithParent(NULL, psf, pidl, IID_PPV_ARGS(&psi)); if (SUCCEEDED(hr)) { hr = AddItemToTreeView(_hwndTreeView, psi, _rghTreeParentItemArray[_iCurTreeDepth]) ? S_OK : E_FAIL; psi->Release(); } return hr; }
HRESULT GetCurrent(REFIID riid, void **ppv) { *ppv = NULL; if (SUCCEEDED(_hr)) { // Create the childID by truncating _pidlRel temporarily PUIDLIST_RELATIVE pidlNext = ILNext(_pidlRel); const WORD cbSave = pidlNext->mkid.cb; // Save old cb pidlNext->mkid.cb = 0; // Make _pidlRel a child _hr = SHCreateItemWithParent(NULL, _psfCur, (PCUITEMID_CHILD)_pidlRel, riid, ppv); pidlNext->mkid.cb = cbSave; // Restore old cb } return _hr; }
IFACEMETHODIMP EnterFolder(IShellFolder *psf, PCUITEMID_CHILD pidl) { HRESULT hr = (_iCurTreeDepth < ARRAYSIZE(_rghTreeParentItemArray) - 1) ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { IShellItem *psi; hr = SHCreateItemWithParent(NULL, psf, pidl, IID_PPV_ARGS(&psi)); if (SUCCEEDED(hr)) { _rghTreeParentItemArray[_iCurTreeDepth + 1] = AddItemToTreeView(_hwndTreeView, psi, _rghTreeParentItemArray[_iCurTreeDepth]); hr = _rghTreeParentItemArray[_iCurTreeDepth + 1] ? S_OK : E_FAIL; _iCurTreeDepth++; psi->Release(); } } return hr; }
HRESULT ShellItemsLoader::EnumerateFolderItemsNonRecursive(IShellItem* currentBrowseLocation, ShellFileType fileType, std::vector<ComPtr<IShellItem> >& shellItems) { std::vector<std::wstring> itemKinds; if ((fileType & FileTypeImage) == FileTypeImage) { itemKinds.push_back(L"picture"); } if ((fileType & FileTypeImage) == FileTypeVideo) { itemKinds.push_back(L"video"); } if ((fileType & FileTypeImage) == FileTypeAudio) { itemKinds.push_back(L"music"); } // Enumerate all objects in the current search folder ComPtr<IShellFolder> searchFolder; HRESULT hr = currentBrowseLocation->BindToHandler(nullptr, BHID_SFObject, IID_PPV_ARGS(&searchFolder)); if (SUCCEEDED(hr)) { bool const isEnumFolders = (fileType & FileTypeFolder) == FileTypeFolder; SHCONTF const flags = isEnumFolders ? SHCONTF_FOLDERS : SHCONTF_NONFOLDERS; ComPtr<IEnumIDList> fileList; if (S_OK == searchFolder->EnumObjects(nullptr, flags, &fileList)) // EnumObjects has "empty success semantics", so it could also return S_FALSE. Thus, we only check for S_OK { ITEMID_CHILD* idList = nullptr; unsigned long fetched; while (S_OK == fileList->Next(1, &idList, &fetched)) { ComPtr<IShellItem2> shellItem; hr = SHCreateItemWithParent(nullptr, searchFolder, idList, IID_PPV_ARGS(&shellItem)); if (SUCCEEDED(hr)) { if (isEnumFolders) { shellItems.push_back(static_cast<IShellItem*>(shellItem)); } else { // Check if we the item is correct wchar_t *itemType = nullptr; hr = shellItem->GetString(PKEY_Kind, &itemType); if (SUCCEEDED(hr)) { auto found = std::find(itemKinds.begin(), itemKinds.end(), itemType); if (found != itemKinds.end()) { shellItems.push_back(static_cast<IShellItem*>(shellItem)); } ::CoTaskMemFree(itemType); } } } ILFree(idList); } } } return hr; }