Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

LP#TrayIconBuster

0.00/5 (No votes)
21 Dec 2014 10  
A utility that removes phantom icons from the Icon Tray
Before After
Screenshot - LPTrayIconBuster1.gif Screenshot - LPTrayIconBuster2.gif

Introduction

A lot of programs install a NotifyIcon in the Icon Tray. Your system probably shows icons for Volume Control, Task Manager, Network Connections and many more. When a process installs a NotifyIcon and then exits without disposing of it, the icon remains present until you move the mouse pointer over it, making it suddenly disappear. A lot of such phantom icons may pile up when developing a new application that installs a NotifyIcon but exits due to a fatal exception or a debug session being stopped in the middle. This utility will take care of Tray Icon clean-up automatically.

The Icon Tray

The Icon Tray is one of many windows managed by the Windows Explorer. It is in fact a toolbar holding a lot of buttons. Some of these buttons are hidden while others are visible. For each button, the Explorer holds a "process image name," which is the filename of the process that created the NotifyIcon.

There is no direct support for manipulating the Icon Tray in the .NET Framework, so we need a lot of P/Invoke stuff to call Win32 functions, first to locate the right toolbar and then to enumerate the buttons and get their file names, i.e. the filenames of the processes that created them. The final step is to delete the icons without filenames, i.e. the filename gets replaced by null when the process exits.

The program

The program is simple. It does not show a Form; it simply shows yet another NotifyIcon. It offers a ContextMenu with three MenuItems: periodic run (runs every 5 seconds, checked by default), run now (for an immediate single run) and quit. When the TrayIconBuster attempts to remove phantom icons, it first locates the correct ListView. Then it enumerates all icons a first time looking for its own icon (more on that later). Next it enumerates all icons again and removes the ones without a file name associated to them. There are basically four source files. Those are the main Form, the TrayIconBuster and two help classes: LP_Process and LP_Pinner.

Main Form

Yes, there actually is a main form. By default, it is invisible. One could change the source code to make it visible. Then it would simply hold a single button, equivalent to the "run now" item of the ContextMenu.

TrayIconBuster

This class has a single static method that removes the phantom icons. It can be called directly by any program that wishes to clean up the icons. Typically this would happen when the application starts up and wants to remove its own icons from a previous run.

public static uint RemovePhantomIcons() {...}

The method attempts to remove all phantom icons and returns the number of icons actually removed. It throws an exception if and when things go badly wrong.

LP_Process

This class represents some other process. It supports reading and writing its memory. It is used in conjunction with those Win32 functions that have pointers as parameters. Since these pointers need to be valid in the other process, special steps have to be taken to get valid pointers in the first place, and to copy data to and from those locations from/to our own memory map.

LP_Pinner

This class is used by LP_Process to pin down a buffer, so the garbage collector is not able to move it around. This is sometimes necessary when pointers are used, as is the case when LP_Process performs a read or write operation crossing process boundaries.

Safety

The TrayIconBuster class deletes some windows. If anything goes wrong with that, chances are that too many things will disappear. While deleting icons is not fatal for Windows Explorer, there seems to be only one way to recover from it and that is by restarting Windows. We want to avoid that being necessary. As a safety measure, TrayIconBuster enumerates the icons twice:

  • In the first pass, it does not change or delete any icon; it just looks for the file names associated with them. In particular, it tries to find one ending on ".exe" as a sanity check.
  • If the first pass is successful, a second pass is started and only now phantom icons -- i.e. the ones without filenames -- are removed. So if, for whatever reason, not a single filename is retrieved, the second pass will not be launched and all icons remain as they were.

The program has been tested on a 32-bit version of Windows XP. It has not been tested on other versions of Windows and it has not been tested on any 64-bit Windows version. Thanks to the safety precautions, we believe it is safe to try and run it on those systems as well, but we can not guarantee that it will perform the job it is intended for. Please feel free to report successes or problems on any Windows version.

Points of interest

Some of the following items relate to recent topics on one of the CodeProject message boards:

  • Read or write memory belonging to another process
  • Apply the "using statement" to automatically have Dispose() called when the statement block is exited
  • Use some GC methods to temporarily pin down a buffer in memory
  • Have a global try-catch construct in the static Main() method
  • Form1.cs also contains code to convert a JPEG image into an icon file

History

  • 16 July, 2007 -- LP#TrayIconBuster 1.0 (first release)
  • 22 December, 2014 -- version 2.0 now supports 64-bit Windows (using two different data structures for toolbar buttons) and works on Windows 7 and Windows 8 (dealing with the new NotifyIconOverflowWindow. Warning: in order to operate properly on Win64, it must be built for "Any CPU".

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