Showing tag results for Code

Apr 12, 2004
Post comments count0
Post likes count0

The random number seed can be the weakest link

Raymond Chen
Raymond Chen

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

Code
Apr 8, 2004
Post comments count0
Post likes count0

Where does the taskbar get grouped button titles from?

Raymond Chen
Raymond Chen

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

Code
Apr 6, 2004
Post comments count0
Post likes count2

Reference counting is hard.

Raymond Chen
Raymond Chen

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

Code
Mar 25, 2004
Post comments count0
Post likes count0

Regular expressions and the dreaded *? operator

Raymond Chen
Raymond Chen

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 between, and th...

Code
Mar 22, 2004
Post comments count0
Post likes count0

Why an object cannot be its own enumerator

Raymond Chen
Raymond Chen

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, so o...

Code
Mar 12, 2004
Post comments count0
Post likes count0

What is the default security descriptor?

Raymond Chen
Raymond Chen

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

Code
Mar 9, 2004
Post comments count0
Post likes count0

Char.IsDigit() matches more than just "0" through "9"

Raymond Chen
Raymond Chen

Warning: .NET content ahead! Yesterday, Brad Abrams noted that Char.IsLetter() matches more than just "A" through "Z". What people might not realize is that Char.IsDigit() matches more than just "0" through "9". Valid digits are members of the following category in UnicodeCategory: DecimalDigitNumber. But what exactly is a DecimalDigitNumber? ...

Code
Feb 27, 2004
Post comments count0
Post likes count0

The correct order for disabling and enabling windows

Raymond Chen
Raymond Chen

If you want to display modal UI, you need to disable the owner and enable the modal child, and then reverse the procedure when the modal child is finished. And if you do it wrong, focus will get all messed up. If you are finished with a modal dialog, your temptation would be to clean up in the following order: But if you do that, you'll...

Code
Feb 24, 2004
Post comments count0
Post likes count0

What's so special about the desktop window?

Raymond Chen
Raymond Chen

The window returned by GetDesktopWindow() is very special, and I see people abusing it all over the place. For example, many functions in the shell accept a window handle parameter to be used in case UI is needed. IShellFolder::EnumObjects, for example. What happens if you pass GetDesktopWindow()? If UI does indeed need to be displaye...

Code