Complex types as arrays are expected to be VARIANT's when calling into COM
through COM interop.
So you have to declare them as Object (or ref Object for [in,out]) in
CSharp.
Here's a sample that illustrates this...
// CS file
using System.Runtime. InteropServices ;
using System;
// interface
[InterfaceType(C omInterfaceType .InterfaceIsDua l)]
[Guid("1ab6f6ea-83b5-4b16-934b-fb82d9af8e0a")]
public interface IDotNetInterfac e
{
int Write( Object aParam1); //[in]VARIANT
}
// class
[ClassInterface( ClassInterfaceT ype.None)]
[ProgId("Test.Do tNetIf")]
public class DotNetInterface : IDotNetInterfac e
{
public DotNetInterface () {}
public int Write( Object aParam1)
{
foreach(byte b in aParam1 as Array)
Console.WriteLi ne(b);
return (aParam1 as Array).Length; // Return Array.Length (will be
marshalled as a VARIANT containing an int type)
}
}
'VBS file
On Error Resume Next
Dim o
Dim ar 'Variant
Dim arr(2) 'Array 3 elements
arr(0) = CByte(1) 'add 1 as byte value in element 0
arr(1) = CByte(2)
arr(2) = CByte(3)
ar = arr 'set ar to array
set o = CreateObject("T est.DotNetIf")
r = o.Write (ar) 'pass Variant containing an array of bytes to callee
if Err.Number <> 0 then
Wscript.Echo "Failure :" & Err.Description
else
Wscript.Echo r
end if
Willy.
"Ken Layton" <Ke*******@disc ussions.microso ft.com> wrote in message
news:E9******** *************** ***********@mic rosoft.com...
Attempting to pass a VBScript Array of Bytes to a C# Managed COM object
has
resulted in some very undesirable and strange behavior. When definining a
method in my C# COM class as "void Write(byte[] bBytes)" or "void
Write(byte[] bBytes, int nLen)" or "int Write(byte[] bBytes, int
nLen)"VBScript returns a "Invalid procedure call or argument" exception.
However when I define the method as "int Write(byte[] bBytes)" it works
just
fine. Unfortunately the method definition that works is not the one I
need.
Regardless of whether one works or one doesn't work, they should all work.
Does anyone know why the CCW can't handle the method definition, and why
it
is behaving in such a flakey manner?
Things I have tried to make it work:
- I have used the appropriate MarshalAs attribute in front of the byte[]
array.
- I have used the [ClassInterface( ClassInterfaceT ype.None)] attribute with
an explicit interface to make the tlbexp tool explicitly define the
interface
methods.