Solution for Imitating Assert Behavior of Wince in WINXP

Source: Internet
Author: User

 
// ================================================ ====================================
// Title:
// A solution that imitates the Assert Behavior of Wince in the winxp Environment
// Author:
// Norains
// Date:
/// Tuesday 23-February-2010
// Environment:
// Windows CE 5.0
// Windows XP
// ================================================ ====================================

 

If you have developed an wince program, you must be familiar with the use of the assert macro. To put it simply, this macro takes effect only in the debug version. When its expression is false, the debugger automatically stops in this code segment and outputs the corresponding information in the output window:

 

If you put assert (false) in the MFC project of WINXP, you may be taken to hell if you are used to the method of Wince assertions. With a bang, the warning message is displayed:

You just wrote a program in WINXP. Maybe you saw this dialog box and thought your code had a fatal error. Although we can click "Ignore" to continue debugging, if there are more than a few dialog boxes, I believe few people can endure it.

 

If you do not use MFC, but directly use the Win32 API, the final result will be even more outrageous. You cannot even pass the compilation and will directly prompt that the Asser is not defined.

 

Check the document. Win32 does not have the assert option, but it is replaced by an underscore (_ assert) before the macro. However, the underlined expression is the same as the assert of MFC, which will speed up your heartbeat:

Okay, okay. Basically, we can make sure that _ assert and assert are the same thing. But if we want to get the expression in wince, isn't it the same?

 

Before discussing this issue, let's take a look at the definition of assert under wince.

# Define assert (exp) dbgchk (text ("unknown"), exp) </P> <p> # define dbgchk (module, exp) /<br/> (void) (exp )? 1 :(/<br/> nkdbuplintfw (text ("% s: debugchk failed in file % s at line % d/R/N"),/<br/> (lpwstr) module, text (_ file _), __line _),/<br/> debugbreak (),/<br/> 0/<br/> ))) <br/>

 

In the final analysis, assert in wince uses two functions: nkdbuplintfw and debugbreak. It is easy to know that nkdbuplintfw is a function that outputs information to the output window, while debugbreak is used to pause the debugger.

 

Because nkdbuplintfw is a unique function of wince, Let's ignore it first and look at debugbreak. Fortunately, this function is also defined in WINXP. Then we put it into the code to see what it is.

 

When the code is executed to debugbreak (), the debugger also jumps out of a dialog box:

In this dialog box, if break is selected, debugging of the assembly code will be performed. If it is continue, the debugger will enter the next statement.

 

Although there is no silent method of wince, it is much better than the red "X", and there is also a sound assert?

 

The pause of the debugger is an imperfect solution. What should I do if I output the information in the output window? Nkdbuplintfw is the privilege of wince. WINXP does not offer this privilege. At wince, we can still use printf to output to the output window, but with the same call, printf can be hard to find in the output window under WINXP.

 

Search for the document and find that if you want to input the string to the output window in WINXP, you can call outputdebugstring. It seems that the problem can be solved, but outputdebugstring does not support string formatting!

 

In other words, if you replace the code printf ("Param: % d/R/N", ival) with outputdebugstring ("Param: % d/R/N ", ival), so you will not be able to compile it, Because outputdebugstring only supports one form parameter!

 

Isn't it possible for us to implement an Assert macro similar to wince in WINXP?

 

No. Since there is no corresponding formatting output function, can we just create one by ourselves?

 

Initially, there are two difficulties in the entire function: variable parameters and string formatting. In fact, both of these difficulties can be solved in wvsprintf. It can be formatted as a string based on the variable parameters, and then saved to a specific buffer. The buffer size, according to the document, is 1024. Therefore, we can write the function output to the output window:

Void debugstrng (tchar * pszformat ,...) <br/>{< br/> // The maximum size of the buffer in the wvsprintf is 1024 bytes. <br/> tchar sztext [1024]; </P> <p> // The argument pointer. <br/> va_list ARGs; </P> <p> _ Try <br/> {<br/> // start with the pstrformat variable address <br/> va_start (ARGs, pszformat ); </P> <p> // format the string <br/> wvsprintf (sztext, pszformat, argS ); </P> <p> // send the string to the output window <br/> outputdebugstring (sztext ); <br/>}< br/> _ finally <br/>{< br/> // stop enumerating the argument. <br/> va_end (ARGs); <br/>}< br/>

 

If the output function is available, will the next thing be much simpler?

 

Finally, our own assert macro was born:

# Define dbgchk (module, exp)/<br/> (void) (exp )? 1 :(/<br/> debugstrng (text ("% s: debugchk failed in file % s at line % d/R/N"),/<br/> (lpwstr) module, text (_ file _), __line _),/<br/> debugbreak (),/<br/> 0/<br/> ))) </P> <p> # ifdef _ debug <br/> # define assert (exp) dbgchk (text ("unknown"), exp) <br/> # else <br/> # define assert (exp) (void) (exp) <br/> # endif

 

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.