Click here to Skip to main content
13,408,530 members (42,679 online)
Click here to Skip to main content
Add your own
alternative version


53 bookmarked
Posted 27 Feb 2007

Host Windows Explorer in your applications using the new Vista hosting APIs

, 27 Feb 2007
Rate this:
Please Sign up or sign in to vote.
This article describes in detail the new IExplorerBrowser interface and the Explorer Browser object available in Windows Vista. The Explorer Browser object allows developers to host Windows Explorer in their applications.

Screenshot - ExplorerBrowser1.jpg


A neat but barely documented feature of Windows Vista is the API support to host a Windows Explorer window. The image above shows the screenshot of the sample MFC application hosting the Explorer window. The COM based API is quite simple, and it allows a considerable control over the Windows Explorer window, in the following ways:

  1. Ability to control the different view types: Large Icon, Small Icon, Tiles, Details, Thumbnails etc.
  2. Automatic TravelLog: The user's browser history is maintained by the hosted Explorer window, and the hosting application can control the forward/back/up navigation.
  3. Ability to handle the Explorer window navigation events and take appropriate actions to control the navigation from within the hosted application.
  4. Ability to display navigational frames like that in the Explorer.

This feature is extremely useful in applications such as Installer IDEs, CD Burners, or any other application which shows a view of the Shell or file system and allows users to drag and drop items from the Shell view to the application window. Another use of this feature may be in a Visual Studio add-in that can display a file system view within the IDE. There might be more uses in your custom application with the only (big) drawback being that this feature is only available in Windows Vista.

Preparing the Build Environment

To start, you need to install the Microsoft Windows Vista SDK. The installation is available as a web install or a DVD image. The web install is a small exe which you run to start the installation, and the exe downloads the files from internet on an as needed basis. The web install normally takes around 2-3 hours to complete. However, if you want to install the SDK on multiple machines, or reinstall on the same machine at a later time, you should download the DVD image of the installation. You can download the SDK from this link.

Once you have downloaded and installed the SDK, you need to integrate the SDK with VS2005. The SDK ships with new C++ header and library files that contain the definition of the new APIs available in Windows Vista. Thus, you need to set the path of your VS2005 header and library files to point to the installation directory of VS2005. The easiest way to do that is to launch a bat file that comes with the Windows SDK installation. This can be launched from the Start menu, through a shortcut as shown below:

Launching the integration tool

Once you have run this tool, you can verify that the path of the include, header, and executable file of VS2005 has changed, by launching the Tools -> Options dialog:

VS 2005 Tools Options

An important thing to note here is that the executable files directory, include directory, and the library files directory should be modified. The Windows SDK ships with newer versions of tools such as the MIDL compiler, which are needed to build applications for Windows Vista, and so that path of executable files should also be set correctly.

Once you have verified that the build environment is set up properly, you can start developing for Windows Vista. To get all the Vista APIs, you need to define the macros WINVER, WIN32_WINNT, and WIN32_IE appropriately:

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
#define _WIN32_IE 0x0700    

Now, you can start using the Windows Vista APIs in your application. Let's move next to see how the Explorer can be hosted in an MFC application.

Hosting the Explorer Window in the View

In this sample application, we will host the Explorer window in a CView derived class. There is no restriction on what window the Explorer can be hosted on. For example, it can even be hosted in a dialog. I found it the hard way that the view should have the WS_CLIPSIBLINGS and WS_CLIPCHILDREN Windows styles set, otherwise the hosted Explorer has some painting issues. This can be done in the PreCreateWindow function.

BOOL CExplorerBrowserView::PreCreateWindow(CREATESTRUCT& cs)
    if (!CView::PreCreateWindow(cs))

    return TRUE;

The actual hosting API is a COM API, and the interface which we need to use is the IExplorerBrowser interface. A pointer to this interface should be a member of the CView derived class, and it should have the same lifetime as the CView derived class. In the sample application, a smart pointer to the IExplorerBrowser interface is made a member of the CExplorerBrowserView class.

class CExplorerBrowserView : public CView
    CComPtr<IExplorerBrowser> m_spExplorerBrowser;

