By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,786 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,786 IT Pros & Developers. It's quick & easy.

Casting problem : COMImport interfaces in different assemblies

P: n/a
A class in assembly A implements a COM interface as follows :

[ComImport, Guid("CEF04FDF-FE72-11d2-87A5-00C04F6837CF"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
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.GetObjectFromIUnkown(unk); // 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

Oct 10 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.IsComObjects reports False, Marshal.ReleaseComObject will fail -
this was very surprising in our app where the one component first was really
COM / C++ and then ported to .NET: Marshal.ReleaseComObjects 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" <st*********@gmail.comwrote in message
news:O1****************@TK2MSFTNGP04.phx.gbl...
>A class in assembly A implements a COM interface as follows :

[ComImport, Guid("CEF04FDF-FE72-11d2-87A5-00C04F6837CF"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )]
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.GetObjectFromIUnkown(unk); // 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

Oct 10 '06 #2

P: n/a
Bob S <st*********@gmail.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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 10 '06 #3

P: n/a
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]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Bob S <st*********@gmail.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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.