Click here to Skip to main content
Click here to Skip to main content

ACF Does Shell – Part 1

, 26 May 2004
Rate this:
Please Sign up or sign in to vote.
This article introduces a tiny shell library built on ACF which makes shell programming a joy.

Introduction

Shell programming is one of the challenging tasks in Windows programming. A developer has to deal with mystery PIDLs, IShellFolder and some shell functions; developing and debugging can be very difficult. This article shows a tiny shell library built on ACF (Another C++ Framework) which makes shell programming a joy.

If you are not familiar with ACF, please visit ACF homepage (SourceForge), or check another article on CodeProject.

Getting Started

The first thing is to build the WinShell library and setup the include and library directories in your development environment. Since this process is similar to other ACF libraries (e.g., Corlib), I’m skipping it here.

The first example is to display items in the desktop folder. You can now recall how to do this using PIDLs and IShellFolder.

Following is the code using ACF and the WinShell library, I believe which is much simpler than you thought:

#define _WIN32_DCOM
#include <objbase.h>

#include <AcfCorlib.h>
#include <AcfWinShell.h>
using namespace Acf;
using namespace Acf::Windows::Shell;

int main() {
    ::CoInitializeEx(null, COINIT_APARTMENTTHREADED);

    ItemPtr desktop = Item::get_Desktop();

    FOREACH (ItemPtr, item, desktop) {
        Console::WriteLine(item->DisplayName);
    }

    ::CoUninitialize();
}

The Item class represents an object in the Shell namespace (can be folder or non-folder). It contains properties and methods which support navigating the Shell namespace and retrieving information of an item. Since it implements IEnumerable<T>, you can use FOREACH on it.

Now, let’s see another example. This example shows how to get sub-folders of My Computer and then sort them. Did you ever believe that this can be done only in several lines of code? Smile | :)

ItemPtr folder = new Item(SpecialFolder::MyComputer);
RefPtr<IEnumerable<ItemPtr> > c = folder->Folders;

RefPtr<List<ItemPtr> > list = new List<ItemPtr>(c);
list->Sort();

As you can see, concepts and models used here are just as other parts of ACF. Developers should feel easy and comfortable.

Sine the Item class is the one that does the magic, let’s take a look at it in detail:

Class Item

Represents an object in the Shell namespace.

class Item : public Object, public IEnumerable<ItemPtr>

Namespace: Acf::Windows::Shell.

Contructors

  • Item(SpecialFolder s);

    Initializes a new instance using one of the SpecialFolder enumerations (E.g., SpecialFolder::MyComputer).

  • Item(String* path);

    Initializes a new instance for the specified file.

Properties

  • static ItemPtr get_Desktop();

    Gets the desktop Item.

  • ItemPtr Parent;

    Gets the current object's parent.

  • StringPtr DisplayName;

    Gets the object’s display name. E.g., “My Computer”.

  • StringPtr ParsingName;

    Gets the parsing name of the object relative to the desktop. E.g., “C:\\Windows”.

  • StringPtr RelativePath;

    Gets the object's name relative to the parent folder.

  • bool IsDesktop;

    Gets a value that indicates whether this object is the desktop.

  • bool IsFolder;

    Gets a value indicating whether the object is a folder.

  • bool IsFileSystem;

    Gets a value indicating whether the object is part of the file system.

  • StringPtr FileSysPath;

    Gets the full path of the object. If the object is not part of the file system, an exception is thrown.

  • RefPtr<IEnumerable<ItemPtr> > Folders;

    Gets the child folders of the object. If this object is not a folder, an exception is thrown.

  • RefPtr<IEnumerable<ItemPtr> > NonFolders;

    Gets the child non-folders of the object. If this object is not a folder, an exception is thrown.

  • const ITEMIDLIST* Pidl;

    Gets the relative PIDL of the object.

  • const ITEMIDLIST* FullPidl;

    Gets the full PIDL of the object.

  • int ImageIndex;

    Gets the system image list index of the object.

Methods

  • static int Compare(Item* a, Item* b);

    Compares the two items. If the two items are not in the same folder, an exception is thrown.

  • virtual RefPtr<IEnumerator<ItemPtr> > GetEnumerator();

    Returns an enumerator.

  • RefPtr<IEnumerable<ItemPtr> > GetChildItems(ChildItemType typeFlags);

    Returns the children of the specified type. If this object is not a folder, an exception is thrown.

  • RefPtr<IEnumerable<ItemPtr> > GetChildItems(HWND hwndOwner, ChildItemType typeFlags);

    Returns the children of the specified type. If this object is not a folder, an exception is thrown. The window handle is used to show dialog box or message box (for example, when enumerating on a CD-ROM that has no disk, the system will prompt the user to insert a disk).

  • RefPtr<IEnumerable<ItemPtr> > GetFolders(HWND hwndOwner);

    Returns the child folders of the object. If this object is not a folder, an exception is thrown.

  • RefPtr<IEnumerable<ItemPtr> > GetNonFolders(HWND hwndOwner);

    Returns the child non-folders of the object. If this object is not a folder, an exception is thrown.

  • ItemPtr GetChild(String* relativeParsingName);

    Returns the child with the specified relative parsing name. If this object is not a folder, an exception is thrown.

  • ItemPtr GetChild(HWND hWnd, String* relativeParsingName);

    Returns the child with the specified relative parsing name. If this object is not a folder, an exception is thrown.

  • ItemAttributes GetAttributes(ItemAttributes mask);

    Returns the attributes of the object. The mask parameter specifies which bits to return.

About the Demo

The demo is an Explorer-like MFC dialog application which shows how to use the shell library presented in this article. It’s quite simple and silly, but it could be a good starting point for you to learn and use the library.

Reference

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Yingle Jia
Web Developer
China China
Yingle Jia is a software engineer located in Beijing, China. He currently works at IBM CSDL (China Software Development Lab). His interests include C++/COM/C#/.NET/XML, etc. He likes coding and writing.
 
He is the creator of ACF (Another C++ Framework) project. See http://acfproj.sourceforge.net/.
 
He also has a blog at http://blogs.wwwcoder.com/yljia/

Comments and Discussions

 
GeneralBuild Error Pinmembertrangbaria12-Oct-06 16:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 27 May 2004
Article Copyright 2004 by Yingle Jia
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid