views:

306

answers:

2

I asked a question the other day regarding overlay icons. With help, I figured out how to get that working.

Here's how an icon overlay works (as far as I understand): Before the shell draws an icon it contacts all the icon overlay handlers in the system to determine whether it should draw an overlay on the that particular icon.

My setup:
I have a registered Shell Extension (Icon Overlay Handler) that I want to use to display icon overlays. Also, I have a .NET application (C#) that will write to a database (SQLite, most likely) with the names, etc. of all the files and folders I want to display an overlay on.

My problem is:
How do I get the Shell Extension (I think its basically a COM DLL) to call back into my .NET application? Or is that overkill and should I just have the Shell Extension read from the database directly?

Possible solutions?

  1. Have the Shell Extension (icon overlay handler) read the database and determine whether to show overlay.
  2. Have the Shell Extension call back into a .NET application to determine whether to show the overlay.

I hope this makes sense, if not, I'll try to elaborate.

+1  A: 

A COM DLL cannot talk to .NET assembly directly. You might need to expose your .NET assembly as COM object and talk to this COM object instead. But this might in fact be an overkill in your scenario. Another option would be to expose the functionality that talks to the database in your .NET assembly as some interoperable service (WCF?) that might be called from the shell etension.

Darin Dimitrov
What do you suggest? Would it be a bad idea to have the COM DLL read the database directly? Isn't this basically the same as the second thing you suggested (obviously not exactly, but the functionality).
Sean
Shell extension talking to the database would violate the DRY principle because if I understand you correctly you already have the code written in C#. So either regasm.exe your DLL and call it from the Shell Extension or expose it as web service and use this web service.
Darin Dimitrov
Ok, so if I regasm my C# DLL I can talk to it from my COM DLL?
Sean
Right, as you would with any COM object.
Darin Dimitrov
Thank you. I think I've got it now. I just got my shell extension to call a managed dll (C#) which adds two numbers!
Sean
A: 

Yes, if you mark your assembly as COM visible and run regasm, then your COM dll can import the generated type library and call CoCreateInstance to get a reference to your .NET classes.

HOWEVER, it is a little scary to pull the .NET framework into a shell extension. So you might want to make sure that the .NET code is invoked out-of-process... ie CLSCTX_LOCAL _SERVER to CoCreateInstance.

Skrymsli
I'm only calling a function from my .NET DLL, though. The shell extension is written in unmanaged code. Is this still a problem?
Sean
This (http://www.codeproject.com/KB/cs/ManagedCOM.aspx) should work. Right?
Sean
Yeah, just make sure that this line is eventually passing CLSCTX_LOCAL_SERVER as opposed to CLSCTX_INPROC_SERVER:HRESULT hRes = pDotNetCOMPtr.CreateInstance(MyInterop::CLSID_MyDotNetClass);This will cause your managed code to run in another process and that way it doesn't get pulled into explorer.exe.
Skrymsli