Discussion:
Accessing C# arrays in VB6 results in "Function or interface marked as restricted..."
(too old to reply)
Arghyle
2009-05-27 21:36:12 UTC
Permalink
Ok - 10,000 foot view... I have a C# class that exposes a public
bool array. Looks kinda like this:

[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]public MyClass
{
public bool[] myArray;
}

Assume I allocated 5 items for the array in the constructor. Now
let's say I'm in VB6 and assume I've created an instance of MyClass
called MyObject. When trying to access the member field like so:
MyObject.myArray(1) results in "Function or interface marked as
restricted, or the function uses an Automation type not supported in
Visual Basic".

I'm pretty sure it's marshalling properly - the VB Object Browser
knows that.myArray is an array of Booleans.

Also, I can do a uBound on myArray and get the correct # of allocated
items. But when I try to access those items - kaboom.

I'm pretty sure I'm not the first person to attempt something like
this. I've read some posts that said if I were passing the array as a
parameter, marking it as 'ref' would resolve the problem. BUT, I'm
just trying to access the property directly so that won't work.

Anybody have any insights they can share with me? Thanks much!
Ben Voigt [C++ MVP]
2009-05-29 19:31:16 UTC
Permalink
Do you have the same problem with short[]? I don't think that C# bool and
VB6 Boolean are the same. To add to the confusion, C# 'bool' is just a
shorthand for 'System.Boolean' so it might show up as Boolean() in the VB6
Object Browser but it still means .NET System.Boolean, not VB6 Boolean.
Post by Arghyle
Ok - 10,000 foot view... I have a C# class that exposes a public
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]public MyClass
{
public bool[] myArray;
}
Assume I allocated 5 items for the array in the constructor. Now
let's say I'm in VB6 and assume I've created an instance of MyClass
MyObject.myArray(1) results in "Function or interface marked as
restricted, or the function uses an Automation type not supported in
Visual Basic".
I'm pretty sure it's marshalling properly - the VB Object Browser
knows that.myArray is an array of Booleans.
Also, I can do a uBound on myArray and get the correct # of allocated
items. But when I try to access those items - kaboom.
I'm pretty sure I'm not the first person to attempt something like
this. I've read some posts that said if I were passing the array as a
parameter, marking it as 'ref' would resolve the problem. BUT, I'm
just trying to access the property directly so that won't work.
Anybody have any insights they can share with me? Thanks much!
Giovanni Dicanio
2009-06-02 21:03:54 UTC
Permalink
Post by Arghyle
Ok - 10,000 foot view... I have a C# class that exposes a public
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]public MyClass
{
public bool[] myArray;
}
[...]
Post by Arghyle
I'm pretty sure it's marshalling properly - the VB Object Browser
knows that.myArray is an array of Booleans.
Following Ben's suggestion about differentiation between VB6 Boolean vs.
.NET Boolean, you may want to try with a MarshalAs setting like this for
your 'bool[] myArray' public property:

[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_BOOL)]

Or you could just export a pair of getter/setter public methods to expose
the array property, e.g.

public bool GetMyArray(int index)

public void SetMyArray(int index, bool value)

HTH,
Giovanni
Arghyle
2009-06-22 19:09:01 UTC
Permalink
FYI: Marshalling it differently was indeed the trick. Many thanks!
Post by Giovanni Dicanio
Post by Arghyle
Ok - 10,000 foot view... I have a C# class that exposes a public
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]public MyClass
{
public bool[] myArray;
}
[...]
Post by Arghyle
I'm pretty sure it's marshalling properly - the VB Object Browser
knows that.myArray is an array of Booleans.
Following Ben's suggestion about differentiation between VB6 Boolean vs.
..NET Boolean, you may want to try with a MarshalAs setting like this for
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_BOOL)]
Or you could just export a pair of getter/setter public methods to expose
the array property, e.g.
public bool GetMyArray(int index)
public void SetMyArray(int index, bool value)
HTH,
Giovanni
Loading...