c# - 来自 C# 的 VirtualBox COM API

标签 c# com interop virtualbox

我正在尝试使用 C# 中的 VirtualBox COM API (VBoxC.dll)。我跑了

tlbimp VirtualBox.tlb

针对 VirtualBox SDK 中包含的类型库。引用输出程序集构建正常,但在运行时,每当我尝试访问返回数组的属性时,我都会收到 SafeArrayTypeMispatchException(“指定的数组不是预期的类型”)。

例如,我可以看到 IVirtualBox.get_Machines() 定义为

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

我怎样才能正确地接到对 marshall 的调用?

最佳答案

这里是解释您的异常的链接:MSDN reference

您的问题点很可能是 SafeArraySubType,IMachine 是 IDispatch 吗? VarEnum 中有更多选项用于 IUnknown 等。

不幸的是,您必须编辑 COM 库,使其超出 tlbimp 输出的内容...

关于c# - 来自 C# 的 VirtualBox COM API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1126012/

相关文章:

c# - WPF SaveFileDialog 中的 SupportMultiDottedExtensions

c# - 使用c#登录mysql

c# - .Net Release 构建工作速度比 Debug 慢

c# - RealPopup 通过邮槽发送什么样的数据?

c# - 使用 interop.word 在不更改 View 模式的情况下静默更新文档

c# - `Phone` 应该是 C# 中的类还是结构?

c# - 在 C# 中获取文件和文件夹的透明外壳图标

delphi - 将adoconnection从vba传递到delphi

.net - 通过 COM 组件调用 .NET 程序集时出现 "Parameter is incorrect"错误

c# - C# 对 native 互操作的限制