When developing and debugging programs using Visual Studio, we often need to open the Watch window to analyze variables. Sometimes the content displayed in the View window is not intuitive. To get more information from window variables, we need some tips. The following are examples.
1.WindowsMessage
When developing and debugging interface programs, we often need to view the message content. In the View window, only the integer corresponding to the message is displayed, so we cannot intuitively know what the message is. For example:
To display the message content in the display window, you only need to add the format suffix ", WM" to the variable name to display the message Name:
2.Handle Return Value
In Windows, many APIs return a handle value to indicate whether the operation is successful. When an operation fails, a specific value is returned to indicate the cause of the operation failure. Generally, we cannot remember the failure type corresponding to the error code. For example, in the following example, we cannot see the cause of the error indicated by 0x80070005:
To help us intuitively find the cause of the error from the handle error code, Visual Studio provides a suffix ", HR" in the View window. Add the suffix after the variable name to display readable information in the window. For example, after adding ", HR" after the preceding return value, we can get:
According to the name of the handle value, the access permission may be faulty.
3.Error Code
In Windows, many APIs set an error code when the operation fails. The programmer can call the getlasterror function to obtain the error code. During debugging, if the Code does not call getlasterror, we cannot easily obtain the error code.
This error code is set in a register named $ err. We can display the value of this register in the View window to get the error code. For example, run the following code:
Handle hfile = createfile (_ T ("temp2.txt"), generic_read, 0, null,
Open_existing, file_attribute_normal, null );
If we haven't created the temp2.txt 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 previous handling experience, we can add the suffix ", HR" to the end. The display window is as follows:
Now we know that the cause of the error is that the system cannot find the file.
4.Array
Arrays are the most frequently used data structures. However, when an array is represented by a pointer and its length, viewing the window does not visually display the values of each element in the array, the starting address of the array and its first element can only be displayed. The following is an example:
To display the content of all elements in the array, we can add a ", #" (# indicates the length of the array) after the array name ). If we add the suffix to the array name above, we will get: