2

I am trying to use the VirtualBox COM API (VBoxC.dll) from C#. I ran

tlbimp VirtualBox.tlb

against the typelib included in the VirtualBox SDK. Referencing the output assembly builds OK but at runtime I get a SafeArrayTypeMispatchException ("Specificed array was not of the expected type") whenever I try to access properties that return arrays.

I can see, for example, that IVirtualBox.get_Machines() is defined as

[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_DISPATCH)]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime), DispId(0x60020009)]
public virtual extern IMachine[] get_Machines();

How can I get this call to marshall correctly?

2 Answers 2

2

Here is a link explaining your exception: MSDN reference

Your problem spot is most likely SafeArraySubType, is IMachine an IDispatch? There are more options in VarEnum for IUnknown, etc.

Unfortunately you will have to edit your COM library beyond what tlbimp spits out...

1
  • IMachine is not an IDispatch. I am going to give it a go from C++ starting with the IDL.
    – Aidan Ryan
    Commented Jul 15, 2009 at 2:18
1

There are a few command line options you can try with tlbimp.

Run tlbimp /? to see a few, one worth trying is /sysarray, this marshals arrays differently and may solve your issue.

1
  • 1
    Sysarray does not, unfortunately, provide a fix.
    – Aidan Ryan
    Commented Jul 14, 2009 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.