Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

Windows 7: Consuming the Contents of Windows 7 Libraries

23 Apr 2009   #1

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(
IShellFolder *psf;
hr = psi->BindToHandler(NULL, BHID_SFObject, IID_PPV_ARGS(&psf));
IEnumIDList *penumIDList;
psf->EnumObjects(NULL, SHCONTF_FOLDERS, IID_PPV_ARGS(&penumIDList));
//use penumIDList to enumarte 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 Picture Library. 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. 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 RC SDK.

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(
IShellItemArray *psiaFolders;
hr = pslLibrary->GetFolders(
IEnumShellItems *penumShellItems;
//work with penumShellItem to enumerate the items in the library

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 later is used to obtain an enumerator to traverse through the entire library contents.

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


My System SpecsSystem Spec
24 Apr 2009   #2

Windows 7 x64 / Same

Can I access them like regular folders in .NET? (in the System.IO directive)
My System SpecsSystem Spec

 Consuming the Contents of Windows 7 Libraries

Thread Tools

Similar help and support threads
Thread Forum
Is there a less time consuming way to do this?
What I'm doing is making room on what is now an external hard drive (used to be C drive on my old computer). I've already transferred 11 gig of download files I want to keep, to a flash drive. It took 5 hours to do it. The point here is, make room for a backup of the Win 7 computer on the external...
General Discussion
Windows 7 OS consuming 100GB space ,how????
Thank's for stopping by, I was wondering how just the windows 7 operating system alone takes nearly 100GB? :cry: I am trying to use my c drive just for os keeping all other stuff in other partition-assuming this will speed up system!!! (HDD reading speed,my windows experience index lacks(5.9)...
Performance & Maintenance
svchost.exe consuming 100% ram
I have a laptop with a 4gb RAM.. suddenly the laptop got slow that i was'nt able to move my cursor...i saw the processes list and found that 'svchost.exe' was using exorbitantly high amout of memory...i cannot post the screenshot of the task my system has become unresponsive......
Performance & Maintenance
The consuming of RAM
My notebook with 6GB RAM & core i5 CPU has run mad recently. While my notebook was idle, there're somethings consuming it's RAM (about 1 - 1.8 GB). I didn't downloaded or ran any program, then why? Why my RAM has go crazy when it's idle? And how can I fix this? Anyone please help me..! And Sorry...
Performance & Maintenance
contents of C\windows switched with contents of c\program files
I am not a 100% on how I managed this but here were my last steps: - changed the host file in c\windows\system32\drivers\etc - rebooted computer - received system errors, so I ran a HDD defragmenter - I then tried to access a file in c\program files but could not find it, but it mysteriously...
General Discussion

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd

All times are GMT -5. The time now is 19:10.
Twitter Facebook Google+