Showing results for Code - The Old New Thing

May 11, 2011
Post comments count0
Post likes count0

Why double-null-terminated strings instead of an array of pointers to strings?

Raymond Chen
Raymond Chen

I mentioned this in passing in my description of the format of double-null-terminated strings, but I think it deserves calling out. Double-null-terminated strings may be difficult to create and modify, but they are very easy to serialize: You just write out the bytes as a blob. This property is very convenient when you have to copy around the lis...

Code
May 6, 2011
Post comments count0
Post likes count0

A function pointer cast is a bug waiting to happen

Raymond Chen
Raymond Chen

A customer reported an application compatibility bug in Windows. We have some code that manages a Win32 button control. During button creation, we subclass the window by calling . On the previous version of Windows, the subclass procedure receives the following messages, in order: We do not handle any of these messages and pass them thro...

Code
May 5, 2011
Post comments count0
Post likes count0

Your program loads libraries by their short name and you don't even realize it

Raymond Chen
Raymond Chen

In the discussion of the problems that occur if you load the same DLL by both its short and long names, Xepol asserted that any program which loads a DLL by its short name "would have ONLY itself to blame for making stupid, unpredictable, asinine assumptions" and that Windows should "change the loader to NOT load any dll with a short name where the...

Code
Apr 29, 2011
Post comments count0
Post likes count0

Why is there a RestoreLastError function that does the same thing as SetLastError?

Raymond Chen
Raymond Chen

Matt Pietrek noticed that and do exactly the same thing and wondered why there's a separate function for it. It's to assist in debugging and diagnostics. Say you're debugging a problem and when you call you get . It would really help a lot if you could figure out who set the error code to . If you set a breakpoint on , you find that people...

Code
Apr 26, 2011
Post comments count0
Post likes count0

No, you can't ask whether your notification icon is visible or not

Raymond Chen
Raymond Chen

A customer asked what started out as a simple question, but by now you know that this sort of simple question only raises new questions: Is there a way that we can detect that the user has hidden our notification icon? No, there is no way to ask whether your notification icon is hidden or visible. Users decide which icons they want to see, and y...

Code
Apr 22, 2011
Post comments count0
Post likes count0

Even if you have a lock, you can borrow some lock-free techniques

Raymond Chen
Raymond Chen

Even if you prefer to use a lock (after all, they are much easier to program), you can borrow some lock-free techniques. For example, consider this: There are some concerns here. First of all, there's the lock hierarchy issue: If reticulating a spline takes the geometry lock, that may violate our lock hierarchy. If the lock is a hot lock, yo...

Code
Apr 21, 2011
Post comments count0
Post likes count0

The performance improvements of a lock-free algorithm is often not in the locking

Raymond Chen
Raymond Chen

GWO wonders what the conditions are under which the lock-free version significantly outpeforms a simple critical section. Remember that switching to a lock-free algorithm should be guided by performance measurements. Switching from a simple algorithm to a complex one shouldn't be done unless you know that the simple algorithm is having trouble....

Code
Apr 20, 2011
Post comments count0
Post likes count0

Corrections to Patterns for using the InitOnce functions

Raymond Chen
Raymond Chen

Adam Rosenfield pointed out that it is not possible to fail an asynchronous initialization; if you pass when completing an asynchronous initialization, the function fails with . (Serves me right for writing an article the night before it goes up.) A more correct version is therefore In other words, the pattern is as follows: While I'm her...

Code
Apr 19, 2011
Post comments count0
Post likes count0

Visual Studio 2005 gives you acquire and release semantics for free on volatile memory access

Raymond Chen
Raymond Chen

If you are using Visual Studio 2005 or later, then you don't need the weird function because Visual Studio 2005 and later automatically impose acquire semantics on reads from volatile locations. It also imposes release semantics on writes to volatile locations. In other words, you can replace the old function with the following: This is a g...

Code
Apr 18, 2011
Post comments count0
Post likes count0

Don't forget to include the message queue in your lock hierarchy

Raymond Chen
Raymond Chen

In addition to the loader lock, the message queue is another resource that people often forget to incorporate into their lock hierarchy. If your code runs on a UI thread, then it implicitly owns the message queue whenever it is running, because messages cannot be dispatched to a thread until it calls a message-retrieval function such as or . In o...

Code