Next, we need to create the actual Explorer window as a child of the view window. This can be done in the OnCreate method which gets called when the window is created. Also, we can handle any error that may occur when hosting the browser. First, we need to instantiate the ExplorerBrowser COM class which has the CLSID of CLSID_ExplorerBrowser. Once the object is instantiated successfully, a call to the Initialize method creates the Explorer window as a child of the view window. The following is the code to create the Explorer window:

int CExplorerBrowserView::OnCreate(LPCREATESTRUCT lpCreateStruct)
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;

    HRESULT hr = SHCoCreateInstance(NULL, &CLSID_ExplorerBrowser, NULL, 

    if (SUCCEEDED(hr))
        if (theApp.ShowFrames())
        fs.fFlags = 0;
        fs.ViewMode = FVM_DETAILS;
        hr = m_spExplorerBrowser->Initialize(m_hWnd, CRect(0, 0, 0, 0), &fs);

    return SUCCEEDED(hr) ? 0 : -1;

Notice the call to the SetOptions method before calling the Initialize method. The SetOptions allows you to specify more options on how the display should look like. For example, you can include frames when displaying the Explorer window. The frames consist of the main details view, the places bar, the folders tree, and the details pane, as shown in the screenshot below:

Framed Explorer Browser Views

The default is to not show the frames, and the view looks like the following:

NormalExplorer Browser Views

The call to SetOptions(EBO_SHOWFRAMES) displays the frames. Once the Initialize method is called, it is not possible to turn on or turn off the frames. If you intend to show frames dynamically, you have to destroy the Explorer browser and call the Initialize method again.

Cleaning Up After the View is Destroyed

When the view is destroyed, the Explorer browser object should be cleaned up. Note that merely releasing the COM interface pointer is not sufficient. You have to call IExplorerBrowser::Destroy in the OnDestroy method as shown below:

void CExplorerBrowserView::OnDestroy()

Sizing the Explorer Browser Window

If the view consists of entirely the Explorer browser window, it needs to be sized to fit the view. The Explorer Browser object provides the SetRect method that can be used to resize the window. The code to resize is placed in the OnSize method of the view object, and is shown below:

void CExplorerBrowserView::OnSize(UINT nType, int cx, int cy)
    CView::OnSize(nType, cx, cy);

    m_spExplorerBrowser->SetRect(NULL, CRect(0, 0, cx, cy));

So far, we have learnt how to create the Explorer browser window, size it, and destroy it. But it still does not do anything interesting. Now in the next section, we will see how to navigate using the Explorer browser.

Navigating Using the Explorer Browser

The hosted Explorer can be navigated to any Shell folder which includes file system folders and non-file system folders like Network, Printers, and Control Panel. Because the Windows Shell items are not limited to just the file system, items in the Shell are not identified by the path, rather they are identified by item ID lists (PIDL). (The Item ID List structure is described in more detail in the Shell documentation available on MSDN.) The IExplorerBrowser provides a function named BrowseToIDList which accepts the item ID list of a Shell folder to which the browser navigates. To navigate to a file system path, we have to convert the file system path to an item ID list. The following code shows how the Explorer browser can be navigated to a folder specified by a file path.

bool CExplorerBrowserView::NavigateTo(LPCTSTR szPath)
    HRESULT hr = S_OK;
    LPITEMIDLIST pidlBrowse;

    if (FAILED(hr = SHParseDisplayName(szPath, NULL, &pidlBrowse, 0, NULL)))
        ATLTRACE("SHParseDisplayName Failed! hr = %d\n", hr);
        return false;

    if (FAILED(hr = m_spExplorerBrowser->BrowseToIDList(pidlBrowse, 0)))
        ATLTRACE("BrowseToIDList Failed! hr = %d\n", hr);


    return SUCCEEDED(hr);

The NavigateTo function accepts the path of a folder. It first converts the folder to PIDL using the SHParseDisplayName function, and finally it calls the BrowseToIDList function to make the Explorer browser navigate to that folder. Finally, it frees the allocated PIDL by calling ILFree.

The Explorer browser also maintains a travel log of all the folders visited. It is possible to programmatically navigate back or forward. This can be done using the BrowseToIDList function as shown below:

void CExplorerBrowserView::OnViewBack()
    m_spExplorerBrowser->BrowseToIDList(NULL, SBSP_NAVIGATEBACK);

void CExplorerBrowserView::OnViewForward()
    m_spExplorerBrowser->BrowseToIDList(NULL, SBSP_NAVIGATEFORWARD);

Listening to Navigation Events

In addition to programmatic navigation, the Explorer browser also provides means to listen to navigation events which occur when the user navigates using the Explorer user interface (e.g., by double clicking on a folder). This can be done by supplying the Explorer browser object with an object implementing the IExplorerBrowserEvents. The CExplorerBrowserEvents class in the sample implements the IExplorerBrowserEvents and delegates to the main view class. Here is the code for CExplorerBrowserEvents:

class CExplorerBrowserEvents :
    public CComObjectRootEx<CComSingleThreadModel>,
    public IExplorerBrowserEvents
    CExplorerBrowserView* m_pView;


    void SetView(CExplorerBrowserView* pView)
        m_pView = pView;

    STDMETHOD(OnNavigationPending)(PCIDLIST_ABSOLUTE pidlFolder)
        return m_pView ? m_pView->OnNavigationPending(pidlFolder) : E_FAIL;

    STDMETHOD(OnViewCreated)(IShellView *psv)
        if (m_pView)
        return S_OK;

    STDMETHOD(OnNavigationComplete)(PCIDLIST_ABSOLUTE pidlFolder)
        if (m_pView)
        return S_OK;

    STDMETHOD(OnNavigationFailed)(PCIDLIST_ABSOLUTE pidlFolder)
        if (m_pView)
        return S_OK;

All four events fired by the Explorer browser and handled in the CExplorerBrowserEvents class are delegated back to the hosting CExplorerBrowserView instance. The view itself declares the event handling methods as virtual so that they can be overridden by the derived class.

class CExplorerBrowserView : public CView
    virtual HRESULT OnNavigationPending(PCIDLIST_ABSOLUTE pidlFolder);
    virtual void OnNavigationComplete(PCIDLIST_ABSOLUTE pidlFolder);
    virtual void OnViewCreated(IShellView *psv);
    virtual void OnNavigationFailed(PCIDLIST_ABSOLUTE pidlFolder);

Let's take a look at these event methods in a little more detail. Three of these methods take the PIDL of the folder as parameter.

  • The OnNavigationPending method is called when the navigation is initiated by either the user or programmatically. The actual navigation process can be canceled by returning a failure HRESULT from the method.
  • The OnNavigationComplete method is called when the navigation has completed; this method is the right place to update the UI so that it is in sync with the navigated folder.
  • The OnViewCreated method is called just after the navigation pending method. The Explorer destroys and recreates the list view window each time it navigates to a folder. This method takes a pointer to the IShellView interface which provides additional methods to query and control the view.
  • Finally, the OnNavigationFailed method is called if the folder navigation fails for some reason.

Once we have implemented the IExplorerBrowserEvents, we need to create an instance of the implementing class, CExplorerBrowserEvents in this case, and supply it to the Explorer browser object. This is done by calling the IExplorerBrowser::Advise method. This method should be called before navigating the browser to a particular folder. In the case of CExploreBrowserView, this function is called in the OnCreate function just after the call to Initialize.

int CExplorerBrowserView::OnCreate(LPCREATESTRUCT lpCreateStruct) {
    CComObject<CExplorerBrowserEvents>* pExplorerEvents;
    if (SUCCEEDED(CComObject<CExplorerBrowserEvents>::
        m_spExplorerBrowser->Advise(pExplorerEvents, &m_dwAdviseCookie);

The call to the Advise method ensures that the CExplorerBrowserEvents class receives a notification for each of the events. A matching call to the Unadvise methods stops the Explorer browser from notifying events to the object. The Advise method returns a DWORD cookie which can be supplied to the Unadvise method. The call to Unadvise is in the OnDestroy method of CView:

void CExplorerBrowserView::OnDestroy()


Obtaining the List of Selected Items

In some applications, it might be necessary to obtain the selected items in the Explorer browser and take some action on them. To obtain the selected items is a two step process:

  1. Obtain an interface pointer to the IShellView interface by calling the GetCurrenView method.
    CComPtr<IShellView> spSV;
    if (SUCCEEDED(m_spExplorerBrowser->GetCurrentView(IID_PPV_ARGS(&spSV))))
  2. The IShellView pointer thus obtained can be used to get the selected items as an OLE DataObject.
    CComPtr<IDataObject> spDataObject;
  3. Finally, the data object can be used to get the selected items as shown below:
  4. [__strong__]//Code adapted from
                      -1, TYMED_HGLOBAL };
    STGMEDIUM stg;
    stg.tymed =  TYMED_HGLOBAL;
    if (SUCCEEDED(spDataObject->GetData(&fmt, &stg)))
        HDROP hDrop = (HDROP) GlobalLock ( stg.hGlobal );
        UINT uNumFiles = DragQueryFile ( hDrop, 0xFFFFFFFF, NULL, 0 );
        HRESULT hr = S_OK;
        for(UINT i = 0; i < uNumFiles; i++)
            TCHAR szPath[_MAX_PATH];
            szPath[0] = 0;
            DragQueryFile(hDrop, i, szPath, MAX_PATH);
            if (szPath[0] != 0)
        GlobalUnlock ( stg.hGlobal );
        ReleaseStgMedium ( &stg );

    The above code adds the path of the selected items to a CStringArray object.

The CExploreBrowserView class provides a method called GetSelectedItems which accepts a CStringArray reference; on return, the string array is filled with the path of each of the selected items.

Conclusion and Future Work

Windows Vista has lot of such nice goodies in the Shell, but unfortunately, they are not very well documented. I hope that this article and the sample will help someone to use the Explorer browser in their own applications. I selected MFC for this article because it was easy to develop the sample using MFC, but I am working on porting the application to .NET and C#. So stay tuned for future updates.


  • 2/27/2006 - Initially posted.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rama Krishna Vavilala
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralDrag and drop on IExplorerBrowserControl Pin
douglasjordan3-Feb-08 10:10
memberdouglasjordan3-Feb-08 10:10 
GeneralRe: Drag and drop on IExplorerBrowserControl Pin
douglasjordan11-Feb-08 9:51
memberdouglasjordan11-Feb-08 9:51 
QuestionHow to programmatically close left task panel in XP? Pin
nanfang2-Jan-08 7:34
membernanfang2-Jan-08 7:34 
GeneralThis works from windows 98 onwards Pin
Nice Life26-Mar-07 0:08
memberNice Life26-Mar-07 0:08 
GeneralRe: This works from windows 98 onwards Pin
Rama Krishna Vavilala26-Mar-07 1:41
mvpRama Krishna Vavilala26-Mar-07 1:41 
GeneralRe: This works from windows 98 onwards Pin
Nice Life26-Mar-07 4:44
memberNice Life26-Mar-07 4:44 
GeneralPerfect, but... Pin
YoSilver16-Mar-07 14:11
memberYoSilver16-Mar-07 14:11 
GeneralExcellent work Pin
Dr.Luiji13-Mar-07 14:07
memberDr.Luiji13-Mar-07 14:07 
Generala few suggestions Pin
chrisg12-Mar-07 6:26
memberchrisg12-Mar-07 6:26 
GeneralRe: a few suggestions Pin
Rama Krishna Vavilala12-Mar-07 6:36
mvpRama Krishna Vavilala12-Mar-07 6:36 
AnswerRe: a few suggestions Pin
Gnarlhead927-Feb-08 14:28
memberGnarlhead927-Feb-08 14:28 
GeneralDamn cool Pin
Quartz...9-Mar-07 7:08
memberQuartz...9-Mar-07 7:08 
GeneralRe: Damn cool Pin
mou1239-Mar-07 19:39
membermou1239-Mar-07 19:39 
QuestionXP? Pin
axelriet5-Mar-07 1:17
memberaxelriet5-Mar-07 1:17 
AnswerRe: XP? Pin
Rama Krishna Vavilala5-Mar-07 3:33
mvpRama Krishna Vavilala5-Mar-07 3:33 
AnswerRe: XP? Pin
chrisg12-Mar-07 6:32
memberchrisg12-Mar-07 6:32 
GeneralSay... Pin
Shog91-Mar-07 12:48
sitebuilderShog91-Mar-07 12:48 
GeneralRe: Say... Pin
Rama Krishna Vavilala1-Mar-07 13:16
mvpRama Krishna Vavilala1-Mar-07 13:16 
GeneralRe: Say... Pin
Shog91-Mar-07 14:15
sitebuilderShog91-Mar-07 14:15 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180221.1 | Last Updated 27 Feb 2007
Article Copyright 2007 by Rama Krishna Vavilala
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid