When developing a debugger using Visual Studio, we often need to open the View window (Watch) to parse the variables. Sometimes the content displayed in the View window is not very intuitive. In order to get more information from the variables in the view window, we need some small tricks. Here are a few examples.
1. Windows messages
We often need to look at the contents of a message when developing a debugging interface program. When in the viewing window, only the integer value corresponding to the message is displayed, we cannot intuitively know what the message is. Like what:
In order for the viewing window to display the message content, we only need to add the ", WM" format suffix to the variable name to display the name of the message:
2. handle return value
Windows has many APIs that return a value of type handle to indicate whether the operation was successful. When the operation fails, a specific value is returned to indicate why the operation failed. It is often impossible to remember all the error codes corresponding to the failure type. For example, in the following example, we do not necessarily see the cause of the error that 0x80070005 represents:
To make it easy for us to find the cause of the error in the wrong code of the handle, Visual Studio provides an ", hr" suffix in the view window. Add the suffix after the variable name, and the viewing window will show you the information that is readable. For example, after we add ", hr" after the above return value, we can get:
From the name of the handle value, it is possible that there is a problem with access permissions.
3. Error code
There are many APIs in Windows where an error code is set when the operation fails, and the programmer can call the function GetLastError to get the error code. When debugging, if the code does not call GetLastError, then we can not easily get to the error code.
Since this error code is set in a register called $err. We can get the error code by displaying the value of the Register in the view window. For example, we run the following code:
HANDLE hfile = CreateFile (_t ("Temp2.txt"), Generic_read, 0, NULL,
Open_existing, File_attribute_normal, NULL);
If we have not created a new Temp2.txt file before, then we can get the following error code:
We may not be able to see the cause of the error from the 0x0002 code. With the experience of the previous handle, we can add the ", hr" suffix later, at which point the View window appears as:
Now we know that the reason for the error is that the system cannot find the file.
4. Array
Arrays are one of the most frequently used data structures. However, when an array is represented by a pointer and its length, the view window does not visually display the value of each element in the array, but only the starting address of it and its first element. Here's an example:
In order to display the contents of all the elements in the array, we can add a ", #" (#表示数组的长度) after the names of the arrays. If we add the suffix name to the array name above, we get:
Debugging Visual Studio Debugging Tips (2)-Get more information from the viewing window (reprint)