The Old New Thing

Practical development throughout the evolution of Windows.

Latest posts

Using the MNS_DRAGDROP style: Dragging out
Dec 28, 2011
Post comments count 0
Post likes count 0

Using the MNS_DRAGDROP style: Dragging out

Raymond Chen
Raymond Chen

Windows 2000 introduced the menu style, which permits drag/drop operations in a menu. Nobody uses this style, probably because it's totally undiscoverable by the end-user. But I'll write a sample program anyway. Mind you, I knew nothing about the menu style until I started writing this entry. But I simply read the documentation, which says that if you set this style, you will receive and messages. The message is sent when the user drags a menu item, so let's go with that first. The documentation says that you get information about the item that was dragged, and then you return a code that specifies w...

Introducing the for-if anti-pattern
Dec 27, 2011
Post comments count 0
Post likes count 0

Introducing the for-if anti-pattern

Raymond Chen
Raymond Chen

Over the years, I've seen a bunch of coding anti-patterns. I figured maybe I'll share a few. Today, I'll introduce what I'm calling the for-if anti-pattern, also known as "We'll sell you the whole seat, but you'll only need the edge." This is a special case of the for-case anti-pattern, where all but one of the cases is null. This can naturally be simplified to The for-if anti-pattern arises in many forms. For example: This enumerates all the files in a directory looking for a specific one, and if it's found, it returns a stream on it. The slightly-less-crazy version would be Note that both version...

Celebrating the end of the gluttony season, but the effects linger
Dec 26, 2011
Post comments count 0
Post likes count 0

Celebrating the end of the gluttony season, but the effects linger

Raymond Chen
Raymond Chen

The Washington State Ferry system has reduced the rated carrying capacity of its fleet because people have gotten fatter: The average weight of an adult passenger has been officially revised from 160 pounds to 185 pounds. (That's from 11 stone 6 to 13 stone 3 in the UK, or from 73kg to 84kg for the rest of the world.) This has happened before: In 1999, the rated capacity of Washington State ferries dropped when the previous method for determining seating density was abandoned due to passengers' big butts. (I recall that The Seattle Times printed a ruler next to the article so that for readers could assess th...

Why is the file size reported incorrectly for files that are still being written to?
Dec 26, 2011
Post comments count 0
Post likes count 0

Why is the file size reported incorrectly for files that are still being written to?

Raymond Chen
Raymond Chen

The shell team often gets questions like these from customers: Attached please find a sample program which continuously writes data to a file. If you open the folder containing the file in Explorer, you can see that the file size is reported as zero. Even manually refreshing the Explorer window does not update the file size. Even the command shows the file size as zero. On the other hand, calling reports the correct file size. If I close the file handle, then Explorer and the command both report the correct file size. We can observe this behavior on Windows Server 2008 R2, but on Windows Server 2003, the fi...

How do I get the full path for the target of a shortcut file?
Dec 23, 2011
Post comments count 0
Post likes count 0

How do I get the full path for the target of a shortcut file?

Raymond Chen
Raymond Chen

A customer was having trouble obtaining information from a shortcut file. "Here is a sample program that tries to print the target of a shortcut file, but it only gets the file name without a directory. How do I get the full path?" Recall that the structure contains only a file name in the member. It doesn't have any path information. The structure was originally created for the function, and you already know the directory you are searching in because you passed it to . But we're not using the structure in conjunction with , so where do I get the directory from? In the customer's excitement over the ...

How do I determine programmatically whether a particular language is LTR or RTL?
Dec 22, 2011
Post comments count 0
Post likes count 0

How do I determine programmatically whether a particular language is LTR or RTL?

Raymond Chen
Raymond Chen

Given an , how does one determine whether the language lays out left-to-right or right-to-left? One suggestion was simply to hard-code the list of known right-to-left languages, and if the language isn't on the list, then assume that it is left-to-right. This technique is clearly fragile, because Windows adds support for new languages not infrequently, and if one of those is a right-to-left language, then your table is now out of date. And besides, there are languages whose layout is neither left-to-right nor right-to-left. For example, Chinese and Japanese traditionally lay out top-to-bottom. To obtain the text...

Deftly solving compatibility problems by withholding information
Dec 21, 2011
Post comments count 0
Post likes count 0

Deftly solving compatibility problems by withholding information

Raymond Chen
Raymond Chen

One of the continuing compatibility problems that plagued Direct3D was the way it reported texture formats. Historically, the way an application checked which texture formats were available was by calling and passing a callback function which is called once for each supported format. The application's callback made some sort of decision based on the information it received. The problem was that any time a new format was added, a bunch of programs ended up not working. Either the new format confused them, or the change in the order of the formats violated some assumption. For example, they may have assumed that i...

Microspeak: Offline (noun)
Dec 20, 2011
Post comments count 0
Post likes count 0

Microspeak: Offline (noun)

Raymond Chen
Raymond Chen

Sure, any noun can be verbed, and any verb can be nouned. But today, we're going to noun an adjective. I have no written citations of this usage; the only report was via a colleague who overheard it in a hallway conversion. I had some offlines with Fred about that. In Microspeak, offline is an adjective which means "outside this meeting." In order to keep a meeting on track, the meeting organizer may advise the people engaged in the discussion of a side topic or a topic of limited interest to take it offline, please, meaning discuss this amongst yourselves after the meeting, please. In other words, "Let's not...

Paint messages will come in as fast as you let them
Dec 19, 2011
Post comments count 0
Post likes count 0

Paint messages will come in as fast as you let them

Raymond Chen
Raymond Chen

There is a class of messages which are generated on demand rather than explicitly posted into a message queue. If you call or and the queue is empty, then the window manager will look to see if one of these generated-on-demand messages is due, messages like , , and . Neil wonders, "In that program that called 100,000 times, how many paint messages were generated?" The Zen answer to this question is "Yes." A more practical answer is "As many as you can get." When somebody calls , the window manager adds the specified rectangle to the window's invalid region (or invalidates the entire client area if no recta...