Page 1 of 1

The return value type of GetPhaseInfo

PostPosted: 04 September 2018, 01:53
by bcbooo
GetPhaseInfo is defined as following:

GetPhaseInfo(BSTR phaseLabel, BSTR phaseAttribute, VARIANT * value);

where when phaseAttribute is 'StateOfAggregation', 'Vapor' or 'Liquid' or 'Solid' will be returned as string.

I have finished one Property Package, and tested in COFE environment, the following codes are wrong:

====== C++ codes start =============
CVaraint v;
v.MakeArray(1,VT_BSTR);
if(phase==_T("Vapor")) v.AllocStringAt(0,_T("Vapor"));
else if.......
......;
*value=v.ReturnValue();
return S_OK;
====== C++ codes end =============

COFE shows that:
"error: GetPhaseInfo() failed for phase Vapor: Failed to get state of aggregation, string value expected"

Then I changed the codes into the following:

====== C++ codes start =============
CVaraint v;
if(phase==_T("Vapor")) v.bstrVal=CBSTR(_T("Vapor"));
else if.......
......;
*value=v.ReturnValue();
return S_OK;
====== C++ codes end =============

The error tip of COFE still exists.

So I am not sure which VARIANT type of 'value' should I return?

Re: The return value type of GetPhaseInfo

PostPosted: 04 September 2018, 07:14
by jasper
A string:

v.vt=VT_BSTR;
v.bstrVal=SysAllocString(L"Liquid");

Re: The return value type of GetPhaseInfo

PostPosted: 04 September 2018, 07:17
by jasper
Also noting a problem with your second attempt (other than not setting the .vt field): the CBSTR will surely destroy the string in its destructor, and once you return the string, it is no longer owned by you (the callee should destroy it). This will likely lead to a crash. Not sure which CBSTR this is exactly, but most implementations have some sort of 'detach' member to release ownership.

Re: The return value type of GetPhaseInfo

PostPosted: 04 September 2018, 08:43
by bcbooo
Thank you Jasper