Casting problem : COMImport interfaces in different assemblies

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob S

    Casting problem : COMImport interfaces in different assemblies

    A class in assembly A implements a COM interface as follows :

    [ComImport, Guid("CEF04FDF-FE72-11d2-87A5-00C04F6837CF"),
    InterfaceType(C omInterfaceType .InterfaceIsIUn known)]
    internal interface IMyInterface
    {
    [PreserveSig]
    int MyMethod();
    }

    class MyClass : IMyInterface
    {
    int MyMethod()
    {
    ....
    }
    }

    This interface is exposed to the unmanaged world via COM Interop.

    Now, assembly B also defines IMyInterface and gets this interface from the
    unmanaged world as follows :

    IntPtr unk = GetMyInterface( ); // Gets IMyInterface from unmanaged world
    object obj = Marshal.GetObje ctFromIUnkown(u nk); // its really the class
    MyClass from assembly A above
    IMyInterface myIntf = obj as IMyInterface; // this cast does not work and
    returns null becuase of separate declarations of IMyInterface in the two
    assemblies !

    How to get this scenario to work? One would think that .Net would recognize
    that both the interfaces are the same ( based on the ComImport and Guid
    attributes) and allow the cast, but it doesnt allow the cast.

    It is not possible for me to use a shared assembly and put the declaration
    of IMyInterface in that assembly.

    So how can I solve this problem?

    Thanks
    Bob



  • Dr. Jochen Manns

    #2
    Re: Casting problem : COMImport interfaces in different assemblies

    As far as I understand .NET always detects in this scenario that you are
    talking from .NET to .NET. So the COM InterOp is removed
    (Marshal.IsComO bjects reports False, Marshal.Release ComObject will fail -
    this was very surprising in our app where the one component first was really
    COM / C++ and then ported to .NET: Marshal.Release ComObjects throws an
    exception from this day on) and in the pure .NET world you are not able to
    cast an instance to a .NET interface which it does not implement.

    By the way: .NET in this case is not interested in the interface attributes
    at all.

    Jochen

    "Bob S" <staheli.bob@gm ail.comwrote in message
    news:O1P%23fmC7 GHA.4644@TK2MSF TNGP04.phx.gbl. ..
    >A class in assembly A implements a COM interface as follows :
    >
    [ComImport, Guid("CEF04FDF-FE72-11d2-87A5-00C04F6837CF"),
    InterfaceType(C omInterfaceType .InterfaceIsIUn known)]
    internal interface IMyInterface
    {
    [PreserveSig]
    int MyMethod();
    }
    >
    class MyClass : IMyInterface
    {
    int MyMethod()
    {
    ...
    }
    }
    >
    This interface is exposed to the unmanaged world via COM Interop.
    >
    Now, assembly B also defines IMyInterface and gets this interface from the
    unmanaged world as follows :
    >
    IntPtr unk = GetMyInterface( ); // Gets IMyInterface from unmanaged world
    object obj = Marshal.GetObje ctFromIUnkown(u nk); // its really the class
    MyClass from assembly A above
    IMyInterface myIntf = obj as IMyInterface; // this cast does not work and
    returns null becuase of separate declarations of IMyInterface in the two
    assemblies !
    >
    How to get this scenario to work? One would think that .Net would
    recognize
    that both the interfaces are the same ( based on the ComImport and Guid
    attributes) and allow the cast, but it doesnt allow the cast.
    >
    It is not possible for me to use a shared assembly and put the declaration
    of IMyInterface in that assembly.
    >
    So how can I solve this problem?
    >
    Thanks
    Bob
    >
    >
    >

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Casting problem : COMImport interfaces in different assemblies

      Bob S <staheli.bob@gm ail.comwrote:

      <snip>
      How to get this scenario to work? One would think that .Net would recognize
      that both the interfaces are the same ( based on the ComImport and Guid
      attributes) and allow the cast, but it doesnt allow the cast.
      No. They're *not* the same interface. If they're in different
      assemblies, they're different types.

      You need to put the interface in a single assembly.
      It is not possible for me to use a shared assembly and put the declaration
      of IMyInterface in that assembly.
      Why not? Why do you need both interfaces to start with?

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Bob S

        #4
        Re: Casting problem : COMImport interfaces in different assemblies

        Is not always possible to have the interface in the shared assembly, because
        one .Net assembly may expose an interface (eg COM IDataObject) to the
        unmanaged world while another .Net assembly may consume the interface.
        Though the underlying interface is the same, .Net will not allow the
        casting. (In the case of IDataObject, this situation will not arise if you
        use the Microsoft defined type IOleDataObject - however this will not be
        possible for other interfaces).

        Thus .Net does not support the basic tenet of COM which says that COM is a
        "binary standard". It should not matter what the underlying ".Net type" of
        the interface is as long as the binary contract of the interface is the
        same.

        I think this is a serious flaw in COM support of .Net. I would love to hear
        some more views on this.

        Regards
        Bob



        Suppose assembly A exposes a COM IDataObject to the unmanaged world and
        defines

        "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
        news:MPG.1f954a a35c114d9d98d53 4@msnews.micros oft.com...
        Bob S <staheli.bob@gm ail.comwrote:
        >
        <snip>
        >
        >How to get this scenario to work? One would think that .Net would
        >recognize
        >that both the interfaces are the same ( based on the ComImport and Guid
        >attributes) and allow the cast, but it doesnt allow the cast.
        >
        No. They're *not* the same interface. If they're in different
        assemblies, they're different types.
        >
        You need to put the interface in a single assembly.
        >
        >It is not possible for me to use a shared assembly and put the
        >declaration
        >of IMyInterface in that assembly.
        >
        Why not? Why do you need both interfaces to start with?
        >
        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...