CLR SPY and Customer Debug Probes: The QueryInterface Failure Probe

It has taken awhile, but with this entry I will have described all 11 probes that exist in v1.1 of the CLR.

When an RCW queries its underlying COM object for a specific interface, the QueryInterface call could fail for a variety of reasons.  The QueryInterface Failure probe doesn't report every failed QI call as you might expect, but it isolates a specific cause of failure: when an interface cannot be marshaled to the current context.  When this happens, this probe emits a warning like the following:

Failed to QI for interface IMyInterface because it does not have a COM proxy stub registered.

To detect this condition, this probe makes the CLR do some extra work after an applicable QueryInterface call fails.  If the failure happens in a different context than the COM object's original context, the CLR will transition to the original context and try another QueryInterface call.  If this also fails, the CLR does nothing more; the original failure must have been unrelated to the context.  But if it succeeds, then that indicates that the QueryInterface failure is context-specific, so it emits the warning.

There's one other scenario in which this probe emits a warning: when a QueryInterface call fails in the object's original context, but the CLR has determined that this COM object is a standard proxy (because the interface pointer's address is in the range of the loaded ole32.dll).

What can you do about such failures?  You can register an appropriate type library or proxy-stub DLL, depending on the COM component. Or you can take steps to avoid context transitions, such as using the STAThreadAttribute.