Click here to Skip to main content
13,730,590 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 24 Jul 2007
Licenced CPOL

Using Preview Handlers in Windows Vista

, 24 Jul 2007
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how to use Windows Vista Preview Handlers in your application.

Introduction - Preview Mechanism in Vista

In Windows Vista, the Explorer supports a new feature to preview files. You can enable or disable this in Explorer.

Screenshot - Figure1.jpg

Outlook 2007 and Vista both use the same preview infrastructure for showing previews. They check the extension of the file being used and then look for any preview handlers installed on the machine for the same. If a preview handler is registered for this extension, then the preview handler is loaded and called to show the preview.

The calling application passes the HWND and the co-ordinates along with the information about the file to be previewed. The previewer puts up a window in the given region and renders the file preview.

Interfaces Used in Preview

The preview mechanism is based on the IPreviewHandler interface. This interface has a method to show the preview. The preview handler needs a way to receive file information. For this, the preview handler needs a IInitializeWithFile or IInitializeWithStream interface. The preview handler will require a class implementing either two or three of the above interfaces.

In summary, the following interfaces are to be kept in mind:

  • IInitializeWithStream
  • IInitializeWithFile
  • IPreviewHandler

Writing Preview Handlers

The Windows SDK sample PreviewHandler is a great place to begin with if you are going to implement a preview handler. We can take this and customize it to our requirements. We can use the documentation and sample easily, and hence this is not discussed here.

Using Preview Handlers in Applications

Using preview handlers in your application now is easy. Just load the right interface and call it. But that requires a class ID; the CLSID of the component.

Browsing the Registry and looking for the preview-able files, you will notice that handlers register themselves under HKEY_CLASSES_ROOT\<extn>\shellex\ {8895b1c6-b41f-4c1c-a562-0d564250836f}. Take the CLSID from there and create an object of type IID_IPreviewHandler.

Screenshot - Figure2.jpg

Now, IID_IPreviewHandler itself does not have any parameter to take the file name. Query this interface for the existence of IInitializeWithFile. If you get the interface, you can initialize this interface with the file path.

if( S_OK == CoCreateInstance(cls, NULL, CLSCTX_INPROC_SERVER |
IID_IPreviewHandler,(LPVOID*)&m_pIP) )
    if( S_OK == m_pIP->QueryInterface(IID_IInitializeWithFile, 
               (LPVOID*)&m_pIFile ); 
        hr = m_pIFile->Initialize( szFile,STGM_READ); 

Many preview handlers do not implement IInitializeWithFile, but instead implement IInitializeWithStream. In this case, an IStream object has to be created and initialized. To create a stream object, use the CreateStreamOnHGlobal API. Read the contents of the file and initialize this stream.

HANDLE hFile = CreateFile(szFile,FILE_READ_DATA, 
    DWORD dwSize = GetFileSize( hFile,NULL );
    m_hGlobal= GlobalAlloc(GPTR, dwSize );
    BYTE * pByte = (BYTE *)GlobalLock(m_hGlobal);

    if( pByte )

        CreateStreamOnHGlobal(m_hGlobal, TRUE, &m_pStream);    
        hr = m_pIStream->Initialize( m_pStream,STGM_READ);

    CloseHandle( hFile );

Now, you are all set to get the preview. Set the window co-ordinates and call the DoPreview method to get your preview.

if( m_pIP )
    hr = m_pIP->SetWindow( hWnd , &rectPreview );
    hr = m_pIP->DoPreview( );

Managing Associations Between File Names and Handlers

We know that .bat, .cmd, .inf and a few other formats fall in the same category as that of a .txt file. We can just change some Registry keys and change the preview handlers so that these files can be previewed.

Under HKEY_CLASSES_ROOT\<extn>, all extensions that you use normally are listed.

We need to add the "\shellex\ {8895b1c6-b41f-4c1c-a562-0d564250836f}" key and the CLSID of the server which can preview this type of file.

To find the CLSID of the previewers installed on the machine, look for HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\PreviewHandlers. This Registry key has the CLSID and friendly name mappings.

Screenshot - Figure6.jpg

Now, enumerate all the extensions from the HKEY_CLASSES_ROOT key, and display an UI with the friendly names of the previewers so that the user can choose the previewer. On the selection, get the CLSID from the friendly name and use this for setting the previewer.

Where did we reach?

By now:

  • We are able to enumerate the available previewers
  • Manage the associations
  • Preview the files

If we combine all this into a tool, the tool can come handy to manage the previewers and to actually see the preview of the files from a directory. These are the screenshots:

Screenshot - Figure7.jpg


If you have a custom file format, then it calls for writing a preview handler. Writing a preview handler will make the new file format explorer friendly and easy to search.

The preview handling infrastructure of Windows Vista opens up a new feature for applications. Document management, email readers, and printing applications can use the preview mechanism and give great features.


Windows SDK documentation.

Build Instructions

  1. Install VSTS 2005.
  2. Install Windows Vista SDK.
  3. Build using VSTS 2005.


To set the handler, the application needs to be launched as Administrator – the application is not UAC aware.


  • July 25, 2007 -- Created.


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


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

AnswerOutlook 2010+ .msg files et al. not working? Here's why: Pin
fafalone6-Dec-15 0:06
memberfafalone6-Dec-15 0:06 
SuggestionCommercial Ready-to-use preview control Pin
marder19-Dec-13 23:44
membermarder19-Dec-13 23:44 
Generaldoes't work Pin
umeca7426-Apr-11 0:27
memberumeca7426-Apr-11 0:27 
QuestionRe: does't work Pin
LeslieM2-Jun-11 21:27
memberLeslieM2-Jun-11 21:27 
QuestionUnable to compile PreviewHandler SDK Sample Pin
KinnariTShah25-Sep-07 19:32
memberKinnariTShah25-Sep-07 19:32 
AnswerRe: Unable to compile PreviewHandler SDK Sample Pin
KinnariTShah25-Sep-07 22:33
memberKinnariTShah25-Sep-07 22:33 
GeneralManaged code Pin
j.d.smith1-Aug-07 9:52
memberj.d.smith1-Aug-07 9:52 

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 | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 25 Jul 2007
Article Copyright 2007 by Srikanth.rajan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid