"Go" in MFC to view output information during debugging--good

Source: Internet
Author: User

Original URL: http://blog.sina.com.cn/s/blog_4e24d9c501014o39.html

Notes && easy access.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When debugging an MFC program, we often need to look at the output value of a particular location variable. Or when a particular condition is executed, an output identifier is given.

In general, there are 3 ways:

1) Call Trace (LPCTSTR Lpszformat, ...) Function

Using the trace function in MFC to print out results is very handy, and is similar to using the printf function in a console program. But there are a few.

Note: One, the output of the trace function is under the debug option in the Output window, and the output is only available when the debug version is debugged, if

Release version when debugging or running the program, you will not see the output.

========================================================================================================

1. Adding Trace statements to MFC

2. Select "ENABLE tracing" in Tools->mfc tracer to click OK

3. perform debug Run, GO (F5) (Special note: not execute '! ' I can't see the trace content before, because it's not debug execution, it's '! ', remember, remember.

4. The trace content is then seen in the Debug window in output, and debug execution automatically jumps from the build window to the Debug window, where you see the trace content, ^_^

The following is a detailed description of the trace found:

================================================================================================

Trace macro for the VC under the program debugging is very useful things, with a similar function of printf, the macro is only in the debug version of the program appears, when the release of the macro is complete message, thus helping you to adjust the release also reduce the amount of code.

The use is very simple and the format is as follows:

TRACE ("ddddddddddd");

TRACE ("wewe%d", 333);

There are also trace0,trace1,trace2 ... correspond to 0,1,2 respectively. A parameter

The trace information is output to the Output window of the VC IDE environment (which is the window you are prompted to compile the project error), but only to the program you are running your debug version of in the VC.

Trace information can also be captured using DebugView. In this case, you can not run your program in the VC IDE environment, and the build good debug version of the program run alone, this time in the DebugView window to see the output of the Debugvie format.

The use of trace in VC has the following four kinds:

1:

TRACE, which is a C-like printf ("output string") without a dynamic parameter output string;

2:

The string in TRACE can be output with a parameter, like C's printf ("...%d", variable);

3:

TRACE can take two parameter outputs, such as C's printf ("...%d...%f", variable 1, variable 2);

4:

TRACE can take three parameter outputs, such as C's printf ("...%d,%d,%d", variable 1, variable 2, variable 3);

TRACE macros are a bit like the printf function we used in C, which allows the program to output some debugging information during the run, so that we can understand some of the state of the program. But there's a difference:


The TRACE macro is output only in the debug state, and the previously used printf function has output in all cases. Like the printf function, the trace function can accept multiple parameters such as:

int x = 1;
int y = 16;
float z = 32.0;
Trace ("This is a TRACE statement\n");
TRACE ("The value of X is%d\n", x);
TRACE ("x =%d and y =%d\n", x, y);
TRACE ("x =%d and y =%x and z =%f\n", x, Y, z);

Note that the trace macro only works on the debug version of the project, and the trace macro is ignored in the release version of the project.

From: http://blog.csdn.net/jiang1013nan/article/details/4166017

========================================================================================================

2) Use the AfxMessageBox () function to output information

This function is also more commonly used in debugging, the use of simple, here do not introduce.

=============================================================================================================== ===

The difference between the MessageBox and the AfxMessageBox is outlined below.

1. The AfxMessageBox () function can be used in any class, and the MessageBox () function can only be used in the inheritance class of the CWnd class.

2. The parameters of the AfxMessageBox () function are not rich in the parameters of the MessageBox () function, so the latter is more flexible than the former.

3, AfxMessageBox can not control the message box title, often used to debug the internal data output or warning; MessageBox is more formal, often in the version of the application to be submitted, you can control the title content without having to use the unknown executable name as the title.

int iRes = MessageBox ("Do you see the message box?", "Test", mb_yesno| mb_iconwarning);

if (iRes = = Idyes)

MessageBox ("See!");

The use of AfxMessageBox

int AfxMessageBox (

LPCTSTR LpszText,

UINT NType = MB_OK,//default is 0

UINT nidhelp = 0

);


The type of string is LPCTSTR.

Workaround 1: Change line 4th to: if (AfxMessageBox (_t) ("Do you really want to exit the current program?"). "), Mb_yesno) ==idyes)

Workaround 2: Select the general character set, configuration properties, item properties, Project menu, and then change to not set.

I think we should try to solve this problem with the method!

You can change your string to a long byte type by adding _t in front of the string ....

LPCTSTR Type:

L represents a long pointer this is for compatibility with 16-bit operating systems such as Windows 3.1, and in Win32 and other 32-bit operating systems, the long pointer and the near pointer and the far modifier are all for compatibility purposes. No practical significance.

P indicates that this is a pointer

C means a constant.

T means that in a Win32 environment, there is a _t macro

STR indicates that this variable is a string

This macro is used to indicate whether your character uses Unicode, and if your program defines Unicode or other related macros, then the character or string will be used as a Unicode string, otherwise the standard ANSI string.

So LPCTSTR represents a string that points to a constant fixed address that can change semantics based on some macro definitions.

LPCTSTR = = Const TCHAR *

CString and LPCTSTR can be said to be universal . The reason for this is the automatic type conversion defined by CString, nothing fancy, the simplest C + + operator overloading.

LPSTR LPSTR = (LPSTR) (LPCTSTR) string;

Char buf[20];

AfxMessageBox (CString (BUF), 0,0);

========================================================================================================

3) direct the standard output to the console you created

#include "io.h"
#include "fcntl.h"

void Initconsole ()
{
int nret= 0;
file* FP;
AllocConsole ();
Nret= _open_osfhandle ((Long) GetStdHandle (Std_output_handle), _o_text);
fp = _fdopen (Nret, "w");
*stdout = *FP;
Setvbuf (stdout, NULL, _IONBF, 0);
}

Call this function where the MFC program was initialized, and use the console to view the print information for the printf function

"Go" in MFC to view output information during debugging--good

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.