0

我想使用 COM 调用具有以下签名的函数:

void GetCompoundList(ref object compIds, ref object formulae, ref object names, ref object boilTemps, ref object molwts, ref object casnos)

我无权访问实现,但对象是包含 String 和 Double 的 SafeArrays 的 Variant 类型,并且都是 out 参数。

这是我声明数组和调用函数的方式:

Array thermoCompounds = null;
Array thermoCompFormulae = null;
Array thermoCompName = null;
Array thermoCompTemp = null;
Array thermoCompWei = null;
Array thermoCompCAS = null;

ppThermoComp.GetCompoundList(thermoCompounds, thermoCompFormulae, thermoCompName, thermoCompTemp, thermoCompWei, thermoCompCAS);

其中 ppThermoComp 是实现接口的类的实例。

但是,函数调用没有效果:调用后数组仍然为

  • 如果我以正确的大小初始化数组,则没有变化:该函数也没有效果
  • 如果我使用不同的参数(例如返回的数组)从同一个 COM 接口调用另一个函数,它可以工作
  • 如果我从 C++ 代码(仍然通过 COM)调用这个函数,它可以工作

C++:

CComVariant cIdsM, cFormulaeM, cNamesM, cTempM, cWeiM, cCASM;
HRESULT hr = thermocompoundsMat->GetCompoundList(&cIdsM, &cFormulaeM, &cNamesM, &cTempM, &cWeiM, &cCASM);

知道我的 C# 代码有什么问题吗?

4

1 回答 1

0

谢谢菲利普和汉斯。实际上,我不得不根据您的 2 条评论进行修改以修复代码。

object thermoCompounds = null;
object thermoCompFormulae = null;
object thermoCompName = null;
object thermoCompTemp = null;
object thermoCompWei = null;
object thermoCompCAS = null;

ppThermoComp.GetCompoundList(ref thermoCompounds, ref thermoCompFormulae, ref thermoCompName, ref thermoCompTemp, ref thermoCompWei, ref thermoCompCAS);

在提出问题之前,我尝试了每一个(objectref),但分别...

顺便说一句,传递 Array 类型在没有ref的情况下编译,但不是使用ref关键字,可能是因为 Array 已经是一个引用。

于 2016-05-12T18:42:35.273 回答