What is the best way to deal with kludgy interface hierarchies (MSHTML)? - c#

I'm using the MSHTML API from C# 4.0 and the logistics of running code are not a problem. Writing the code, however, is a pain due to the way that MSHTML and/or COM interfaces are designed. Specifically, there is no interface hierarchy when there should be one. For example, IHTMLDocument7 does not extend IHTMLDocument6, which doesn't extend IHTMLDocument5, and so on (IHTMLDocument2 does extend IHTMLDocument, though).
To further confuse matters there is an HTMLDocument interface that extends DispHTMLDocument (which has all of the methods of the IHTMLDocument* interfaces) and HTMLDocumentEvents_Event (which provides some, but not all, events). To add to the mess, HTMLDocumentClass is a coclass that implements all of the aforementioned interfaces and then some, such as IDocumentSelector and HTMLDocumentEvents4_Event.
I'd really like to be able to work with the API of HTMLDocumentClass, but trying to cast to it gave me:
System.InvalidCastException: Unable to
cast COM object of type
'mshtml.HTMLDocumentClass' to class
type 'mshtml.HTMLDocumentClass'.
Instances of types that represent COM
components cannot be cast to different
types that represent COM components;
however they can be cast to interfaces
as long as the underlying COM
component supports QueryInterface
calls for the IID of the interface.
In addition, some of the interfaces don't have an associated coclass; e.g., there are IHTMLElement* interfaces but no HTMLElement interface nor a HTMLElementClass class. Overall, I am finding it difficult to program to an interface.
Are there good techniques for wrangling with this interface train wreck, or should I give up IntelliSense and use dynamic everywhere? I considered writing wrapper classes that implemented all of the interfaces, but there are so many MSHTML interfaces and each of them has a ton of members so a practical solution has to be automated.

IHTMLDocument6 doesn't extend IHTMLDocument5
Even if it extends IHTMLDocument5, per COM rules, you are still supposed to QueryInterface to get IHTMLDocument5, not to use inheritance. I am glad that they did not let you wonder how you can QI for an interface that is already implemented by the wrapper class as a side effect of inheritance.
I suggest you to not use any of the wrapper classes and switch to backward compatible interfaces when you control the objects. The COM wrapper CLR generated for IE looks like a mshtml.HTMLDocumentClass class from a different assembly, based on the error message.
In COM programming you would see the factory pattern quite often. For the html element object, the factory method is IHTMLDocument2.createElement. Usually you can not create the object on your own if the author choose to use this pattern.
Visual Studio would automatically reference the PIA if one exists, otherwise it uses tlbexp.exe to generate interop assembly prefixed with "Interop". However most of time you would be using a handful interfaces in the PIA, so you can write your own interop types (or copy from Google Code Search) and get ride of this big assembly.

Related

Difference between the different Solution interfaces

