The Old New Thing
Practical development throughout the evolution of Windows.
Latest posts

A feature I didn't even know existed much less had a name: Color hot-track

I hadn't even noticed this until somebody pointed it out: When you hover your mouse over a button in the Windows 7 taskbar which corresponds to a running application, the taskbar button lights up in a color that matches the colors in the icon itself. (And even more subtly, the lighting effect is centered on the mouse.) This feature even has a name: Color hot-track. (Gentlemen, start your photocopiers.) Some people ask how it's done. It's really nothing special. The code just looks for the predominant color in the icon. (And, since visual designers are sticklers for this sort of thing, black, white, and...

Why does my program still show up in the Programs and Features folder after my uninstaller exits?

A customer reported that they were having problems with their program entry in the Programs and Features folder (formerly known as Add and Remove Programs, formerly known as Add/Remove Programs). When the user goes to the Programs and Features folder, selects their program, and clicks Uninstall, the uninstaller runs, but after the uninstaller exits, their entry remains in Programs and Features. They have to manually refresh the folder to get it to disappear. Why won't the item disappear? The Programs and Features folder launches your uninstaller and waits for it to exit, at which point the Programs and Features ...

Don't let more than one process try to read from stdin at the same time

A customer reported a problem with a program that ran a series of other programs in parallel. We have a main program (call it main.exe) that runs a bunch of child processes with stdout and stderr redirected. (We are not redirecting stdin.) We've found that some of the child processes get stuck inside the C runtime startup code on a call to on the stdin handle. What could be the reason for this? Is there something we can do that doesn't require us to modify the child processes? (They are third party code we do not have control over.) This is one of those once you've debugged this problem you never forget it ty...

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...