Shell Icon Handler extension

Author

Pavel Yosifovich
25+ years as Software developer, trainer, consultant, author, and speaker. Co-author of “Windows Internals”. Author of “Windows Kernel Programming”, “Windows 10 System Programming, as well as System and kernel programming courses and “Windows Internals” series.

Introduction to Shell Extensions

Shell extensions are a powerful feature of the Windows shell that allow developers to extend and customize the functionality of File Explorer (formerly Windows Explorer) and any other applications utilizing the same interfaces. These extensions are implemented as COM objects and can take various forms, including context menu handlers, property sheet handlers, drag-and-drop handlers, and icon handlers. In this post, we will focus on creating an icon handler, a type of shell extension that enables dynamic customization of file icons based on specific file properties.

Note: This blog post is designed to complement the accompanying video embedded at the top of the page. The video provides an in-depth, visual demonstration of the concepts and code discussed here, making it an invaluable resource for learners.

What Is an Icon Handler?

By default, files with the same extension in Windows have identical icons. However, an icon handler allows us to override this behavior by dynamically assigning icons based on a file’s characteristics. For instance, we can assign different icons to DLL files depending on whether they are 32-bit or 64-bit.

Setting Up the Project

To begin, we use the Active Template Library (ATL), which simplifies the implementation of COM components. Here’s a step-by-step guide:

  1. Create an ATL Project:
    • Open Visual Studio and create a new ATL project.
    • Choose “DLL” as the project type and enable “Allow merging of proxy stub code” for flexibility.
  2. Generate a COM Class:
    • Add a new ATL Simple Object to the project, naming it something like BitnessIconHandler.
    • Configure the object to use single-threaded apartment (STA) mode, as required by File Explorer.

Implementing the Icon Handler

The icon handler requires implementation of two key COM interfaces:

  • IExtractIcon: This interface provides methods to supply the icon’s location and index.
  • IPersistFile: This interface allows the handler to load and process specific files.

Determining File Bitness

The core functionality involves determining whether a DLL file is 32-bit or 64-bit. Here’s how it’s done:

  1. Load the DLL: Use LoadLibraryEx to load the file as data.
  2. Analyze the Header: Access the file’s NT headers to retrieve its machine type and determine the bitness.

Assigning Icons

We use two pre-designed icons, one for 32-bit files and another for 64-bit files, and embed them as resources in the project. The GetIconLocation method assigns the appropriate icon based on the file’s bitness.

Registry Configuration

To register the icon handler, we update the Windows registry under the HKEY_CLASSES_ROOT\DLLFile\ShellEx\IconHandler key. This informs File Explorer to use our custom handler for DLL files.


Windows master developer badge 1

$1,478

$1182 or $120 X 10 payments

Windows Master Developer

Takes you from a “generic” C programmer to a master Windows programmer in user mode and kernel mode.

Testing and Debugging

Since debugging shell extensions in a live File Explorer instance can be challenging, we use the following steps:

  • Use tools like Process Explorer to verify that the handler DLL is loaded.
  • Restart File Explorer after making registry changes.
  • Utilize debug tracing to log critical information during development.

Conclusion

This post demonstrates the basics of creating an icon handler shell extension to customize file icons dynamically. For a deeper dive into COM programming and shell extensions, check out our comprehensive course on TrainSec: “COM Programming with C++.” Try implementing your own icon handler and share your experience in the comments below!

Gain Insider Knowledge

Subscribe to updates from the TrainSec trainers

For more insights into Windows internals and advanced programming concepts, keep exploring TrainSec’s Knowledge Library. Stay tuned for more deep dives into topics that empower your technical growth!

blue depth

About the author

Pavel Yosifovich
25+ years as Software developer, trainer, consultant, author, and speaker. Co-author of “Windows Internals”. Author of “Windows Kernel Programming”, “Windows 10 System Programming, as well as System and kernel programming courses and “Windows Internals” series.