Win32 Shell API question

Hi everybody,

this is an absolutely tough question. Please answer only if you really know what
the Shell API is about. In fact, I will only give to grades on answers: A and F.

Now about the problem: It has to do with the Shell Name Space introduced with
Windows 95. Imagine I wanted to re-write the File-Open common dialog. I will
have to deal with ITEMIDLIST and IShellFolder and related stuff. But how?
Documentation is scarce.

For example, when I use IShellFolder::EnumObjects to query for subfolders of the
Desktop, I do not only get "My Computer", "Network Neighborhood" and "My Briefcase",
but also "Internet Explorer" and "Recycle Bin". Of course, in the File-Open common
dialog, the latter two don't appear. But how does Windows achieve this? I really
have a hard time finding out about all this. If I'd query for subfolders of
"My Computer", I'd probably get "Control Panel", "Printers", and so on. There are
quite a lot of different folder types in Win32. But I have absolutely no clue how
to distinguish between them.

Can you point out a good source of information on this? Or any other advise how
to achive what I outlined above?

Any ideas?
Who is Participating?
naveenkohliConnect With a Mentor Commented:
You are right that there can be more than one folders having the sane display name. But if you save the complete path, then it is guranteed that it will be unique through out the shell space. And thats the propertyof PIDL too that it guarntees the uniqueness of a folder ID..
Tommy HuiEngineerCommented:
The idea is that you have to use IShellFolder::EnumObjects() to enumerate through the items. Start with SHGetDesktopFolder() to retrieve the root folder. Then use EnumObjects() to iterate through the top level items. On top of this, you will need to use IShellFolder::GetAttributes() to check what attributes the current item has.

For example, on my computer, checking the attributes of the items give:

Recycle Bin FOLDER
My Briefcase FOLDER

So if you only want "Recycle Bin" and "My Briefcase", you can use SFGAO_FOLDER as your mask to GetAttributes() to get the right folders.

I think IShellFolder::GetAttributesOf can tell you that.

SFGAO_FILESYSTEM  The specified folders or file objects are part of the file system (that is, they are files, directories, or root directories).
SFGAO_FILESYSANCESTOR  The specified folders contain one or more file system folders.
SFGAO_FOLDER  The specified items are folders.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

The MSDN documentation may be a first stop.

The common explorer dialog probably uses EnumObjects in conjunction with GetAttributesOf.  The Flag SFGAO_FILESYSTEM
indicates that the object is part of the file system and not a special object.  Check out this function and see what you get back for Internet Explorer and the Recycle Bin, Control Panel, History Folder (IE), Fonts folder, etc.  There are other APIs that may be used but this might start you off.


HRESULT GetAttributesOf(
    UINT cidl,
    ULONG *rgfInOut

Retrieves the attributes of one or more file objects or subfolders.

Returns NOERROR if successful, or an OLE-defined error value otherwise.

    Number of file objects from which to retrieve attributes.


Address of an array of pointers to ITEMIDLIST structures, each of which uniquely identifies a file object relative to the parent folder. Each ITEMIDLIST structure must contain exactly one SHITEMID structure followed by a terminating zero.

Address of a single ULONG value that, on entry, contains the attributes that the caller is requesting. On exit, this value contains the requested attributes that are common to all of the specified objects. Note that this is the address of a single ULONG value, not an array of ULONG values. The lists below describe the possible flags for this parameter.

A file object's capability flags may be zero or a combination of the following values:

The specified file objects or folders can be copied (same value as the DROPEFFECT_COPY flag).

The specified file objects or folders can be deleted.

It is possible to create shortcuts for the specified file objects or folders (same value as the DROPEFFECT_LINK flag).

The specified file objects or folders can be moved (same value as the DROPEFFECT_MOVE flag).

The specified file objects or folders can be renamed.

Mask for the capability flags.

The specified file objects or folders are drop targets.

The specified file objects or folders have property sheets.

A file object's display attributes may be zero or a combination of the following values:

Mask for the display attributes.

The specified file objects or folders should be displayed using a ghosted icon.

The specified file objects are shortcuts.

The specified file objects or folders are read-only.

The specified folders are shared.

A file object's contents flags may be zero or a combination of the following values:

Mask for the contents attributes.

The specified folders have subfolders (and are, therefore, expandable in the left pane of Windows Explorer).

A file object's miscellaneous attributes may be zero or a combination of the following values:

The specified items can be browsed in place.

The specified items are compressed.

The specified folders or file objects are part of the file system (that is, they are files, directories, or root directories).

The specified folders contain one or more file system folders.

The specified items are folders.

The objects contain new content.

The items are nonenumerated items.

The specified file objects or folders are on removable media.

Validate cached information. The shell will validate that the objects specified in apidl still exist and will not used cached information when retrieving the attributes. If one or more of the items specified in apidl no longer exist, this method will return an error code. If cidl is zero, the shell will discard all cached information for the shell folder. This is similar to doing a refresh of the folder.

You can optimize this operation by not returning unspecified flags

Goto this URL, it has the answer to your question... complete implementation of some Shell API functins..

Using the Shell Namespace To Get Network Computers, Printers and Recycle Bin Items


And do goto the comments section, the code for getting control panel items has been posted in one of the comments :)

And if you will need updated version of the source code in that artice, let me know. The code for getting the icons for control pannel items has been added in last couple of days which needs to be updated on codeguru site.
First level of browsing ( "My Computer", "Network Neighborhood" and "My Briefcase" ) this you know.

To get subfolder use "BindToObject" method.

To get more information about each item Use "SHGetDataFromIdList" method.

This will work fine. For getting information from this method I think it will take more time then a Day.

Documentation is not correct for the "SHGetDataFromIdList" you have to get from Debugging.

bogie2Author Commented:
thui: thanks for your answer. However, I did not want to GET "Recycle Bin", but AVOID it.

naavenkohli: thanks for your answer. It got me started, at least. You deserve the points, I think. Just one more thing: can I save an ITEMID (in order to remember
the last path a user chose) Or must I remember the DisplayName and then
generate the ITEMID from it? If the latter's the case, isn't that somewhat
ambiguous since several items may have the same DisplayName?

wylliker: thanks, but I read the online docu and it didn't help (not in the first place, at least). Now, I know that GetAttributesOf() is the key function, but it was really
naavenkohli's code that got me started.

statishks: thanks for your comment. I will try the function you mentioned.
All others, thanks as well.
From DisplayName generating ITEMID will fail for SomeItem.
Ex : Network etc..
All Courses

From novice to tech pro — start learning today.