Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / ATL

The Complete Idiot's Guide to Writing Shell Extensions - Part IX

Rate me:
Please Sign up or sign in to vote.
4.95/5 (42 votes)
2 Jun 2006 718.8K   5.7K   151  
A tutorial on writing an extension to customize the icons displayed for a file type.
// TxtFileIcons.cpp : Implementation of DLL Exports.


// Note: Proxy/Stub Information
//      To build a separate proxy/stub DLL, 
//      run nmake -f TxtFileIconsps.mk in the project directory.

#include "stdafx.h"
#include "resource.h"
#include <initguid.h>
#include "TxtFileIcons.h"

#include "TxtFileIcons_i.c"
#include "TxtIconShlExt.h"

CComModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_TxtIconShlExt, CTxtIconShlExt)
END_OBJECT_MAP()

/////////////////////////////////////////////////////////////////////////////
// DLL Entry Point

extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
    if (dwReason == DLL_PROCESS_ATTACH)
    {
        _Module.Init(ObjectMap, hInstance, &LIBID_TXTFILEICONSLib);
        DisableThreadLibraryCalls(hInstance);
    }
    else if (dwReason == DLL_PROCESS_DETACH)
        _Module.Term();
    return TRUE;    // ok
}

/////////////////////////////////////////////////////////////////////////////
// Used to determine whether the DLL can be unloaded by OLE

STDAPI DllCanUnloadNow()
{
    return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// Returns a class factory to create an object of the requested type

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    return _Module.GetClassObject(rclsid, riid, ppv);
}

/////////////////////////////////////////////////////////////////////////////
// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer()
{
CRegKey key;
LONG lRet;

    // On NT/2K, put our extension in the "approved" list.
    if ( 0 == (GetVersion() & 0x80000000) )
        {
        lRet = key.Open ( HKEY_LOCAL_MACHINE, 
                          _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"),
                          KEY_SET_VALUE );

        if ( ERROR_SUCCESS == lRet )
            {
            lRet = key.SetValue ( _T("Text file icon extension"), 
                                  _T("{DF4F5AE4-E795-4C12-BC26-7726C27F71AE}") );

            key.Close();
            }

        if ( ERROR_SUCCESS != lRet )
            return HRESULT_FROM_WIN32(lRet);
        }

    // Store the current DefaultIcon so we can restore it when we're 
    // unregistered.  If there already is a backup of the DefaultIcon value,
    // then do nothing.
    // **NOTE**  In production code, you should not hardcode HKCR\txtfile
    // as the key holding info on text files, but instead read the key name
    // from the default value of HKCR\.txt to make your code robust.  I've
    // hard-coded "txtfile" here for simplicity.
    lRet = key.Open ( HKEY_CLASSES_ROOT, _T("txtfile\\DefaultIcon"), 
                      KEY_QUERY_VALUE | KEY_SET_VALUE );

    if ( ERROR_SUCCESS == lRet )
        {
        TCHAR szOldDefIcon[2*MAX_PATH];
        DWORD dwSize = sizeof(szOldDefIcon);
        DWORD dwType;

        // Check if there's already a backup of the DefaultIcon value.
        lRet = key.QueryValue ( szOldDefIcon, _T("OldDefIcon"), &dwSize );

        if ( ERROR_SUCCESS != lRet )
            {
            // The OldDefIcon backup doesn't exist, so save the current value.
            //
            // Note that I'm using the registry APIs directly in order to handle
            // any value type.  On Win2K the DefaultIcon value is of type
            // REG_EXPAND_SZ, which CRegKey doesn't handle.  (It only handles
            // REG_SZ and REG_DWORD.)
            dwSize = sizeof(szOldDefIcon);

            lRet = RegQueryValueEx ( key.m_hKey, NULL, NULL, &dwType,
                                     (LPBYTE) szOldDefIcon, &dwSize );

            if ( ERROR_SUCCESS == lRet )
                {
                RegSetValueEx ( key.m_hKey, _T("OldDefIcon"), 0, dwType, 
                                (const BYTE*) szOldDefIcon, dwSize );
                }
            }

        key.Close();
        }

    // registers object, typelib and all interfaces in typelib
    return _Module.RegisterServer(false);
}

/////////////////////////////////////////////////////////////////////////////
// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer()
{
CRegKey key;
LONG lRet;

    // On NT/2K, remove our extension from the "approved" list.
    if ( 0 == (GetVersion() & 0x80000000) )
        {
        lRet = key.Open ( HKEY_LOCAL_MACHINE, 
                          _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"),
                          KEY_SET_VALUE );

        if ( ERROR_SUCCESS == lRet )
            {
            key.DeleteValue ( _T("{DF4F5AE4-E795-4C12-BC26-7726C27F71AE}") );
            key.Close();
            }
        }

    // Restore the old DefaultIcon value from the backup we made during
    // registration.
    // **NOTE**  In production code, you should not hardcode HKCR\txtfile
    // as the key holding info on text files, but instead read the key name
    // from the default value of HKCR\.txt to make your code robust.  I've
    // hard-coded "txtfile" here for simplicity.
    lRet = key.Open ( HKEY_CLASSES_ROOT, _T("txtfile\\DefaultIcon"), 
                      KEY_QUERY_VALUE | KEY_SET_VALUE );

    if ( ERROR_SUCCESS == lRet )
        {
        TCHAR szOldDefIcon[2*MAX_PATH];
        DWORD dwSize = sizeof(szOldDefIcon);
        DWORD dwType;

        // Check if there's already a backup of the DefaultIcon value.
        //
        // Note that I'm using the registry APIs directly in order to handle
        // any value type.  On Win2K the DefaultIcon value is of type
        // REG_EXPAND_SZ, which CRegKey doesn't handle.  (It only handles
        // REG_SZ and REG_DWORD.)
        lRet = RegQueryValueEx ( key.m_hKey, _T("OldDefIcon"), NULL, &dwType,
                                 (LPBYTE) szOldDefIcon, &dwSize );

        if ( ERROR_SUCCESS == lRet )
            {
            // The OldDefIcon backup exists, so write the contents back to the
            // default value of the DefaultIcon key...
            RegSetValueEx ( key.m_hKey, NULL, 0, dwType, 
                            (const BYTE*) szOldDefIcon, dwSize );

            // ...and get rid of the backup.
            key.DeleteValue ( _T("OldDefIcon") );
            }

        key.Close();
        }

    return _Module.UnregisterServer(false);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Software Developer (Senior) VMware
United States United States
Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

Mike was a VC MVP from 2005 to 2009.

Comments and Discussions