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 / March 2007

Tip: Looking for answers? Try searching our database.

Marshalling complex types between a .NET CCW and VBScript

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GregB - 01 Mar 2007 11:44 GMT
We want to determine whether it's possible to develop a .NET COM-
callable wrapper (CCW) which exposes complex types that are accessible
to a VBScript client.  We have found that VBS has no problem accessing
'simple' types like integer, String, Boolean.  On the other hand, we
are seeking advice on the optimal approach to dealing with more
complex types, such as string arrays etc.

Marshalling Parameters

Suppose our CCW exposes a class Polygon:

public class Polygon {
   public int Compare(Polygon p)
   {
       // Implementation here.
   }
}

The ProgID for this class is Acme.Geometry.Polygon.

Here's the VBS code to initialise and compare two Polygon objects with
based on our CCW:

dim oP1, oP2
dim iResult

set oP1 = CreateObject("Acme.Geometry.Polygon")
oP1.Points = ' Initialise Points property here for 1st polygon

set oP2 = CreateObject("Acme.Geometry.Polygon")
oP2.Points = ' Initialise Points property here for 2nd polygon

iResult = oP1.Compare(oP2)

The final line leads to the following error raised by the Windows
Script Host (WSH):

438 Object doesn't support this property or method

This seems to be because oP2 loses its type information when it
crosses the COM interface.  Apparently because .NET uses strong
typing, COM Interop is failing to find an overload of the Compare
method matching the 'typeless' object.  At present the workaround for
this is to expose an additional, surrogate method, InteropCompare,
which accepts a weakly-typed parameter:

   public int InteropCompare(Object p)
   {
       return Compare((Polygon) p); // explicit typecast required
   }

Is there another way around this, perhaps using custom marshalling,
ie. the attribute MarshalAs()?

Marshalling Fields

Suppose our CCW exposes a class Strings:

public class Strings {
   public String[] items;
}

The ProgID for this class is Acme.Strings.

Here's the VBS code to initialise a Strings object based on our CCW:

dim oStrings

set oStrings = CreateObject("Acme.Strings")
oStrings.items = Array("Mary", "had", "a", "little", "lamb")

The final line leads to this error:

5 Invalid procedure call or argument

This reference to a "procedure" or "argument" suggests that, under the
COM Interop covers, an implicit method call takes place in order to
make the array assignment.

And this attempt to read the elements in a Strings object:

dim iJoined
iJoined = ""
for I = 0 to UBound(oStrings.items) // no problem here: UBound result
is correct
   iJoined = iJoined & oStrings.items(I) // fails here
next

leads to this error:

450 Wrong number of arguments or invalid property assignment

There may be other types apart from array types that fail to marshal
correctly when defined as class fields.

How can we resolve these issues for our CCW?

Thanks in advance.

GregB
Patrick Steele - 06 Mar 2007 03:43 GMT
> We want to determine whether it's possible to develop a .NET COM-
> callable wrapper (CCW) which exposes complex types that are accessible
> to a VBScript client.

You should be able to as long as all of the methods you want to access
are defined on your object's "default" interface (the only interface
VBScript can access).  See:

http://www.15seconds.com/issue/040721.htm

Signature

Patrick Steele
http://weblogs.asp.net/psteele


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.