Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicate between a COM DLL and C#

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.

like image 472
Sean Avatar asked Jan 25 '26 22:01

Sean


2 Answers

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.

like image 155
Darin Dimitrov Avatar answered Jan 28 '26 15:01

Darin Dimitrov


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.

like image 36
Skrymsli Avatar answered Jan 28 '26 15:01

Skrymsli