问题
I have a mixed-mode project which contains both C++ and C# code. Character Set is UNICODE. Following string literal is defined somewhere in the C++ code.
LPCTSTR lpctszRegKey = _T( "SOFTWARE\\MICROSOFT\\KEYBOARD\\");
When I select "Native Only" as the 'Local Windows Debugger', following is displayed in the Watch window.
lpctszRegKey 0x01054134 L"SOFTWARE\\MICROSOFT\\KEYBOARD\\" const wchar_t *
When I select "Mixed" as the 'Local Windows Debugger', following is displayed in the Watch window.
lpctszRegKey 0x00274134 "SOFTWARE\MICROSOFT\KEYBOARD\" const wchar_t *
Note that, in the first case it shows double backslashes (i.e. \\) and in the second case it only shows single backslash (i.e. \) which are present in between the string literal.
How come escape sequence \ displayed in the case of "Native Only" debugger and not in the case of "Mixed" debugger?
回答1:
The debugger makes an effort to display strings in the syntax that matches the language that was used to write the program. The string does not really have double backslashes of course, something you can see from the text visualizer (click the spy-glass icon) or the memory view (Debug + Windows + Memory + Memory 1).
To make that work, it of course needs to know the language the program was written in. This is a lot harder than it looks, there is no metadata in the executable file that allows it to guess correctly. Not included in the PDB file either. It punts at the obvious approach, it formats based on the type of project that's being debugged. So a C# or C++ programmer always sees the double backslashes, a VB.NET programmer does not since that language has very different rules for string literals.
When you change the Debugger Type to mixed then you'll get two debugging engines, one for native code and one for managed code. Now the source language choice gets very murky, all that the debugger knows is that there's code that is not written in C or C++. But cannot tell which, a .NET assembly looks the same no matter what language compiler generated it. Nor can it tell which one is more "important".
Being forced to guess with significant odds that it will guess wrong, it does the logical thing when you cannot make the right choice. You make everybody equally unhappy. You see the string as-is, the way it is displayed in the text visualizer, without any escapes.
来源:https://stackoverflow.com/questions/26359937/vs-2012-debugger-different-display-of-string-literals-having-escape-sequenc