You now have all the essential elements needed to navigate anywhere in the namespace. The simplest way to start is to have your application call SHGetDesktopFolder to get the desktop's IShellFolder interface. Then, to navigate downward through the namespace, your application can follow these steps:
Repeat these steps as often as necessary to reach the target.
The following piece of sample code is a simple console application that illustrates a number of the procedures discussed in the preceding sections. Error checking has been omitted for clarity. The application performs the following tasks:
#include <shlobj.h> #include <shlwapi.h> #include <iostream.h> main() { LPMALLOC pMalloc; LPITEMIDLIST pidlProgFiles = NULL; LPITEMIDLIST pidlItems = NULL; IShellFolder *psfFirstFolder = NULL; IShellFolder *psfDeskTop = NULL; IShellFolder *psfProgFiles = NULL; LPENUMIDLIST ppenum = NULL; ULONG celtFetched; HRESULT hr; STRRET strDispName; TCHAR pszDisplayName[MAX_PATH]; ULONG uAttr; CoInitialize(); hr = SHGetMalloc(&pMalloc); hr = SHGetFolderLocation(NULL, CSIDL_PROGRAM_FILES, NULL, NULL, &pidlProgFiles); hr = SHGetDesktopFolder(&psfDeskTop); hr = psfDeskTop->BindToObject(pidlProgFiles, NULL, IID_IShellFolder, (LPVOID *) &psfProgFiles); psfDeskTop->Release(); hr = psfProgFiles->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum); while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1) { psfProgFiles->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName); StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH); cout << pszDisplayName << '\n'; if(!psfFirstFolder) { uAttr = SFGAO_FOLDER; psfProgFiles->GetAttributesOf(1, (LPCITEMIDLIST *) &pidlItems, &uAttr); if(uAttr & SFGAO_FOLDER) { hr = psfProgFiles->BindToObject(pidlItems, NULL, IID_IShellFolder, (LPVOID *) &psfFirstFolder); } } pMalloc->Free(pidlItems); } cout << "\n\n"; ppenum->Release(); if(psfFirstFolder) { hr = psfFirstFolder->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum); while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1) { psfFirstFolder->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName); StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH); cout << pszDisplayName << '\n'; pMalloc->Free(pidlItems); } } ppenum->Release(); pMalloc->Free(pidlProgFiles); psfProgFiles->Release(); psfFirstFolder->Release(); return 0; CoUninitialize; }