The Old New Thing
Practical development throughout the evolution of Windows.
Latest posts
Our code needs to run on multiple platforms with different rules, so we follow none of them!
A customer was encountering sporadic crashes in their 64-bit application, and upon investigation, the problem was traced to a misaligned RSP register. We saw some time ago that the Windows x64 calling convention requires the RSP register to be 16-byte aligned. The customer traced the source of the misalignment to a third-party library they were using. They contacted the vendor, who acknowledged that they were not following the Windows x64 calling conventions, but explained that their code needs to run on multiple x64 operating systems, and since each operating system has different calling conventions, they ad...
If you protect a write with a critical section, you may also want to protect the read
It is common to have a critical section which protects against concurrent writes to a variable or collection of variables. But if you protect a write with a critical section, you may also want to protect the read, because the read might race against the write. Adam Rosenfield shared his experience with this issue in a comment from a few years back. I'll reproduce the example here in part to save you the trouble of clicking, but also to make this entry look longer and consequently make it seem like I'm actually doing some work (when in fact Adam did nearly all of the work): There is a race condition here: ...
Things I've written that have amused other people, Episode 8
In a technical discussion, I opened a reply with Bob's paper which I haven't yet read points out that... Some people wrote to me to say that the've added this quote to their file in the hopes of being able to use it themselves someday. For those who are curious, I found the technical discussion in question. It had to do with whether the following code is thread-safe: Question: Can this code legitimately print ? Surprisingly, the answer is yes!
Why is CLIPFORMAT defined to be a WORD rather than a UINT?
Commenter Ivo wants to know if the function returns a , why is the data type defined to be a ? Since a is smaller than a , you have to stick in a cast every time you assign the result of to a . Rewind to 16-bit Windows. Back in those days, a and a were the same size, namely, 16 bits. As a result, people got lazy about the distinction. Six of one, a half dozen of the other. (People are lazy about this sort of distinction even today, assuming for example that and are the same size, and in turn forcing to remain a 32-bit integer type even on 64-bit Windows.) The function came first, and when the OL...
How to insert a large number of items into a treeview efficiently
Just a quick tip today. If you need to insert a large number of items into a treeview, like tens of thousands, then it's much more efficient to insert them "backwards". (I'm ignoring for now the usability question of having a treeview that large in the first place.) In other words, instead of do it this way: Why is backwards-insertion faster? It has to do with the annoying parameter. To validate that the parameter is valid, the treeview needs to verify that the is a valid child of the , and this is done by walking the parent's children looking for a match. The sooner you find the match, the faster the...
How can I extend the deadline for responding to the PBT_APMSUSPEND message?
A customer observed that starting in Windows Vista, the deadline for responding to the message was reduced from twenty seconds to two seconds. Their program needs more than two seconds to prepare for a suspend and they wanted to know how they could extend the deadline. The program communicates with a device, and if they don't properly prepare the device for suspend, it has problems when the system resumes. No, you cannot extend the deadline for responding to the message. The two second deadline is hard-coded; it is not configurable. The whole point of reducing the deadline from twenty to two seconds is to en...
It is not unreasonable to expect uninitialized garbage to change at any time, you don't need to ask for an explanation
A customer admitted that they had a bug in their code: One bug in the above code is in the final parameter passed to : It's supposed to be the count in bytes, but the calculation appends only one byte for the terminating null instead of a full . In other words, it should be For concreteness, let's say the original string was five s in length, not counting the terminating null. Therefore, the correct buffer size is 12 bytes, but they passed only 11 to . This error is compounded in the code that reads the value back: The code happily divides without checking that the division is even. In our example, the c...
The Control Panel search results understand common misspellings, too
Here's a little trick. Open your Start menu and type scrensaver into the search box. That's right, spell it with only one e. Hey, it still found the Control Panel options for managing your screen saver. If you enable Improve my search results by using online Help in Windows Help and Support, this sends your search query to a back-end server to see if there's updated online help content related to your search. And the people who develop the online help content look over those queries to see if, for example, there is a category of issues people are searching for help with and not finding anything. It also means ...
Why not use animated GIFs as a lightweight alternative to AVIs in the animation common control?
Commenter Vilx- wondered why animated GIFs weren't used as the animation format for the shell animation common control. After all, "they are even more lightweight than AVIs." Animated GIFs are certainly more lightweight than general AVIs, since AVI is just a container format, so decoding a general AVI means decoding any encoding format invented now or in the future. On the other hand, the shell common control imposed enough limits on the type of AVIs it could handle to the point where what was left was extremely lightweight, certainly much more lightweight than an animated GIF. Think about it: To use an animat...