Discussion:
.NET coclass has multiple dispinterfaces
(too old to reply)
Markus Ewald
2008-01-17 07:26:37 UTC
Permalink
Raw Message
Hi!

I've got a COM Interop library written in C#, .NET 2.0. This library
exposes a COM class which implements two interfaces, one interface comes
from an imported VB6 class library (CHelloWorld) and another is declared
in C# itself (CExtendedHello).

If I attempt to use this COM class via late binding (IDispatch) in VB6,
the following happens:

Dim o As Object
Set o = CreateObject("ManagedInteropHello.CExtendedHello")

Call o.Hello ' Doesn't work!
Call o.HelloUser("TestApplication")

I can only call the HelloUser() method that is in the default interface
of the class. But watch what happens if cast this class to CHelloWorld
and back:

Dim hw As HelloWorldLib.CHelloWorld
Set hw = o
Set o = hw

Call o.Hello
Call o.HelloUser("TestApplication") ' Doesn't work!

Now the Hello() method from the CHelloWorld interface works and the
HelloUser() method from the default interface ceases to exist.

Its as if there were two completely seperate IDispatch interfaces for
the class.

Can someone explain this?
What I'm trying to do is give the class a single IDispatch interface
that contains all methods of all interfaces.

-Markus-
Brandon Moore
2010-10-24 18:22:34 UTC
Permalink
Raw Message
In your code you have:

----------
Set hw = o
Set o = hw
----------

Think about this:
x = 5
y = 10
x = y 'now both variables = 10
y = x 'both variables still = 10
Post by Markus Ewald
Hi!
I've got a COM Interop library written in C#, .NET 2.0. This library
exposes a COM class which implements two interfaces, one interface comes
from an imported VB6 class library (CHelloWorld) and another is declared
in C# itself (CExtendedHello).
If I attempt to use this COM class via late binding (IDispatch) in VB6,
Dim o As Object
Set o = CreateObject("ManagedInteropHello.CExtendedHello")
Call o.Hello ' Doesn't work!
Call o.HelloUser("TestApplication")
I can only call the HelloUser() method that is in the default interface
of the class. But watch what happens if cast this class to CHelloWorld
Dim hw As HelloWorldLib.CHelloWorld
Set hw = o
Set o = hw
Call o.Hello
Call o.HelloUser("TestApplication") ' Doesn't work!
Now the Hello() method from the CHelloWorld interface works and the
HelloUser() method from the default interface ceases to exist.
Its as if there were two completely seperate IDispatch interfaces for
the class.
Can someone explain this?
What I'm trying to do is give the class a single IDispatch interface
that contains all methods of all interfaces.
-Markus-
Submitted via EggHeadCafe - Software Developer Portal of Choice
Review of DevExpress DXperience Control Suite
http://www.eggheadcafe.com/tutorials/aspnet/f8ce82a9-d5c2-44fc-91af-e67df5ae502f/review-of-devexpress-dxperience-control-suite.aspx
Brandon Moore
2010-10-24 18:25:09 UTC
Permalink
Raw Message
Set hw = o
Set o = hw

The above isn't going to set o to what hw originally was because hw has been changed to o. For example:

x = 5
y = 10
x = y 'now both x and y = 10
y = x 'both x and y still = 10
Post by Markus Ewald
Hi!
I've got a COM Interop library written in C#, .NET 2.0. This library
exposes a COM class which implements two interfaces, one interface comes
from an imported VB6 class library (CHelloWorld) and another is declared
in C# itself (CExtendedHello).
If I attempt to use this COM class via late binding (IDispatch) in VB6,
Dim o As Object
Set o = CreateObject("ManagedInteropHello.CExtendedHello")
Call o.Hello ' Doesn't work!
Call o.HelloUser("TestApplication")
I can only call the HelloUser() method that is in the default interface
of the class. But watch what happens if cast this class to CHelloWorld
Dim hw As HelloWorldLib.CHelloWorld
Set hw = o
Set o = hw
Call o.Hello
Call o.HelloUser("TestApplication") ' Doesn't work!
Now the Hello() method from the CHelloWorld interface works and the
HelloUser() method from the default interface ceases to exist.
Its as if there were two completely seperate IDispatch interfaces for
the class.
Can someone explain this?
What I'm trying to do is give the class a single IDispatch interface
that contains all methods of all interfaces.
-Markus-
Post by Brandon Moore
----------
Set hw = o
Set o = hw
----------
x = 5
y = 10
x = y 'now both variables = 10
y = x 'both variables still = 10
Submitted via EggHeadCafe - Software Developer Portal of Choice
SharePoint Workflow Custom Input Forms
http://www.eggheadcafe.com/tutorials/aspnet/2a494ffa-c3b0-41e5-9847-80e7cdf3779a/sharepoint-workflow-custom-input-forms.aspx
Markus Ewald
2010-10-24 19:22:44 UTC
Permalink
Raw Message
Post by Markus Ewald
Set hw = o
Set o = hw
x = 5
y = 10
x = y 'now both x and y = 10
y = x 'both x and y still = 10
You are resurrecting a thread from 2 1/2 years ago.

Do you honestly believe someone writing COM interop libraries in .NET
could have misunderstood the concept of basic variable assignment?
Please read the text!


Those lines are placed right next to each other to make it even more
obvious that I'm just assigning the reference forth and back. What
actually happens in those lines is VB casting between IDispatch and
CHelloWorld (which, in COM, involves asking the class to return a
pointer implementing the other interface via QueyInterface())

Casting the coclass instance to CHelloWorld (set hw = o) and back to
IDispatch (set o = hw) should not change a thing. Yet in the RCW created
by the .NET it does alter the behavior. As if IDispatch (the
"dispinterface" was implemented multiple times, once for the class'
default interface and once for the CHelloWorld interface. Thus the
thread title "coclass has multiple dispinterfaces" - and not "help, i'm
confused about variables".

I don't remember what I did back then to fix the actual issue and I
don't even work for the company that required me to write Visual Basic
code anymore.

-Markus-

--- news://freenews.netfront.net/ - complaints: ***@netfront.net ---
Loading...