c++ - 如何在没有返回它的属性/函数的情况下获取派生接口(interface) (OLE/COM)

标签 c++ vb.net com

我正在使用 C++ 客户端代码与 VB 应用程序交互。我在这方面完全是新手(我只是一名化学工程师),所以如果问题很愚蠢,请原谅。

因此,访问“BackDoor”接口(interface)需要 VB 中的以下代码,我可以获得所有其他接口(interface),因为我遵循层次结构(如 strm = hyApp.ActiveDocument.Flowsheet.Streams.Item(strmName)

但是正如您在下面的代码中看到的,BackDoor 接口(interface)等于 ProcessStream 接口(interface)!!我不明白这一点,也不知道如何在 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;
        }

}

以下是我用来访问接口(interface)内属性的 COMMethod(它可以与所有其他接口(interface)一起正常工作)

    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 = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, nType, &dp, pvResult, NULL, NULL);"

最佳答案

您拥有的 VB 代码使用的是早期绑定(bind),而您拥有的 C++ 代码使用的是后期绑定(bind)。

将您的 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;
    }
}

您获得的调度对象不起作用的原因是通常只有第一个接口(interface)由标准调度程序处理。即使对于处理多个接口(interface)的自定义或手动调度程序,它们通常也不会调度隐藏(或以其他方式供私有(private)使用)接口(interface)。

关于c++ - 如何在没有返回它的属性/函数的情况下获取派生接口(interface) (OLE/COM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40837201/

相关文章:

internet-explorer - 如何从 Powershell 访问 Internet Explorer 运行实例的经典 Internet Explorer COM 自动化对象?

com - 查找 MS Office Interop 常量的值,而不是对其进行硬编码

c++ - 在不使用 for 或 while 的情况下在排序数组中查找元素

c++ - 为加密程序加载二进制数据

c++ - 如何实现 'square list' 迭代器

vb.net - StartupNextInstance 事件中的Environment.GetCommandLineArgs()

c++ - stringstream noshowpoint 不起作用

xml - 将更新的 XMLDocument 保存为字符串(vb.net 或其他 .net 语言。)

html - 在 gridview 中编码字段

dll - 这是 "better": COM DLL or Standard DLL with a Typelib?