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

COM Interop issue : .Net is being too 'smart'

P: n/a
I am having trouble with the following scenario....

Assembly A
Returns a managed implementation of COM IStream interface to the
unmanaged world
=============

/////////////////
[ComImport]
[GUID("..")]
interface IStreamAssemblyA
{
...
}

class MyStreamImpl : IStreamAssemblyA
{
..........
}

IntPtr GetStreamImpl()
{
MyStreamImpl obj = new MyStreamImpl();
IntPtr ret =
Marshal.GetComInterfaceForObject(obj,typeof(IStrea mAssemblyA));
return ret;
}
///////////////////
Assembly B
Gets and uses a COM IStream interface from the unmanaged world. In
case the implementation is an unmanaged one, everything works OK. In
case the implementation is actually from assembly A above (or any
other managed implementation), the cast fails as shown below.....
==========

/////////////////
[ComImport]
[GUID("..")]
interface IStreamAssemblyB
{
...
}

IntPtr GetStreamFromUnmanagedWorld()
{
...
}

void UseStream()
{
IntPtr streamUnk = GetStreamFromUnmanagedWorld();

object obj = Marshal.GetObjectForIUnknown(streamUnk); // obj is
actually class MyStreamImpl from assembly A above

IStreamAssemblyB stream = obj as IStreamAssemblyB; // this fails
becuase of different ".Net type" of obj and stream
}
/////////////////
The above problem exists becuase .Net does not recognize that even though
IStreamAssemblyA and IStreamAssemblyB are different ".Net types" they are
actually COM interfaces imported into .Net and so the cast above should be
allowed (and QueryInterface should be used internally).

How can I solve this issue? Its not possible to have a common IStream
declartion becuase AssemblyA and AssemblyB are not related in any way except
to the extent that assembly B happens to use the managed IStream
implementation from assembly A.

Any help would be appreciated!

Bob
Oct 26 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
TDC
Create AssembyC in which you place IStreamAssemblyCommon and then
reference that from both A and B.
Bob S wrote:
I am having trouble with the following scenario....

Assembly A
Returns a managed implementation of COM IStream interface to the
unmanaged world
=============

/////////////////
[ComImport]
[GUID("..")]
interface IStreamAssemblyA
{
..
}

class MyStreamImpl : IStreamAssemblyA
{
.........
}

IntPtr GetStreamImpl()
{
MyStreamImpl obj = new MyStreamImpl();
IntPtr ret =
Marshal.GetComInterfaceForObject(obj,typeof(IStrea mAssemblyA));
return ret;
}
///////////////////
Assembly B
Gets and uses a COM IStream interface from the unmanaged world. In
case the implementation is an unmanaged one, everything works OK. In
case the implementation is actually from assembly A above (or any
other managed implementation), the cast fails as shown below.....
==========

/////////////////
[ComImport]
[GUID("..")]
interface IStreamAssemblyB
{
..
}

IntPtr GetStreamFromUnmanagedWorld()
{
..
}

void UseStream()
{
IntPtr streamUnk = GetStreamFromUnmanagedWorld();

object obj = Marshal.GetObjectForIUnknown(streamUnk); // obj is
actually class MyStreamImpl from assembly A above

IStreamAssemblyB stream = obj as IStreamAssemblyB; // this fails
becuase of different ".Net type" of obj and stream
}
/////////////////
The above problem exists becuase .Net does not recognize that even though
IStreamAssemblyA and IStreamAssemblyB are different ".Net types" they are
actually COM interfaces imported into .Net and so the cast above should be
allowed (and QueryInterface should be used internally).

How can I solve this issue? Its not possible to have a common IStream
declartion becuase AssemblyA and AssemblyB are not related in any way except
to the extent that assembly B happens to use the managed IStream
implementation from assembly A.

Any help would be appreciated!

Bob
Oct 26 '06 #2

P: n/a
As I said, its not possible to have a common IStream becuase assembly A (the
managed implementation) is not under my control. I could be consuming a
managed implementation from any arbitrary 3rd party assembly.

Bob

"TDC" <NO***********@lycos.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
Create AssembyC in which you place IStreamAssemblyCommon and then
reference that from both A and B.
Bob S wrote:
>I am having trouble with the following scenario....

Assembly A
Returns a managed implementation of COM IStream interface to the
unmanaged world
=============

/////////////////
[ComImport]
[GUID("..")]
interface IStreamAssemblyA
{
..
}

class MyStreamImpl : IStreamAssemblyA
{
.........
}

IntPtr GetStreamImpl()
{
MyStreamImpl obj = new MyStreamImpl();
IntPtr ret =
Marshal.GetComInterfaceForObject(obj,typeof(IStre amAssemblyA));
return ret;
}
///////////////////
Assembly B
Gets and uses a COM IStream interface from the unmanaged world. In
case the implementation is an unmanaged one, everything works OK. In
case the implementation is actually from assembly A above (or any
other managed implementation), the cast fails as shown below.....
==========

/////////////////
[ComImport]
[GUID("..")]
interface IStreamAssemblyB
{
..
}

IntPtr GetStreamFromUnmanagedWorld()
{
..
}

void UseStream()
{
IntPtr streamUnk = GetStreamFromUnmanagedWorld();

object obj = Marshal.GetObjectForIUnknown(streamUnk); // obj is
actually class MyStreamImpl from assembly A above

IStreamAssemblyB stream = obj as IStreamAssemblyB; // this fails
becuase of different ".Net type" of obj and stream
}
/////////////////
The above problem exists becuase .Net does not recognize that even though
IStreamAssemblyA and IStreamAssemblyB are different ".Net types" they are
actually COM interfaces imported into .Net and so the cast above should
be
allowed (and QueryInterface should be used internally).

How can I solve this issue? Its not possible to have a common IStream
declartion becuase AssemblyA and AssemblyB are not related in any way
except
to the extent that assembly B happens to use the managed IStream
implementation from assembly A.

Any help would be appreciated!

Bob

Oct 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.