Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / April 2006

Tip: Looking for answers? Try searching our database.

Enumerating MarshalByRefObject objects in VB script

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eugene Pavlov - 17 Apr 2006 15:20 GMT
Hi,

I have the following interfaces in my C# project:

       [Guid("EFA1BE2F-92E3-4c69-9204-AE77D31A1E22")]
       [InterfaceType(ComInterfaceType.InterfaceIsDual)]
       public interface IMyEnumerable
       {
               [DispId(-4)]
               IEnumerator GetEnumerator();
       }

       [Guid("95A9B63E-A858-40eb-8493-EE895BCEF70B")]
       [InterfaceType(ComInterfaceType.InterfaceIsDual)]
       public interface IMyItem
       {
               string Name();
       }

The interfaces are implemented by two classes that are derived from the
MarshalByRefObject class:

       public class MyEnumerable : MarshalByRefObject, IMyEnumerable
       {
           // ...
       }

       public class MyItem : MarshalByRefObject, IMyItem
       {
           public string Name()
           {
              return "My Item Name";
           }
       }

If I register the resulting assembly for COM interop and want to
iterate through MyEnumerable class I get a runtime error: 424 "Object
required"
The code in the vb script looks like this:

           Set enumerable = GetObject(...)

           For Each item In enumerable
               MsgBox item.Name
           Next

If I don't derive from MarshalByRefObject, everything works fine.

What am I doing wrong?
Dmytro Lapshyn [MVP] - 18 Apr 2006 07:41 GMT
Hi Eugene,

I've done some research on your question and it seems so far that you don't
need an explicit implementation of GetEnumerator, nor you have to derive
your own interface from IEnumerable.

According to MSDN ("Simulating COM Interfaces" topic) and a PDF document
I've found on the web
(http://center.org/p/docs/events/715/ComNetInterop.pdf), it's enough to
implement IEnumerable in your COM-visible class - the framework will do the
rest of the magic behind the scenes.

P.S. You might be also interested in reading this nice article on COM
interop:

http://www.codeproject.com/dotnet/cominterop.asp

> Hi,
>
[quoted text clipped - 45 lines]
>
> What am I doing wrong?
Eugene Pavlov - 18 Apr 2006 11:25 GMT
Dmytro,

Thank you for your answer, but if I don't declare GetEnumerator() in
the IMyEnumerable interface - nothing changes.

The problem is that when I inherit MyEnumerable class from
MarshalByRefObject class the items in the resulting IEnumVARIANT
exposed by MyEnumerable class to COM has VT_UNKNOWN type, which can not
be used by VB. If I don't - the item's type is VT_DISPATCH and
everything works as expected.
Dmytro Lapshyn [MVP] - 18 Apr 2006 12:47 GMT
OK, why would you derive a COM-visible class from MarshalByRefObject? Do you
plan to use it in .NET Remoting scenarios as well? If so, consider creating
a COM-visible facade (NOT derived from MarshalByRef) that would delegate all
the calls to the real class.

Also, why do you need MyEnumerable at all? It's enough to implement the
standard IEnumerable interface, IMO.

> Dmytro,
>
[quoted text clipped - 6 lines]
> be used by VB. If I don't - the item's type is VT_DISPATCH and
> everything works as expected.
Eugene Pavlov - 18 Apr 2006 14:19 GMT
>> Do you plan to use it in .NET Remoting scenarios as well?
Yes this is what I need.

>> If so, consider creating a COM-visible facade (NOT derived from MarshalByRef) that would delegate all the calls to the real class.
Is this the only solution? Unfortunatly in our case we can't do this.
Dmytro Lapshyn [MVP] - 19 Apr 2006 07:49 GMT
> Is this the only solution? Unfortunatly in our case we can't do this.

Yes, this is the only one I can think of. But what really suprises me is why
can't you introduce an additional facade class? It is not something you just
"can't do", at least technically. Would you elaborate?

>>> Do you plan to use it in .NET Remoting scenarios as well?
> Yes this is what I need.
>
>>> If so, consider creating a COM-visible facade (NOT derived from
>>> MarshalByRef) that would delegate all the calls to the real class.
> Is this the only solution? Unfortunatly in our case we can't do this.
Eugene Pavlov - 19 Apr 2006 09:25 GMT
Dmytro,

>> why can't you introduce an additional facade class
Since we have too much of such classes, the creation of additional
facade classes will make our solution rather chaotic. And, of course,
this is a performance issue.

>>Yes, this is the only one I can think of
What about marshalling IEnumVariant manually?

What is magical about derivatives of the MarshalByRefObject class?
Dmytro Lapshyn [MVP] - 26 Apr 2006 10:13 GMT
> Since we have too much of such classes, the creation of additional
> facade classes will make our solution rather chaotic. And, of course,
> this is a performance issue.

OK this makes sense.

> What about marshalling IEnumVariant manually?

This can be done, but I haven't seen yet anyone in person who would
sucessfully create a custom marshaler. Still, it is worth a try if nothing
else helps.

> What is magical about derivatives of the MarshalByRefObject class?

I think the fact that a class is derived from MarshalByRefObject directly
affects the underlying marshaling code. I am not sure why it affects COM
Interop marshaling though (might have something to do with DCOM). Think of
it as of a "marker interface" - once you derive from it, you declare you
want a certain treatment for your class.

Here's a blog post which delves really deep into the implementation details:

http://blogs.msdn.com/cbrumme/archive/2003/07/14/51495.aspx

> Dmytro,
>
[quoted text clipped - 7 lines]
>
> What is magical about derivatives of the MarshalByRefObject class?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.