Reordering methods in ComImport interfaces throws COMException (0x80041001)

Posted by Ohad Schneider on Stack Overflow See other posts from Stack Overflow or by Ohad Schneider
Published on 2014-05-31T21:22:47Z Indexed on 2014/05/31 21:24 UTC
Read the original article Hit count: 280

Filed under:
|
|
|

Consider the following code for COM interop with internet shortcuts:

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("CABB0DA0-DA57-11CF-9974-0020AFD79762")]
public interface IUniformResourceLocatorW
{
    void SetUrl([MarshalAs(UnmanagedType.LPWStr)] string pcszUrl, int dwInFlags);
    void GetUrl([MarshalAs(UnmanagedType.LPWStr)] out StringBuilder ppszUrl);
    void InvokeCommand(IntPtr purlici);
}

[ComImport]
[Guid("FBF23B40-E3F0-101B-8488-00AA003E56F8")]
public class InternetShortcut
{
} 

The following works as expected:

var ishort = new InternetShortcut();
((IPersistFile)ishort).Load("MyLink.url", 0);
((IUniformResourceLocatorW)ishort).GetUrl(out url);

However:

  • If I comment out IUniformResourceLocatorW.SetUrl (which I am not using), IUniformResourceLocatorW.GetUrl throws a COMException (HResult 0x80041001).
  • If I switch between IUniformResourceLocatorW.SetUrl and IUniformResourceLocatorW.GetUrl (that is place the former below the latter) the same exception is thrown
  • If I comment out IUniformResourceLocatorW.InvokeCommand the code runs fine.

It's as if the order has to be preserved "up to" the invoked method. Is this behavior by design? documented somewhere? I'm asking because some COM interfaces are composed of many methods with possibly many supporting types and I'd rather avoid defining what I don't need if possible.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET