How to register multiple shell extension handlers

182 Views Asked by At

I'm creating a single project that contains multiple shell extension handlers. For example, there's a class for a context-menu handler, a class for a property-system handler, a class for a drag-and-drop handler, etc.

The part I don't understand is how to register CLSIDs for a project that contains multiple handlers. Does each handler get registered using a unique CLSID? Because every time a create a class for a new handler in Visual Studio, the wizard sets it up with its own CLSID. Or is there a way to funnel them all toward my project using a single CLSID? Does a typelib come into play in any of this?

1

There are 1 best solutions below

2
Anders On

You generally have one CLSID per handler type but it might be possible to have just a single CLSID.

If you can have a single CLSID depends on if you can detect which handler you are, just based on what you are QueryInterface'ed for and how/if IShellExtInit::Initialize is called etc.

If you think about how COM works, the shell will call CoCreateInstance(YourCLSID, ..., ISomething) and your .DLL will create a YourCLSID factory and COM will ask this factory to create a instance of ISomething.

If for example your C++ code looks like:

class AllMyHandlers : public IShellExtInit, public IInitializeCommand { ...

then you know that if IInitializeCommand::Initialize is called then you probably are a IExecuteCommand handler. If IShellExtInit::Initialize is called you can look at the parameters to determine your handler type (but not every combination is possible to detect).

This is very much a hack that relies on knowing which interfaces the shell asks for and it could in theory change in the future.

Another approach would be to support everything (and by that I mean, store all useful incoming data from all Initialize methods) and only determine the type when one of the Drag/Add/Execute methods are called and you know for sure which type you are.

I would recommend that you use one CLSID per type, anything else is a bit hacky or requires extra work for little benefit.