When writing an VSIX and you want to access information about the given solution you can simply do DTE2.Solution which will return Solution, so far so good. When looking at the MSDN I can see that there exist multiple solution interfaces: Solution, Solution2, Solution3 and Solution4.
I noticed that the VSIX SDK rather often does this, for whatever reason, in order to offer different functionality. In this case I can't really spot any big difference and I am not really sure when I should use which. Should you always go for Solution4 since it implements all the predecessors?
... for whatever reason, in order to offer different functionality
These are COM interfaces so they're subject to COM rules. Once an interface is published, it is immutable, so adding functionality is done by defining a new interface that inherits from the old.
Numeric suffixes were the convention Microsoft used for versioning COM interfaces. .NET guidelines for interfaces advise against this but, for consistency, the pattern continues in Visual Studio.
Querying a COM interface involves reference counting. That means a call to QueryInterface to get a pointer to the desired interface, and ultimately a call to Release to tell the object you no longer need the reference. The object itself is responsible for its lifetime. The constructor (called by the object's class factory) starts its reference count at 1, and Release deallocates the memory (deletes itself) when the reference count hits 0.
Note that "reference" in this context is not the same as a .NET reference. It's a counted copy of a pointer to one of the object's interfaces.
In the early days, it took a bit of work to make sure you handled the reference correctly. If you can query a newer interface with the combined functionality, that was less work than querying both interfaces separately and ensuring that both were released properly. Inheriting an interface made things easier by reducing reference management.
Now, we have smart pointers, like CComPtr/_com_ptr_t, that can handle those details for you. In the managed world, Runtime-Callable Wrappers (RCW) count that among their responsibilities.
In .NET, it's just as easy to create a new interface as it is to inherit an existing one, and just as easy to use the interface in either case. It's just a matter of a reference cast and sometimes that happens implicitly.
C# 8 adds default implementations but that's a .NET-specific feature. Remember, Visual Studio is still using COM interfaces at its core. Default implementations violate the COM rules.
Should you always go for Solution4 since it implements all the predecessors?
That depends on what you're targeting. As a general rule, use the minimal interface version that has the members you need. If you need members from Solution and Solution3, use Solution3, but not Solution4.
On the other hand, if you know you're targeting at least a version of Visual Studio that implements Solution4, there's no reason you couldn't use Solution4. Once you're certain that you're not accidentally preventing your extension from running in the Visual Studio versions you want to target, it falls to your preference.

Understanding COM Objects and how to declare them

Say I want to create the interface for IMMDeviceEnumerator.
I see examples online showing the definition:
[ComImport]
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IMMDeviceEnumerator
{
}
What I understand (maybe): The [ComImport] Attribute specifies that it's from a dll. The [Guid] attribute is the interface identifier.
What I don't understand: How was this GUID value obtained? What does the [InterfaceType] attribute do? How do I fill in the rest of the functions?
I'm so lost trying to figure this stuff out, all the documentation is super opaque.
How was this GUID value obtained?
The GUID is created as part of the COM interface definition; since you're trying to call someone else's object - you need to use their GUID. You can find it in the mmdeviceapi.h the MMDevice docs point to.
Header file Mmdeviceapi.h defines the interfaces in the MMDevice API.
MIDL_INTERFACE("A95664D2-9614-4F35-A746-DE8DB63617E6")
IMMDeviceEnumerator : public IUnknown
The normal way to do this is to add a reference to the COM dll or run tlbimp.exe which will generate a COM Class Wrapper for you with all the magic goo.
If a COM type library isn't available, though - then you basically have to do it yourself by going through the IDL file.
Like p/invoke signatures, this can get pretty painful - so best to use an existing one if you can.
For the larger questions of COM interop, it basically requires learning a little bit of COM and being familiar with C#. The general docs are out there, but usually if you're just trying to use a well known COM component you're best off using a library if you can.
You create the GUID yourself. There are generators online if you don't want to assign one yourself.
All interface types should derive from IUnknown.
Update: here is a generator. https://www.guidgenerator.com/online-guid-generator.aspx
They use the same one because IMMDeviceEnumerator has already been defined with that specific GUID. If you create your own interface, you will create your own GUID.
You derive off IUnknown because
"Within that constraint, your custom interface can support almost any method or parameter, including asynchronous methods. You can also generate a type library for your custom interfaces so that clients can access information about your object's methods at run time. "

Explicit interface implementation for COM interfaces in C#

Some time I ago I was working on a major refactoring of an old Win32 program implemented with COM, and there were various parts that were implemented with C# (.NET). During my work on this project, I ran across a Microsoft page on COM programming in C# that recommended C# classes explicitly implement COM interfaces, rather than implicity. I recently tried to remember why, and I couldn't. I also couldn't find the page on the MSDN site again. Can anybody please tell me why Microsoft might recommend this ?
Hmm, that makes a wee bit of sense, COM is pure interface-based programming and the actual implementation of the interfaces should be hidden. Implementing interface methods explicitly gets you that automatically because they cannot be public.
Actually doing this is quite pointless, you could (and should) simply apply the [ClassInterface(ClassInterfaceType.None)] attribute to the class. That by itself ensures that the implementation isn't exposed, only the interfaces implemented by the class are visible. Implementing the interface methods explicitly isn't actually good enough. Because you cannot hide the fact that your class inherits System.Object. Which exposes the four public methods of Object and puts a reference to mscorlib.tlb in your type library, a reference that a real COM client will never use. It will almost always work because the odds that the compiler that uses your class runs on a machine that doesn't have .NET installed are pretty small. But very yucky nonetheless, it isn't actually required. Only the machine that uses the class needs it installed.
Just don't do this. Declare the interfaces you implement, give them the [InterfaceType(ComInterfaceType.InterfaceIsDual)] attribute to allow them to be used both early and late bound. And hide the actual implementation of them with [ClassInterface(ClassInterfaceType.None)]. Only sensible way.
It's old, but from here: http://msdn.microsoft.com/en-us/library/aa288461%28v=VS.71%29.aspx they mention implementing an interface explicitly so you can implement multiple interfaces that have the same member names.
This also requires that the user of your class cast an instance of your class to the appropriate interface.
As for why this is especially important for COM: my first guess is so that COM can call one set of methods while managed code may call another. However, I'm guessing here.

.NET add-ins for factory classes where the data can be casted back to the concrete

I have a host application that controls various factory classes which produce implementations of a common data contract. Also, all the factories derive from a particular factory contract. The factories may need particular implementations of the data contract to generate their own objects... so the host can generically pass data via a function in the factory contract that has one argument of the data contract type. The factories then try to cast it to the type they are interested in... ignoring it if it doesn't match. This all works ok so far.
I wanted to extend this to allow users to create add-in factories using the .NET add-in framework, but I'm concerned about the isolation boundaries... For instance, if a factory produces an IData instance, can another factory cast objects produced by the add-in to the shared concrete implementation type? It looks like the need for adaptors in the add-in pipeline may screw this up?
For instance, in the diagram below, the concrete class DataA would be shared between PluginA and PluginB, and the concrete class DataB would be shared between PluginB and PluginC.
Edit:
So far I only knew about the System.Addin functionality for creating add-ins... and the older methods involving direct reflection. I've just discovered MEF, which supposedly doesn't concern itself with the isolation boundaries that are core to the System.Addin stuff. Does anyone with experience with MEF know how this might impact my scenario?
So after some experimentation, I've found MEF can solve these problems. The isolation barriers created by System.Addins seem to make it impossible to retrieve the actual concrete implementation... MEF allows me to cast back discovered add-ins to their concrete types from addins that are aware of the specific implementations.

Exposing C# via COM for C++ Client

we're considering exposing some C# types to C++ clients via COM. What problems can we expect to hit over the life of the project? E.g. how will versioning be managed?
On versioning, it would seem from reading this that we should decorate our types to be exposed with [ClassInterface(ClassInterfaceType.None)] and use an explicit interface. That way I assume we fully control the interface that will be exposed to COM clients.
Thanks in advance.
Since you are using a C++ client you should definitely use explicit interfaces for early binding. Dispatch interfaces are useful when using scripting clients such as VBS but they are rarely useful for C++ clients.
The only way to version an interface is to create a new interface (possibly inheriting from the original interface). When using explicit interfaces you have full control over this process.
This means you should create an interface for every class that you intend to expose via COM. Don't forget to mark every interface and class with the ComVisible and Guid attributes. Also all your classes must have a default constructor.
You'll have to read about the GUID attribute (including this) to maintain binary compatibility and only rebuild the clients when necessary.
Also you might be interested in the ComVisible attribute that helps reduce registry pollution.
To get full control over COM interfaces, define them in MIDL. Build a type library with those interfaces in a C++ project, then import type library to C# and implement interfaces.
This approach is useful with complex interfaces where marshaling is not trivial.
Versions should be done COM-style, changing GUIDs and adding new or inheriting interfaces.

Categories