I learned a new Visual Studio trick today. I’m currently working on a hobby project in C++ and since the plan is to make it cross platform I’m using the excellent Juce framework.
Juce has its own string type which is very nice. It handles Unicode, reference counting and so on. Its like the CString type but cross platform. Some people may prefer the standard lib string std::string but personally I think the std lib is a bit awkward.
Anyway, so what’s my problem then? Well, the thing is that when you debug your program in Visual Studio you really want to see the contents of strings easily. By default you get this when using the juce::String type:
Visual Studio has no knowledge about the juce::String type so it just takes a guess at what we want to see and shows us some of the private members in the type. Not very helpful…
Autoexp.dat
So what can we do about it? Well I found some info about a little file containing debugger settings for Visual Studio. It’s default location is under C:\Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger and the file is called autoexp.dat. This file controls how VS displays different types during debugging. Interesting!
If you are running Vista/Win7, make sure you edit autoexp.dat as administrator because it’s protected since it’s in your program files. Otherwise you end up writing to a file that gets placed in your VirtualStore and it won’t work (yes, I did this myself..doh!)
Note that you can change the autoexp.dat file anytime. It will be reloaded when the debugger starts, so you don’t have to restart VS.
Now lets see what we can do… Open up the file autoexp.dat and scroll down a bit to a section called [Visualizer]. This section controls the VS visualizers for STL, ATL and other stuff. This part has a large DO NOT MODIFY text so we won’t touch that part (make a backup if you’re scared…)
Just below the part with the STL and ATL container stuff add the following:
juce::String{
preview ([$e.text->text,su])
stringview ([$e.text->text,sub])
}
This will tell VS about the Juce string type and explain how we want to view it. The ‘preview’ bit controls what to show in the watch window and the ‘stringview’ bit controls the popup string viewer that allows you to view the string as text, XML or HTML. Very handy feature!
The expression refers to the variable being viewed as $e and then you simply access its members as you would in normal code watch expressions. The ‘su’ part means that the value should be treated as a null terminated string in Unicode format. The ‘sub’ means that the string should be shown using “bare representation”. In this case it will display the string without the surrounding quotes (I only use this in the string viewer, as this seems to be the common practice).
Here is a screenshot of how it looks now:
Neato! Now we see the strings easily while debugging. We can also click the magnifying glass to examine the string in a popup window, even show it as formatted XML. Now the Juce type is even more juicy! Great isn’t it?
These debugger expressions can do a lot more. You can read more it here: http://www.virtualdub.org/blog/pivot/entry.php?id=120
Happy coding!
Leave a Reply