簡體   English   中英

使用COM從C#中將數組作為out參數傳遞

[英]Passing array as out parameter from c# using COM

我想使用COM調用具有以下簽名的函數:

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

我無權訪問實現,但是對象是Variant類型的對象,其中包含String和Double的SafeArrays,並且都是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是實現接口的類的實例。

但是,函數調用無效:調用后數組仍為null

  • 如果我以正確的大小初始化數組,則不會更改:該函數也無效
  • 如果我從同一個COM接口使用不同的參數調用另一個函數(例如,返回的數組),則該函數有效
  • 如果我從C ++代碼(仍然通過COM)中調用此函數,那么它將起作用

C ++:

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

知道我的C#代碼有什么問題嗎?

謝謝菲利普和漢斯。 實際上,我必須根據您的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);

在問這個問題之前,我嘗試了每個對象對象ref ),但分別...

順便說一句,傳遞Array類型時不使用ref進行編譯,但不使用ref關鍵字進行編譯,可能是因為Array已經是引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM