Overview
-----------
I have a legacy application that hosts the VBScript engine. It allows users
to write customization VBScripts that call into the host via a COM interface.
I'd really like to replace the C++ COM host object with a C# implementation,
but I can't seem to work around the bug/flaw/limitation that the .NET COM
Interop marshaler won't pass VBScript arrays to my interface by reference. I
need some type of custom attribute or marshaler to get around this problem
because .NET's default interop handling for this case is wrong.
Detail
-------
I have a COM interface originally defined in IDL but shown in C# here:
[Guid("469DBD63-0774-46D2-AAB3-6E217E0EA0D2"), ComVisible(true)]
public interface ITesting
{
int GetItemCount(object safeArray); //[in] VARIANT safeArray
}
I have THOUSANDS of VBScripts that invoke this interface like:
Dim Arr(1)
Arr(0) = "A"
Arr(1) = "B"
Dim Count
Count = Testing.GetItemCount(Arr)
When this interface is implemented in a C++ ATL COM object, VBScript can
invoke it fine. The Arr argument gets passed to me as a VT_VARIANT|VT_BYREF.
In the C++ code, I dereference it manually to get to the array, and I use
the array without modifying it.
When this interface is implemented in a C# .NET COM object, VBScript throws
a "Variable uses an Automation type not supported in VBScript:
'GetItemCount'" error with code 800A01CA. The only way I can make the call
succeed is by modifying the script to use an extra set of parentheses around
Arr, which forces VBScript to pass the array by value instead of by reference.
Due to the vast number of scripts and the fact that they're out of my
control (i.e., they belong to my customers), I can't just change all the
calls to add an extra set of parentheses around each argument. I really,
desperately need a solution to this. This is a huge roadblock for me because
I can't migrate from C++ to C# until this problem is fixed.
I've tried declaring the parameter a variety of different ways (e.g.,
object[], ref object[]) with no success, and I've even tried using the
MarshalAs(UnmanagedType.CustomMarshaler) attribute with a custom
SafeArrayMarshaler that implements ICustomMarshaler. Unfortunately, the .NET
COM Interop layer returns an error to VBScript without ever calling into my
custom marshaler.
According to the "Marshaling ByRef Variants" section in
http://msdn2.microsoft.com/en-us/library/2x07fbw8.aspx, it should be possible
to marshal the array into my method correctly as long as I don't want to
modify the array. But it just doesn't work, and I consider this a bug in the
.NET COM Interop layer.
Please help! I'm hoping for good things from the "Managed Newsgroups"
support.
Thanks,
Bill
BMenees@community.nospam - 03 Mar 2008 16:57 GMT
Same question. I'm just replying so my post's display name will match what
my MSDN account is configured for.