Visual Studio Debugging Tips (2)-Get more information from the view window

Source: Internet
Author: User

Original: Http://blog.csdn.net/cadcisdhht/archive/2010/06/06/5651488.aspx

When developing a debugger using Visual Studio, we often need to open the View window (Watch) to parse variables. Sometimes the content displayed in the viewing window is not very intuitive. In order to get more information from the variables that look at the window, we need some small tips. Here are a few examples.

1. Windows messages

When developing a debugging interface program, we often need to look at the contents of the message. When in the viewing window, only the integer value of the message is displayed, we do not have an intuitive idea of 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 in 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 not usually possible to remember the type of failure that corresponds to all the error codes. For example, in the following example, we may not be able to see the cause of the error that 0x80070005 represents:

Visual Studio provides an ", hr" suffix in the viewing window in order to visually identify the cause of the error in our handle's error code. Add the suffix after the variable name, and view the window to display the readable information. For example, after adding ", hr" after the above return value, we can get:

From the name of the value of the handle, it is likely that the access permissions have been problematic.

3. Error code

There are many APIs in Windows that will set an error code when the operation fails, and the programmer can call the function GetLastError to get the error code. When debugging, if GetLastError is not invoked in the code, then we cannot easily get to the error code.

Because this error code is set in a register called $err. We can display the value of the Register in the view window and get the error code. 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 did not create a new temp2.txt this file before, we can get the following error code:

We may not see the cause of the error from the error code 0x0002. With the experience of the previous handle, we can add the ", hr" suffix later, and the View window appears as follows:

Now we know the reason for the error is that the system cannot find the file.

4. Array

Arrays are one of the most frequent data structures we use. However, when an array is represented as 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 the set 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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.