VirtualBox COM API from C#

This is for discussing general topics about how to use VirtualBox.
Post Reply
forzar
Posts: 1
Joined: 2. Oct 2009, 05:31
Primary OS: MS Windows XP
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: MS Windows 2003

VirtualBox COM API from C#

Post by forzar »

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

Code: Select all

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

Code: Select all

[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?
DEmberton
Posts: 5
Joined: 26. Sep 2009, 10:00
Primary OS: MS Windows XP
VBox Version: OSE other
Guest OSses: Windows Linux

Re: VirtualBox COM API from C#

Post by DEmberton »

I'm not familiar with the VBox API, but I do know from doing COM in C# that tlbimp can't cover all cases. Pointers to arrays of pointers for example and it gets confused, which sounds like your problem. You may need to write some unsafe C#, or what I found easier was to write a C++ .NET wrapper and call that from C#.
Post Reply