473,473 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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(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
3 4686
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex | last post by:
I am creating an application that allows the user to link a plug-in "utility" class (Watcher) to a class at runtime. There are several Watcher utilities, each with a different style of "watching"...
7
by: Fred Mellender | last post by:
I would like to make a library (dll) that contains a number of classes and interfaces for use by other implementers. One of the interfaces I want to define is: public interface Foo { Bar...
4
by: Chris | last post by:
I created a control that derives from the System.Windows.Forms.Control namespace. I then created an interface to which this new control must adhere, so that I could require future controls to...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
0
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. ...
9
by: Kris | last post by:
Hi. I have a asp class A with a public function AA() ----------------A.aspx.cs------------------------ public partial class A : System.Web.UI.Page { protected void Page_Load(object sender,...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
3
by: TheLongshot | last post by:
I'm in the process of converting a VS 2003 app to VS 2005. Right now I've run into a problem which I can't figure out. The following statement: return...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.