Shell Namespace Extension: Enabling Deep Search
Overview
From my past posts about implementing your own Shell Namespace, there have been some great questions posted by readers. Many of these stem from the fact that the Namespace example is fairly simple in that it does not implement all of the behavior that is possible in Explorer. This was done to focus on the core steps in getting a working Namespace implemented. Yet, there are a few extra steps you can take that don’t require too much more coding on your part to add more useful features. One question in particular that comes up quite often is how to enable deep searching in your Namespace.
You will notice from the existing Namespace example that if you enter a search term in the search box in Explorer, the search only filters items that are currently in the view. It does not search into the folders. In the below images, we try to search for “Two” in the search box which only results in 1 item. Thus, the sub folders were not included.
What does a Namespace implementer have to do in order to include sub folders in their namespace search results? This is actually fairly simple.
Implementing IShellFolderViewCB and IFolderViewSettings
In our previous code, we did not implement an IShellFolderViewCB for our Namespace. This allows your Namespace to be notified of events associated with the view. An implementation of IShellFolderViewCB can be specified in your call to SHCreateShellFolderView. This is optional and previously we were just passing NULL for this. We need to create a class that implements IShellFolderViewCB as well as IFolderViewSettings. For our IFolderViewSettings implementation, we only need to provide a handler for the GetFolderFlags method. It is through this method that we notify the Shell that we want to perform deep searches within our Namespace.
IFACEMETHODIMP CFolderViewCB::GetFolderFlags(__out FOLDERFLAGS *pfolderMask, __out FOLDERFLAGS *pfolderFlags) { if (pfolderMask) { *pfolderMask = FWF_USESEARCHFOLDER; }
if (pfolderFlags)
{
*pfolderFlags = FWF_USESEARCHFOLDER;
}
return S_OK;
}
As you can see from the above implementation of GetFolderFlags, we only care to notify the Shell of the FWF_USESEARCHFOLDER flag. This tells the Shell that our Namespace should use the Search Folder for performing stacking and searching. You could also specify other flags to modify the appearance and behavior of your namespace.
The modified code for this sample is linked below. You will notice that the implementation of IShellFolderViewCB and IFolderViewSettings is rather sparse – most methods just return E_NOTIMPL as we are not using them here. You can implement these yourself if you see the need to extend your code.
Now that we have notified the Shell to use the Search Folder, we can perform deep searches within our Namespace. When we perform the same search we did previously, we now get the following results:
This Namespace simply generates 10 virtual items to a default depth of 5. The Search enumerates the contents of the Namespace to that depth. It should also be called out that we had to implement our namespace's ParseDisplayName method in order for our namespace to function in the Search folder.
*Please note that the method described here only works with the default shell view (Defview). It is not supported for custom IShellView implementations.
Building the FolderView SDK Sample
- To build the FolderViewImpl sample, be sure to download and install the Windows SDK.
- Download the modified FolderView SDK sample
- Launch FolderViewImpl.sln in Visual Studio (The solution file is for Visual Studio 2008)
- Open the properties for the project
- Add a path to the SDK includes to the C/C++ - General page
- Add a path to the SDK libs to the Linker – General page
- Build
Installing the FolderView SDK Sample
- Once you have built the sample, copy the FolderViewImpl.dll and FolderViewImpl.propdesc to the same directory
- From an elevated cmd window, regsvr32 FolderViewImpl.dll
- Restart explorer
- Open explorer to Computer
- There should be a list item named “FolderView SDK Sample”
Written by chrdavis on March 3rd, 2008 with comments disabled.
Read more articles on API and Coding and Programming and Search and Search and Organize and Windows Vista and extension and namespace and shell and vista.