Katy -> Visual C++ -> Show a property page


Show a property page

This class allows you to activate a property page given a filename. It works on WinNT 4.0 and Win95.

I adapted the code from an unattributed sample on microsoft.public.win32.programmer.kernel.

Download this sample.

Code

SHFilePropSheet.h

#ifndef _SHFILEPROPSHEET_H_
#define _SHFILEPROPSHEET_H_

class CSHFilePropSheet
{
public:
    CSHFilePropSheet();
    ~CSHFilePropSheet();
  
    // Show the property sheet. Use "the desktop" as the default owner window
    void Show(LPCSTR filename, HWND owner=0);

private:
    LPMALLOC      m_pMalloc;
    LPSHELLFOLDER m_psfDesktop;

private:
    LPITEMIDLIST CreatePidlFromPath(LPCSTR filename, HWND owner);
};

#endif

SHFilePropSheet.cpp

#include "stdafx.h"            // for precompiled headers
#include "shlobj.h"            // for interface definitions, SH* functions
#include "shfilepropsheet.h"   // class header
#include "afxdisp.h"           // for AfxThrowOleException

CSHFilePropSheet::CSHFilePropSheet()
{
    HRESULT hr;

    // IMalloc *MUST* be accessed before IShellFolder!
    hr = SHGetMalloc(&m_pMalloc);
    if (FAILED(hr))
        AfxThrowOleException(hr);

    hr = SHGetDesktopFolder(&m_psfDesktop);
    if (FAILED(hr))
    {
        m_pMalloc->Release();
        AfxThrowOleException(hr);
    }
}

CSHFilePropSheet::~CSHFilePropSheet()
{
    m_psfDesktop->Release();
    m_psfDesktop = NULL;

    m_pMalloc->Release();
    m_pMalloc = NULL;
}

LPITEMIDLIST CSHFilePropSheet::CreatePidlFromPath(LPCSTR filename, HWND owner)
{
    WCHAR wszName[MAX_PATH];
    DWORD cchEaten;
    LPITEMIDLIST pidl=NULL;
    HRESULT hr;

    MultiByteToWideChar(CP_ACP, 0, filename, -1, wszName, MAX_PATH);
    hr = m_psfDesktop->ParseDisplayName(owner, 0, wszName, &cchEaten, &pidl, 0);
    if (FAILED(hr))
        return NULL;

    return pidl;
}

void CSHFilePropSheet::Show(LPCSTR filename, HWND owner)
{
    // Create a structure with instructions
    SHELLEXECUTEINFO sei;
    sei.cbSize = sizeof(sei);
    sei.fMask = SEE_MASK_INVOKEIDLIST;
    sei.lpVerb = "properties";
    sei.lpIDList = CreatePidlFromPath(filename, owner);

    // If sucessfully created, show the property sheet.
    if (sei.lpIDList)
    {
        ShellExecuteEx(&sei);
        m_pMalloc->Free(sei.lpIDList);
    }
    else
    {
        CString msg;
        msg.Format("Error converting %s to an ITEMIDLIST\n", filename);
        AfxMessageBox(msg);
    }
}

Usage

#include "shlobj.h"
#include "shfilepropsheet.h"

void test()
{
    // Note that this object needs to remain in memory for as long as the
    // property sheet is displayed. In an MFC application, I'll put it into
    // my CWinApp-derived class.
    CSHFilePropSheet Propsheet;
    Propsheet.Show("C:\AUTOEXEC.BAT");
}

This code displays a property sheet that looks like (the details will vary when run on your own computer, of course):

Autoexec.bat Properties

Note that the display may vary depending on the operating system you're using, and the type of file being displayed. For example, if you're using Windows NT, and the file is on an NTFS partition, a "Compressed" checkbox is added to the attributes section. Windows 95 only displays the current folder in the Location field, while Windows NT shows the full path. Depending on the file, tabs other than the "General" tab may be displayed.

In other words, this code shows exactly what the File Explorer shows when you choose File | Properties from the menu.

Wish list

Some other things I wish I could do with this code:

 

 


No solicitation beyond this point.
Don't even think of adding me to your mailing list. Put up a web page I'm interested in, and let me ask you for information.
Comments? Questions? Send mail to Katy

This page was last modified on 1998/08/23