Consuming the Contents of Windows 7 Libraries

This is the fifth post about Windows 7 Libraries. By now, you should be familiar with Windows 7 Libraries (Understanding Windows 7 Libraries) and understand the internal structure of Libraries and how they integrate into the Windows Shell (Libraries Under the Hood). In the previous post, we explained the different programming models and ways to work with Windows 7 Libraries. In this post, we focus on the Windows Shell Programming model that you can use to access libraries even without using the latest IShellLibrray APIs.

In the previous post, we talked about the importance of using the right version of the Common File Dialog (CFD) to enable the complete Libraries user experience in Windows 7. In this post we will further explorer the opportunities to let users select and consume Libraries as if they where regular folders. Let’s imagine the case of a slideshow application that presents users' pictures. By using the Pictures Library, users are essentially telling the system that their important pictures are stored in the Pictures Library, and therefore, our slideshow application can simply be pointed directly to the Pictures Library and show the users' entire pictures collections. Furthermore, from developers’ point of view, using the Libraries system can eliminate the need to maintain a separate configuration file or database, since developers can rely on the Libraries System. But before we dive into the Shell Libraries programming APIs, we need to understand the few concepts regarding the Shell Programming Model.

Shell Programming Model

  • IShellItem, commonly referred to as an item, is the currency of the Shell UI model and its programming model. Items are individual, self-contained content sources. Take for example the above-mentioned Common File Dialogs. Quite few of the interface methods used for controlling the file dialogs use Shell items to refer to folders instead of file system paths. This is important because the CFD can communicate information about both file system folders and other virtual folders that you find in the shell, such as the Control Panel or the Computer folder.
  • IShellFolder interface represents shell folder objects from the shell namespace. Using IShellFolder you can traverse through the contents of a folder, to retrieve the display name of an item in a folder, parse a display name relative to the folder, and obtain an item ID list.
  • IShellLink interface represents a link, usually to a file, folder, or an executable.
  • IPropertyStore interface can be used to manage the property store associated with various shell objects.
  • IShellLibrary interface represents a Windows 7 Library, which we will cover in depth in future posts.

Now that we have defined the different players in the Shell Programming Model, we can see how Libraries fit into this model. Since Libraries are not file system locations, you cannot use file system-specific APIs. Therefore, you have two main options to consume library contents - the Shell Programming Model and the new IShellLibrary API.

Using the Shell Programming Model

You can use the IShellItem and IShellFolder interfaces and a bunch of helper functions to enumerate the contents of Libraries just as if they were regular folders. This means that applications can consume Libraries contents without using the new Libraries API and with very little change to their existing code.

The following code snippet shows how to use the IShellFolder interface to enumerate through the entire contents of the Picture library.

IShellItem *psi;
HRESULT hr = SHGetKnownFolderItem(
FOLDERID_PicturesLibrary,
KF_FLAG_CREATE,
NULL,
IID_PPV_ARGS(&psi));
if(SUCCEEDED(hr))
{
IShellFolder *psf;
hr = psi->BindToHandler(NULL, BHID_SFObject, IID_PPV_ARGS(&psf));
if(SUCCEEDED(hr))
{
IEnumIDList *penumIDList;
psf->EnumObjects(NULL,
SHCONTF_FOLDERS | SHCONTF_NONFOLDERS,
IID_PPV_ARGS(&penumIDList));
//use penumIDList to enumerate the content of the folder
}
}

Here you can see that by using the helper function SHGetKnownFolderItem, we can retrieve the correct library location by passing the FOLDERID_PicturesLibrary, which is a GUID representing the known folder, in our case the Pictures Library.The SHGetKnownFolderItem is a Shell Helper function and is part of a larger group of helper functions that can be found in the shlobj.h header file in the Windows 7 SDK. A successful call will fill the IShellItem *psi with the correct information about the Library represented as a Shell Item. From this point the rest of the code is standard Windows Shell programming, where we use the BindToHandler to bind the previously obtained Shell Item to a Shell folder. Next, we enumerate through the different items in the Shell folder, which in case of a library can be either files or folders. Note the SHCONTF_FOLDERS | SHCONTF_NONFOLDERS flags that we are passing, this is telling the shell folder that we want to return all of the files and folders in a library. We could pass SHCONTF_NAVIGATION_ENUM to get the library locations instead of the library

Using the New IShellLibrary API

You can achieve the same functionality as the code above by using the new Windows 7 IShellLibrary API as shown by the next code snippet.

IShellLibrary *pslLibrary;
HRESULT hr = SHLoadLibraryFromKnownFolder(
FOLDERID_PicturesLibrary,
STGM_READ,
IID_PPV_ARGS(&pslLibrary));
if(SUCCEEDED(hr))
{
IShellItemArray *psiaFolders;
hr = pslLibrary->GetFolders(
LFF_STORAGEITEMS,
IID_PPV_ARGS(&psiaFolders));
IEnumShellItems *penumShellItems;
psiaFolders->EnumItems(&penumShellItems);
//work with penumShellItem to enumerate the library locations.
}

Here you can see that we used another helper function SHLoadLibraryFromKnownFolder to create the IShellLibrary object from which we can call the GetFolders method to return an IShellItemArray that contains the library locations. This is the same as using SHCONTF_NAVIGATION_ENUM in the above Shell programming model example. You can directly enumerate the contents of each of the library locations using the shell programming model or get the path of each of the locations.

In the next post we will focus more on the rest of the IShellLibrary API.


Comments

  1. Posted on: April 23, 2009 at 9:36PM  

    I finally got my hands on Windows 7 and tested how Libraries work with legacy software.  After all, we will be using legacy software if/when our office upgrades to Windows 7.

    My first test was with a program I wrote 5 years ago.  The file dialog box contained the 'Libraries' folder and I was able to choose a file and the path returned to my program was the physical path of the file that was selected.  This works fine, but my program was telling the user the file in use was in a different path to one that was selected.  

    My second test was with Word 2003 and the file dialog box did not  contain 'Libraries' as an option on the left hand side panel but does show as an option in the drop down box at the top of the dialog box.  When I select the 'Libraries' folder I cannot open or save files as I get an error message.

    So it looks like we have more inconsistencies introduced into  the Windows system and this means in the future I will have to train users on what a Library is and how it works and what issues to look out for etc.  And as a programmer too, it looks like I have extra programming work.  So I'm wondering why MS didn't come up with an easy way for users to create  and manage symbolic links?  Symbolic links works at a file system level and therefore would work fine with legacy software.  

  2. Posted on: April 24, 2009 at 11:55AM  

    Tommyinoz,

    Great question. As you've discovered, libraries have been designed to be compatible. Opening and saving files to and from libraries with the common file dialog is transparent to applications.

    Also, we've made libraries not look like file system locations to applications. There are many non-file system locations in the system; for instance, control panel, computer and desktop are all "abstract" locations without actual file system paths. Many applications can already work with non-file system locations like libraries and most older applications exclude these non-file system locations entirely from their experiences.

    Your Word 2003 example, is a great example of why applications should use the common file dialog. It is using a custom file dialog that was explicitly designed for Windows XP and earlier. The file dialog still works with files and folders bit it doesn’t have all of the latest improvements in the Windows Explorer since Windows XP like search, improved navigation and libraries. As the user experience evolves users expect their applications to evolve with it. The common file dialog is the best way to make that happen. As you may have noticed, Office 2007 has been updated to use the common file dialog.

    If you want to learn more about the space I recommend that you watch my PDC talk on the Windows 7 Explorer: channel9.msdn.com/.../PC16

    Thanks much for your feedback,

    David Washington

  3. Posted on: April 26, 2009 at 1:42AM  

    Will you please reintroduce IColumnProvider so we can get back the folder size column back in Explorer? It's unbelievable something which was possible in Windows XP isn't possible in Windows 7. Why was this deprecated in the first place? Was it unsecure code? The Folder size shell extensions was very very useful. The Property system isn't a replacement its developer has been saying (social.msdn.microsoft.com/.../2056b237-574d-483c-8ecd-f2842dd70081) because it is about static metadata that can be indexed by the new search system, is there a way to display DYNAMIC INFORMATION? Without the Folder Size shell extension working, I doubt I will ever migrate to Windows 7.

  4. Posted on: October 17, 2009 at 7:11PM  

    windows 7 news for the consumer to open up the library thank you for this operating system had to be such a thing in your article was beautiful thank

Trackbacks

  1. Posted by: Tech News, Resources from Blogosphere - 24 April 09(7) | Best Webhosting on April 23, 2009 at 10:47PM

    Pingback from  Tech News, Resources from Blogosphere - 24 April 09(7) | Best Webhosting

  2. Posted by: Consuming the Contents of Windows 7 Libraries|Join Our Story!|AngNetwork Blog on April 24, 2009 at 4:30AM

    Pingback from  Consuming the Contents of Windows 7 Libraries|Join Our Story!|AngNetwork Blog

  3. Posted by: Consuming contents of Windows 7 Libraries on April 24, 2009 at 5:16AM

    Pingback from  Consuming contents of Windows 7 Libraries

  4. Posted by: Consuming the Contents of Windows 7 Libraries - Windows 7 for … | Current Technology Updates daily on April 24, 2009 at 7:35AM

    Pingback from  Consuming the Contents of Windows 7 Libraries - Windows 7 for … | Current Technology Updates daily

  5. Posted by: Dew Drop - April 24, 2009 | Alvin Ashcraft's Morning Dew on April 24, 2009 at 8:19AM

    Pingback from  Dew Drop - April 24, 2009 | Alvin Ashcraft's Morning Dew

  6. Posted by: Consuming the Contents of Windows 7 Libraries - Windows 7 for Developers - The Windows Blog | WinSe7en on April 24, 2009 at 10:23AM

    Pingback from  Consuming the Contents of Windows 7 Libraries - Windows 7 for Developers - The Windows Blog | WinSe7en

  7. Posted by: linkfeedr » Blog Archive » Consuming the Contents of Windows 7 Libraries - RSS Indexer (beta) on April 24, 2009 at 7:11PM

    Pingback from  linkfeedr » Blog Archive » Consuming the Contents of Windows 7 Libraries - RSS Indexer (beta)

  8. Posted by: Consuming the Contents of Windows 7 Libraries - Windows 7 for … | DONG'S BLOG on April 25, 2009 at 1:32AM

    Pingback from  Consuming the Contents of Windows 7 Libraries - Windows 7 for … | DONG'S BLOG

  9. Posted by: Windows 7 for Developers on May 09, 2009 at 2:38PM

    After two weeks break, we are back to our normal technical posts, covering Windows 7 Libraries. This

  10. Posted by: Stay in Sync with Windows 7 Libraries | Windows Seven 7 on May 15, 2009 at 12:53AM

    Pingback from  Stay in Sync with Windows 7 Libraries | Windows Seven 7

  11. Posted by: Windows 7 for Developers on May 18, 2009 at 11:42PM

    Here is another post in the Windows 7 Libraries post series. So far, we have covered what Windows 7 Libraries

  12. Posted by: You Get The . Info » Stay in Sync with Windows 7 Libraries - 193th Edition on May 19, 2009 at 8:38PM

    Pingback from  You Get The . Info » Stay in Sync with Windows 7 Libraries - 193th Edition

  13. Posted by: Consuming the Contents of Windows 7 Libraries - By Egidijus Baranauskas - Post - Windows, Technology, Computers, Software News - WinSoftNews on May 21, 2009 at 4:19AM

    Pingback from  Consuming the Contents of Windows 7 Libraries - By Egidijus Baranauskas - Post - Windows, Technology, Computers, Software News - WinSoftNews

  14. Posted by: Windows 7 for Developers on June 09, 2009 at 4:54PM

    We are wrapping up our comprehensive drilldown into the Windows 7 Libraries API and architecture with

  15. Posted by: Windows 7 for Developers on June 09, 2009 at 5:00PM

    We are wrapping up our comprehensive drilldown into the Windows 7 Libraries API and architecture with

  16. Posted by: Windows 7 for Developers on June 09, 2009 at 5:01PM

    We are wrapping up our comprehensive drilldown into the Windows 7 Libraries API and architecture with

  17. Posted by: Windows 7 Libraries ??? Managed Code | Windows Seven 7 on June 09, 2009 at 5:56PM

    Pingback from  Windows 7 Libraries ??? Managed Code | Windows Seven 7

  18. Posted by: Windows 7 Programming Guide ??? Libraries « Agave Anejo's Blog on June 21, 2009 at 10:59AM

    Pingback from  Windows 7 Programming Guide ??? Libraries « Agave Anejo's Blog