0

I want to call, using COM, a function with the following signature:

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

I have no access to the implementation, but the objects are of Variant type containing SafeArrays of String and Double, and are all out parameters.

Here is how I declare the arrays and call the function:

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);

Where ppThermoComp is an instance of the class implementing the interface.

However, the function call has no effect: the arrays are still null after the call.

  • If I initialize the arrays at the right size, no change: the function has no effect either
  • If I call another function from the same COM interface with different arguments (e.g. an array which is returned), it works
  • If I call this function from a C++ code (still through COM), it works

C++:

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

Any idea what is wrong with my C# code?

seb007
  • 488
  • 3
  • 19
  • I'm confused - the docs say "To use a ref parameter, both the method definition and the calling method must explicitly use the ref keyword, as shown in the following example." but your C# code does not. Does it compile Ok ? – PhillipH May 12 '16 at 11:20
  • A variant is not Array. Pass a variable of *object* instead, cast to the expected array type afterwards. And ping the author, he should have returned a failure HRESULT to tell you that the argument was wrong. – Hans Passant May 12 '16 at 11:26

1 Answers1

0

Thanks Philip and Hans. Actually, I had to modify according to your 2 comments to fix the code.

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);

I tried each one (object and ref) before asking the question, but separately...

By the way, passing the Array type compiles without the ref, but not with the ref keyword, probably because an Array is already a reference.

seb007
  • 488
  • 3
  • 19