我正在使用C ++客户端代码与VB应用程序连接。我完全是个新手(我只是一名化学工程师),所以如果问题愚蠢,请原谅。
因此,VB中的以下代码需要访问“BackDoor”接口,我可以获取所有其他接口,因为我遵循层次结构(如strm = hyApp.ActiveDocument.Flowsheet.Streams.Item(strmName)
但正如您在下面的代码中看到的那样,BackDoor接口等同于ProcessStream接口!!我不明白这一点,不知道如何在C ++中实现...你能帮忙吗?
VB中的代码:
Function GetMassExergy(strmName As String) As Double
GetMassExergy = EmptyValue_enum.HEmpty 'initialize to empty
Dim strm As ProcessStream
Dim bd As BackDoor
Dim corrNamesVar As TextFlexVariable
Dim corrNames() As String
Dim i As Integer
Dim exergMoniker As String
Dim exerg As RealVariable
Dim Bval As Double
strm = hyApp.ActiveDocument.Flowsheet.Streams.Item(strmName)
bd = strm
corrNamesVar = bd.BackDoorTextVariable("HysysCorrelation.300.[]:Name.0").Variable
corrNames = corrNamesVar.Values
For i = 0 To corrNames.Count - 1
If corrNames(i) = "Mass Exergy" Then
Exit For
End If
Next i
If i = corrNames.Count Then
'failed to find Mass Exergy Correlation
Exit Function
End If
exergMoniker = String.Format("HysysCorrelation.300.{0}:ExtraData.550.0", i)
exerg = bd.BackDoorVariable(exergMoniker).Variable
Bval = exerg.GetValue("kJ/kg")
GetMassExergy = Bval
End Function
C ++中的代码:
void ConnectToHYSYS::GetBackDoor() {
//HyStream is already acquired using Hierarchy
IDispatch* hyStream;
// Try to Query BackDoor from hyCase interface
HRESULT hr = hyStream->QueryInterface(__uuidof(hyBackDoor), (void**)&hyBackDoorDisp);
//Last hr returns S_OK
if (SUCCEEDED(hr))
{
cout << "Got the BackDoor safely" << endl;
//Get BackDoor Text Variable,
VARIANT result;
VariantInit(&result);
// Try to Get a property from BackDoor interface (to make sure that it returned //an actual interface)
hr = COMMethod(DISPATCH_PROPERTYGET, &result, hyBackDoorDisp, L"BackDoorTextVariable", 1, "HysysCorrelation.300.[]:Name.0");
CheckForHr(hr);
BackDoorTextVariable = result.pdispVal;
if (SUCCEEDED(hr))
{
cout << "Got the BackDoor Text Variable safely" << endl;
}
if (FAILED(hr)) {
cout << "Couldnt get the BackDoor Text Variable" << endl;
}
}
if (FAILED(hr)) {
cout << "Couldnt get the BackDoor" << endl;
}
}
以下是我用来访问接口内部属性的COMMethod(它可以与所有其他接口一起正常工作)
HRESULT ConnectToHYSYS::COMMethod(int nType, VARIANT * pvResult, IDispatch * pDisp, LPOLESTR ptName, int cArgs...)
{
if (!pDisp) return E_FAIL;
va_list marker;
va_start(marker, cArgs);
DISPPARAMS dp = { NULL, NULL, 0, 0 };
DISPID dispidNamed = DISPID_PROPERTYPUT;
DISPID dispID;
char szName[200];
// Convert down to ANSI
WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);
// Get DISPID for name passed...
HRESULT hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT, &dispID);
if (FAILED(hr)) {
return hr;
}
// Allocate memory for arguments...
VARIANT * pArgs = new VARIANT[cArgs + 1];
// Extract arguments...
for (int i = 0; i < cArgs; i++) {
pArgs[i] = va_arg(marker, VARIANT);
}
// Build DISPPARAMS
dp.cArgs = cArgs;
dp.rgvarg = pArgs;
// Handle special-case for property-puts!
if (nType & DISPATCH_PROPERTYPUT) {
dp.cNamedArgs = 1;
dp.rgdispidNamedArgs = &dispidNamed;
}
// Make the call!
hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, nType, &dp, pvResult, NULL, NULL);
if (FAILED(hr)) {
return hr;
}
// End variable-argument section...
va_end(marker);
delete[] pArgs;
return hr;
}
我进行调用的行是唯一一个返回错误“0x80020008错误变量类型”的行...我的意思是我在COMMethod写的最后一行hr行“hr = pDisp-&gt; Invoke(dispID,IID_NULL,LOCALE_SYSTEM_DEFAULT) ,nType,&amp; dp,pvResult,NULL,NULL);“
答案 0 :(得分:2)
你拥有的VB代码是使用早期绑定,而你使用的C ++代码是使用后期绑定。
将C ++代码切换为类似于VB代码的代码,例如:
void ConnectToHYSYS::GetBackDoor() {
IDispatch* hyStream = ...;
// Use an actual hyBackDoor
hyBackDoor* hyBackDoorDisp;
HRESULT hr = hyStream->QueryInterface(IID_PPV_ARGS(&hyBackDoorDisp));
if (SUCCEEDED(hr)) {
cout << "Got the BackDoor safely" << endl;
// From the VB code, it seems BackDoorTextVariable is a TextFlexVariable
hr = hyBackDoorDisp->get_BackDoorTextVariable(&BackDoorTextVariable);
CheckForHr(hr);
if (SUCCEEDED(hr)) {
cout << "Got the BackDoor Text Variable safely" << endl;
}
if (FAILED(hr)) {
cout << "Couldnt get the BackDoor Text Variable" << endl;
}
}
if (FAILED(hr)) {
cout << "Couldnt get the BackDoor" << endl;
}
}
您获得的调度对象不起作用的原因通常是标准调度程序只处理第一个接口。即使是处理多个接口的自定义或手动调度程序,通常它们也不会调度隐藏(或其他用于私人用途)接口。
答案 1 :(得分:0)
伊戈尔先生和Acelent先生的回答都是成功的(抱歉不知道如何接受评论作为伊戈尔先生的回答,所以我会在这里复制并接受它作为任何其他人面对的答案同样的问题。
pArgs [i] = va_arg(marker,VARIANT);这表现出未定义的行为,因为传递给COMMethod的实际参数实际上不是VARIANT:COMMethod(...,1,“HysysCorrelation.300。[]:Name.0”); - Igor Tandetnik
谢谢大家......