The Old New Thing

WM_KILLFOCUS is the wrong time to do field validation

"I'll do my field validation when I get a WM_KILLFOCUS message." This is wrong for multiple reasons. First, you may not get your focus loss message until it's too late. Consider a dialog box with an edit control and an OK button. The edit control validates its contents on receipt of the WM_KILLFOCUS message. Suppose the user fills in ...
Comments are closed.0 0
Code

Mapping all those "strange" digits to "0" through "9"

In an earlier article, I discussed how the Char.IsDigit() method and its Win32 counterpart, GetStringTypeEx report things to be digits that aren't just "0" through "9". If you really care just about "0" through "9", then you can test for them explicitly. For example, as a regular expression, use [0-9] instead of \d. Alternatively, for a ...
Comments are closed.0 0
Code

Not all short filenames contain a tilde

I'm sure everybody has seen the autogenerated short names for long file names. For the long name "Long name for file.txt", you might get "LONGNA~1.TXT" or possibly "LO18C9~1.TXT" if there are a lot of collisions. What you may not know is that sometimes there is no tilde at all! Each filesystem decides how it wants to implement short ...
Comments are closed.0 0
Code

Unicode collation is hard

The principle of "garbage in, garbage out" applies to Unicode collation. If you hand it a meaningless string and ask to compare it to another meaningless string, you get meaningless results. I am not a Unicode expert; I just play one on the web. A real Unicode expert is Michael Kaplan, whose explanation of how comparing invalid ...
Comments are closed.0 0
Code

The random number seed can be the weakest link

Random number generation is hard. That's why you should leave it to the experts. But even if you choose a good random number generator, you still have to seed it properly. The best random number generator in the world isn't very useful if people can guess the seed. That's why seeding the random number generator with the current ...
Comments are closed.0 0
Code

Where does the taskbar get grouped button titles from?

If the "Group similar taskbar buttons" box is checked (default) and space starts to get tight on the taskbar, then then the taskbar will group together buttons represending windows from the same program and give them a common name. Where does this common name come from? The name for grouped taskbar buttons comes from the version resource...
Comments are closed.0 0
Code

Reference counting is hard.

One of the big advantages of managed code is that you don't have to worry about managing object lifetimes. Here's an example of some unmanaged code that tries to manage reference counts and doesn't quite get it right. Even a seemingly-simple function has a reference-counting bug. The point of this function is to take a (pointer to) a ...
Comments are closed.0 1
Code

Regular expressions and the dreaded *? operator

The regular expression *? operator means "Match as few characters as necessary to make this pattern succeed." But look at what happens when you mix it up a bit: ".*?" This pattern matches a quoted string containing no embedded quotes. This works because the first quotation mark starts the string, the .*? gobbles up everything in ...
Comments are closed.0 0
Code

Why an object cannot be its own enumerator

I've seen people using the following cheat when forced to implement an enumerator: Why create a separate enumerator object when you can just be your own enumerator? It's so much easier. And it's wrong. Consider what happens if two people try to enumerate your formats at the same time: The two enumerators are really the same enumerator...
Comments are closed.0 0
Code

What is the default security descriptor?

All these functions have an optional LPSECURITY_ATTRIBUTES parameter, for which everybody just passes NULL, thereby obtaining the default security descriptor. But what is the default security descriptor? Of course, the place to start is MSDN, in the section titled Security Descriptors for New Objects. It says that the default DACL comes ...
Comments are closed.0 0
Code