Tips for Visual Studio debugging

Source: Internet
Author: User
Tags 0x80070005

By http://blog.csdn.net/cadcisdhht/article/details/5651488

When using Visual Studio to debug programs, we often need to add breakpoints Based on numerical values. For example, to interrupt the running when I = 2, we can add the following breakpoint:

In some cases, we need to add breakpoints based on the content of the string. For example, we are debugging the following code:

Void cvsdebugtricksdlg: onbnclickedbutton1 ()

{

Char * string;

For (INT I = 0; I <5; ++ I ){

String = getstring (I );

Trace (string );

}

}

Char * cvsdebugtricksdlg: getstring (INT number)

{

Switch (number ){

Case 0:

Return "zero ";

Case 1:

Return "one ";

Case 2:

Return "two ";

Default:

Return "other ";

}

}

We hope that the running will be interrupted if the string is "two" when it is run to trace (string. If we add a breakpoint in the same way as adding a breakpoint based on a numeric value, add the following breakpoint:

When we run the above Code, we will find that even if the string content is "two", the operation is not interrupted. This is because the = Operator compares the addresses of two strings rather than the content, so the above breakpoint cannot meet our needs.

Visual Studio allows programmers to add breakpoints based on the content of a string. Therefore, it provides special support for adding breakpoints. When adding breakpoints Based on the string content, you can use strcmp and other functions to set breakpoints.

In the above example, we can use the strcmp function to add the following breakpoint:

The string functions supported by the breakpoint adding function in Visual Studio include strlen, wcslen, strnlen, wcsnlen, strcmp, wcscmp, _ stricmp, _ wcsicmp, strncmp, wcsncmp, _ strnicmp, _ wcsnicmp, strchr, wcschr, strstr, wcsstr.

 

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:

By http://blog.csdn.net/cadcisdhht/article/details/5651488

When using Visual Studio to debug programs, we often need to add breakpoints Based on numerical values. For example, to interrupt the running when I = 2, we can add the following breakpoint:

In some cases, we need to add breakpoints based on the content of the string. For example, we are debugging the following code:

Void cvsdebugtricksdlg: onbnclickedbutton1 ()

{

Char * string;

For (INT I = 0; I <5; ++ I ){

String = getstring (I );

Trace (string );

}

}

Char * cvsdebugtricksdlg: getstring (INT number)

{

Switch (number ){

Case 0:

Return "zero ";

Case 1:

Return "one ";

Case 2:

Return "two ";

Default:

Return "other ";

}

}

We hope that the running will be interrupted if the string is "two" when it is run to trace (string. If we add a breakpoint in the same way as adding a breakpoint based on a numeric value, add the following breakpoint:

When we run the above Code, we will find that even if the string content is "two", the operation is not interrupted. This is because the = Operator compares the addresses of two strings rather than the content, so the above breakpoint cannot meet our needs.

Visual Studio allows programmers to add breakpoints based on the content of a string. Therefore, it provides special support for adding breakpoints. When adding breakpoints Based on the string content, you can use strcmp and other functions to set breakpoints.

In the above example, we can use the strcmp function to add the following breakpoint:

The string functions supported by the breakpoint adding function in Visual Studio include strlen, wcslen, strnlen, wcsnlen, strcmp, wcscmp, _ stricmp, _ wcsicmp, strncmp, wcsncmp, _ strnicmp, _ wcsnicmp, strchr, wcschr, strstr, wcsstr.

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:

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.