I have a C# library that I'm trying to expose to VBA. I can pass parameters to functions just fine (ie "ref byte[] someArray"), but passing objects or structs just won't work.
If I try passing a byte array as a property of a class, I get the following error in VB-
Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic
If I try passing a byte array as a property of a struct, I get the following error in VB-
I've been fighting this for two days now and while I keep finding posts that claim to have the answer, none of them have worked for me.
So here's my code as it currently sits:
[ComVisible(true)]
[Guid("7F53F7A5-15C9-4A99-A855-38F5E87702D0")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)] // Tried as InterfaceIsDual and as InterfaceIsIDispatch
public interface IDetail
{
[DispId(1)] // Tried with and without these
int SomeInt { get; set; }
[DispId(2)]
string SomeString { get; set; }
[DispId(3)]
byte[] SomeByteArray {
return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UI1)]
get;
[param: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UI1)]
set;
}
}
[ComVisible(true)]
[Guid("F77FB3D4-27E0-4BFA-A21E-5ACB671151E9")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("G4COMTest.Detail")]
public class Detail:IDetail
{
public int SomeInt { get;set; }
public string SomeString { get; set; }
// Tried MarshalAs in all combinations of class and interface
public byte[] SomeByteArray {
[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UI1)]
get;
[param: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UI1)]
set;
}
}
[ComVisible(true)]
[Guid("5E8F9FF0-3156-479E-A91D-0DADD43881FB")]
[ClassInterface(ClassInterfaceType.None)]
public class Worker:IWorker
{
// works with the 'ref'
public int ReturnIntWByteArrayParam(ref byte[] testByteArray)
{
return testByteArray.Count();
}
public int ReturnIntWObjParam(IDetail detail)
{
return detail.SomeInt;
}
public IDetail ReturnObjNoParams()
{
var o = new Detail();
o.SomeInt = 87;
o.SomeString = "What are you doing Dave";
return o;
}
}
[ComVisible(true)]
[Guid("04962F29-DBBD-48AC-B4FB-180EEF562771")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IWorker
{
int ReturnIntWByteArrayParam(ref byte[] testByteArray);
int ReturnIntWObjParam(IDetail detail);
IDetail ReturnObjNoParams();
}
Calling it from VB6:
Dim o As New G4COMTest.Worker
Dim d As New G4COMTest.Detail
Dim byt(2) As Byte
d.SomeInt = 356 '// Works
d.SomeString = "Hello from client" '// Works
d.SomeByteArray = byt '// Errors as either class or struct
MsgBox mWorker.ReturnIntWObjParam(d)
Thanks in advance for any help!