Showing tag results for Code

Oct 14, 2010
Post comments count0
Post likes count0

The memcmp function reports the result of the comparison at the point of the first difference, but it can still read past that point

Raymond Chen
Raymond Chen

This story originally involved a more complex data structure, but that would have required too much explaining (with relatively little benefit since the data structure was not related to the moral of the story), so I'm going to retell it with double null-terminated strings as the data structure instead. Consider the following code to compare tw...

Code
Oct 13, 2010
Post comments count0
Post likes count0

How do I get the color depth of the screen?

Raymond Chen
Raymond Chen

How do I get the color depth of the screen? This question already makes an assumption that isn't always true, but we'll answer the question first, then discuss why the answer is wrong. If you have a device context for the screen, you can query the color depth with a simple arithmetic calculation: Now that you have the answer, I'll explain why...

Code
Oct 8, 2010
Post comments count0
Post likes count0

Why does TaskDialog return immediately without showing a dialog? – Answer

Raymond Chen
Raymond Chen

Last time, I left an exercise to determine why the function was not actually displaying anything. The problem had nothing to do with an invalid window handle parameter and had all to do with original window being destroyed. My psychic powers told me that the window's handler called . As we learned some time ago, quit messages cause modal loop...

Code
Oct 8, 2010
Post comments count0
Post likes count0

Why does my asynchronous I/O request return TRUE instead of failing with ERROR_IO_PENDING?

Raymond Chen
Raymond Chen

A customer reported that their program was not respecting the flag consistently: My program opens a file handle in mode, binds it to an I/O completion callback function with , and then issues a against it. I would expect that the returns and returns , indicating that the I/O operation is being performed asynchronously, and that the completio...

Code
Oct 7, 2010
Post comments count0
Post likes count0

Why does TaskDialog return immediately without showing a dialog?

Raymond Chen
Raymond Chen

A customer reported a problem with the function. We've encountered a strange behavior in the function. A user reported that when exiting our application, our program played an error beep that didn't appear to be associated with an error. After investigating, we found that the sound is coming from our application trying to display an error dialo...

Code
Oct 1, 2010
Post comments count0
Post likes count0

Non-psychic debugging: Why you're leaking timers

Raymond Chen
Raymond Chen

I was not involved in this debugging puzzle, but I was informed of its conclusions, and I think it illustrates both the process of debugging as well as uncovering a common type of defect. I've written it up in the style of a post-mortem. A user reported that if they press and hold the F2 key for about a minute, our program eventually stops work...

Code
Sep 30, 2010
Post comments count0
Post likes count0

Why doesn't the TAB key work on controls I've marked as WS_TABSTOP?

Raymond Chen
Raymond Chen

A customer had a programming problem regarding tab stops: I create a parent window (child of main frame) as below This window hosts 2 toolbar windows. Each toolbar window has the style set using . MSDN states WS_EX_CONTROLPARENT Allows the user to navigate among the child windows of the window by using the TAB key. But I am not able ...

Code
Sep 24, 2010
Post comments count0
Post likes count0

How reliable is the BatteryLifePercent member of the SYSTEM_POWER_STATUS structure?

Raymond Chen
Raymond Chen

A customer was writing a program that called and used the value. The customer wanted to know whether a reported battery life percentage of 38% really means that the remaining battery life is between 37.5% and 38.5%. Although the value is reported to 1% precision, the accuracy in practice is much worse. Similarly, the is reported in seconds, bu...

Code
Sep 23, 2010
Post comments count0
Post likes count0

You must flush GDI operations when switching between direct access and GDI access, and direct access includes other parts of GDI

Raymond Chen
Raymond Chen

A customer was running into problems when accessing the pixels of a DIB section. They used the parameter to and created two bitmaps from the same underlying memory. Those two bitmaps were then selected into corresponding DCs, and the customer found that changes to the pixels performed by writing via one DC were not visible when read from the oth...

Code