Showing results for Code - The Old New Thing

Oct 28, 2010
Post comments count0
Post likes count0

Why is there an LVN_ODSTATECHANGED notification when there's already a perfectly good LVN_ITEMCHANGED notification?

Raymond Chen
Raymond Chen

If you work with owner-data listviews, you take the responsibility for managing the data associated with each item in the list view. The list view control itself only knows how many items there are; when it needs information about an item, it asks you for it. It's the fancy name for a "virtual list view" control. When you use an ownerdata list vi...

Code
Oct 25, 2010
Post comments count0
Post likes count0

When you call a function, your code doesn't resume execution until that function returns

Raymond Chen
Raymond Chen

Consider this code fragment: When calls , and has not yet returned, does continue executing? Does get called before returns? No, it does not. The basic structure of the C/C++ language imposes sequential execution. Control does not return to the function until returns control, either by reaching the end of the function or by an explic...

Code
Oct 20, 2010
Post comments count0
Post likes count0

How do I get the dimensions of a cursor or icon?

Raymond Chen
Raymond Chen

Given a or a , how do you get the dimensions of the icon or cursor? The function gets you most of the way there, returning you an structure which gives you the mask and color bitmaps (and the hotspot, if a cursor). You can then use the function to get the attributes of the bitmap. And then here's the tricky part: You have to massage the data...

Code
Oct 15, 2010
Post comments count0
Post likes count0

What does the FOF_NOCOPYSECURITYATTRIBS flag really do (or not do)?

Raymond Chen
Raymond Chen

In the old days, the shell copy engine didn't pay attention to ACLs. It just let the file system do whatever the default file system behavior was. The result was something like this: Perfectly logical, right? If a new file is created, then the security attributes are inherited from the container. If an existing file is moved, then its security a...

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