计算监视表达式

当 Visual Studio 准备好显示监视表达式的值,后者又调用 IDebugParsedExpression:: EvaluateSync的调用 IDebugExpression2:: EvaluateSync 。 这将导致包含该表达式的值和类型的一 IDebugProperty2 对象。

IDebugParsedExpression::EvaluateSync的此实现,表达式同时分析并进行计算。 此实现执行以下任务:

  1. 分析并计算表达式导致保存该值及其类型的泛型对象。 在 c# 中,那么,当在 C++ 这表示为 VARIANT时,这表示为 object 。

  2. 实例化类 (称为在此示例中的 CValueProperty ) 实现 IDebugProperty2 接口和存储类将返回的值。

  3. 返回从 CValueProperty 对象的 IDebugProperty2 接口。

托管代码

这是 IDebugParsedExpression::EvaluateSync 的实现托管代码的。 帮助器方法 Tokenize 分析表达式以分析树。 helper 函数 EvalToken 将标记为值。 helper 函数 FindTerm 递归遍历分析树,调用表示值并将所有操作 (添加或减号) 在表达式中每个节点的 EvalToken 。

namespace EEMC
{
    public class CParsedExpression : IDebugParsedExpression
    {
        public HRESULT EvaluateSync(
            uint evalFlags,
            uint timeout,
            IDebugSymbolProvider provider,
            IDebugAddress address,
            IDebugBinder binder,
            string resultType,
            out IDebugProperty2 result)
        {
            HRESULT retval = COM.S_OK;
            this.evalFlags = evalFlags;
            this.timeout = timeout;
            this.provider = provider;
            this.address = address;
            this.binder = binder;
            this.resultType = resultType;

            try
            {
                IDebugField field = null;
                // Tokenize, then parse.
                tokens = Tokenize(expression);
                result = new CValueProperty(
                             expression,
                             (int) FindTerm(EvalToken(tokens[0], out field),1),
                             field,
                             binder);
            }
            catch (ParseException)
            {
                result = new CValueProperty(expression, "Huh?");
                retval = COM.E_INVALIDARG;
            }
            return retval;
        }
    }
}

非托管代码

这是 IDebugParsedExpression::EvaluateSync 的实现在非托管代码中。 helper 函数 Evaluate 分析并计算表达式,它返回一个表示得到的值的 VARIANT 。 helper 函数 VariantValueToProperty 绑定 VARIANT 到 CValueProperty 对象。

[C++]
STDMETHODIMP CParsedExpression::EvaluateSync( 
    in  DWORD                 evalFlags,
    in  DWORD                 dwTimeout,
    in  IDebugSymbolProvider* pprovider,
    in  IDebugAddress*        paddress,
    in  IDebugBinder*         pbinder,
    in  BSTR                  bstrResultType,
    out IDebugProperty2**     ppproperty )
{
    // dwTimeout parameter is ignored in this implementation.
    if (pprovider == NULL)
        return E_INVALIDARG;

    if (paddress == NULL)
        return E_INVALIDARG;

    if (pbinder == NULL)
        return E_INVALIDARG;

    if (ppproperty == NULL)
        return E_INVALIDARG;
    else
        *ppproperty = 0;

    HRESULT hr;
    VARIANT value;
    BSTR    bstrErrorMessage = NULL;
    hr = ::Evaluate( pprovider,
                     paddress,
                     pbinder,
                     m_expr,
                     &bstrErrorMessage,
                     &value );
    if (hr != S_OK)
    {
        if (bstrErrorMessage == NULL)
            return hr;

        //we can display better messages ourselves.
        HRESULT hrLocal = S_OK;
        VARIANT varType;
        VARIANT varErrorMessage;

        VariantInit( &varType );
        VariantInit( &varErrorMessage );
        varErrorMessage.vt      = VT_BSTR;
        varErrorMessage.bstrVal = bstrErrorMessage;

        CValueProperty* valueProperty = new CValueProperty();
        if (valueProperty != NULL)
        {
            hrLocal = valueProperty->Init(m_expr, varType, varErrorMessage);
            if (SUCCEEDED(hrLocal)) 
            {
                hrLocal = valueProperty->QueryInterface( IID_IDebugProperty2,
                        reinterpret_cast<void**>(ppproperty) );
            }
        }

        VariantClear(&varType);
        VariantClear(&varErrorMessage); //frees BSTR
        if (!valueProperty)
            return hr;
        valueProperty->Release();
        if (FAILED(hrLocal))
            return hr;
    }
    else
    {
        if (bstrErrorMessage != NULL)
            SysFreeString(bstrErrorMessage);

        hr = VariantValueToProperty( pprovider,
                                     paddress,
                                     pbinder,
                                     m_radix,
                                     m_expr,
                                     value,
                                     ppproperty );
        VariantClear(&value);
        if (FAILED(hr))
            return hr;
    }

    return S_OK;
}

请参见

概念

计算 " 监视 " 窗口表达式

采样表达式计算的实现