How can I get the DisplayString field from Debugger.GetExpression?

399 views Asked by At

Background

I'm writing a Visual Studio Extension in C# that executes code in the Immediate Window while debugging another application. The expression returns a value that can be a int, a string literal, a class, etc. The application that I'm debugging is written in C++.

The code

To execute the Immediate Window commands I use this line of code:

var expression = dte.Debugger.GetExpression("funcname()");

And to retrieve the result I use:

string val = expression.Value;

and:

var children = expression.DataMembers;

Here is myFunc() in the application I'm debugging:

std::vector<int> myFunc()
{
    return { 1, 2, 3, 4, 5 };
}

The problem

When I manually run the expression in the Immediate Window, the returned object is dumped as I see it in the Watch Window (see here). I managed to find all the children names, but the values are missing (see here).

What I want is to achieve the DisplayString ({ size=5 }), but I haven't found anything useful yet.

How can I get the DisplayString field from Debugger.GetExpression?

EDIT: I don’t have to use this API. If you know another way that can return this sting, please suggest it. One idea could be retrieving the full output string of the Immediate Window (see the right side), and then parsing it.

EDIT2: See this video that better explains the issue

2

There are 2 answers

0
lax48 On BEST ANSWER

I found out what the problem was: I wasn't passing the UseAutoExpandRules (defaulted to false) in GetExpression. Now my code works as expected!

Thanks to @Mia Wu-MSFT who put dte.Debugger.GetExpression("tempC", true, 1) in her code and made me discover the UseAutoExpandRules argument

3
Mia Wu-MSFT On

you used a vector variable to get/parse the function return value, it’s reasonable, because the return result of myFunc() is exactly a vector type, thus the variable “std::vector result” can parse and get the vector object correctly. What do you mean by “ manually run the expression in the Immediate Window, the returned object is dumped as I see it in the Watch Window”?

Whilst, EnvDTE.Expression.Value is a string representing the value of the object. It has nothing to do with the displaystring ‘{ size=5 }’, it’s just a property expression. You can print it with below sample code:

public static void Value(DTE dte)  
{  
    // Setup debug Output window.  
    Window w = (Window)dte.Windows.Item(EnvDTE.Constants.vsWindowKindOutput);  
    w.Visible = true;  
    OutputWindow ow = (OutputWindow)w.Object;  
    OutputWindowPane owp = ow.OutputWindowPanes.Add("Value property: ");  
    owp.Activate();  

    EnvDTE.Expression exp = dte.Debugger.GetExpression("tempC", true, 1);  
    owp.OutputString("\nThe name of the expression: " + exp.Name);  
    owp.OutputString("\nThe type of the expression: " + exp.Type);  
    owp.OutputString("\nThe value of the expression: " + exp.Value);  
}

Thus, it should be by-design we are not able to get ‘{size=5}’ from the Immediate Window here: enter image description here

Regarding “the display string defined in the visualizer file (.natvis)” , DisplayString should be the property customized, could you provide your visualizer file (.natvis), or a sample project which can reproduce the issue?