6

I need a method to create Icon Overlay's for Folders and Files in Windows XP/Vista, using C# or C++? Any examples?

Thanks, -Sean!

Xearinox
  • 3,224
  • 2
  • 24
  • 38
Sean
  • 241
  • 1
  • 3
  • 12

2 Answers2

11

Do not do this! Please, please don't.

You will break a lot of applications. Shell extensions must not use the .net framework (or any other similar framework), ever.

Here's an explanation why you must not do this.

Write your extension in C/C++, but not C#.

Update: Even though as of .NET4 it's possible to have multiple framework versions in one process, it is still not recommended and not supported by Microsoft! See this post about why:

[...] These problems led us to officially recommend against—and not support—the development of in-process shell extensions using managed code.

Stefan
  • 43,293
  • 10
  • 75
  • 117
  • 1
    I think you misunderstood what I was trying to say. I want to write the extension in C++ and use that from C#. Is that possible? – Sean May 09 '09 at 16:17
  • 1
    You want to get the overlays like explorer does in your own application? Now that's of course possible without breaking other apps. – Stefan May 09 '09 at 16:22
  • 1
    But if you just want overlays in your C# app, you don't need to write an overlay handler at all - that's just for explorer. If you need your own overlays, just do it without an overlay handler. The windows list control supports overlays already (you have to add an imagelist to the control, then specify one of 16 possible overlay icons from that list in the item mask). – Stefan May 09 '09 at 16:24
  • 1
    Sorry being so unclear, I just got my wisdom teeth out yesterday and I'm still a little drugged up. I want something like Dropbox (https://www.getdropbox.com) does. I want to be able to add custom icon overlays to files and folders that I choose. And I'll use C++. – Sean May 09 '09 at 16:29
  • 1
    In that case, yes, you have to implement an overlay handler. Here's a good example: http://www.codeproject.com/KB/shell/overlayicon.aspx – Stefan May 09 '09 at 19:42
  • 1
    This was helpful, but actually, I found a really great example in the "Microsoft Platform SDK," which compiled right away w/o any trouble. Thanks for all your help though. ;) – Sean May 10 '09 at 05:52
  • 1
    It seems like you can write shell extensions safely with .NET 4.0. This is because the .NET 4.0 runtime can be loaded side-by-side into the same process with other versions of the .NET runtime. See - http://blogs.msdn.com/b/codefx/archive/2010/09/14/writing-windows-shell-extension-with-net-framework-4-c-vb-net-part-1.aspx – Brandon Cuff Jul 06 '12 at 18:39
  • Isn't this answer out-of-date? Now there even is a shell extension library dedicated to C#: https://github.com/dwmkerr/sharpshell – Nicolas Raoul Feb 18 '16 at 04:41
  • No, it's not out of date. The answer is still no: https://blogs.msdn.microsoft.com/oldnewthing/20130222-01/?p=5163/ – Stefan Feb 18 '16 at 06:45
7

Tigris' TortoiseSVN product heavily uses icon overlays provided by library shared by several Tortoise products, the overlays themselves are written in C++ rather than C#.

The documentation for the TortoiseOverlays project explains how they use it and the problems they have encountered (username: guest, empty password), and the GPL'ed sourcecode is in the Subversion repository (same username/password as above).

Snippit from documentation:

TortoiseOverlays registers itself with the explorer to handle the nine states mentioned above, i.e. it registers nine overlay handlers. The explorer process initializes the TortoiseOverlays handler, calling its IShellIconOverlayIdentifier::GetOverlayInfo(). TortoiseOverlays looks for the registered overlay handlers under HKLM\Software\TortoiseOverlays\Statusname and calls their GetOverlayInfo() method so they can initialize too (Note that any change to the icon name, index, ... your handler does are overwritten later and won't be used - it's TortoiseOverlays that handles the icons now). After the initialization, TortoiseOverlays relays every call to its IShellIconOverlayIdentifier::IsMemberOf() method to the other handlers. The first handler that returns S_OK determines whether the icon is shown or not.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Richard Slater
  • 6,313
  • 4
  • 53
  • 81
  • 1
    Awesome! I'll have a look at this. Do you know how I could implement this into a C# project that I'm working on? Don't I basically just need to register a DLL? – Sean May 09 '09 at 15:58
  • 1
    As hinted by Stefan, there is a reason TortoiseSVN shell extensions are written in unmanaged code. You can always write much of the code in unmanaged then get it to call into your .NET application. – Richard Slater May 09 '09 at 16:22
  • 1
    Thanks. That's what I planned to do, I never intended to imply that I wanted to write the shell extension code in C#, I just wanted to access the C++ code from C#. – Sean May 09 '09 at 16:31