Showing results for Code - The Old New Thing

Oct 9, 2009
Post comments count0
Post likes count0

LoadString can load strings with embedded nulls, but your wrapper function might not

Raymond Chen
Raymond Chen

Whenever somebody reports that the function or the member of the structure is not working, my psychic powers tell me that they failed to manage the double-null-terminated strings. Since string resources take the form of a counted string, they can contain embedded null characters, since the null character is not being used as the string term...

Code
Oct 8, 2009
Post comments count0
Post likes count0

What is the format of a double-null-terminated string with no strings?

Raymond Chen
Raymond Chen

One of the data formats peculiar to Windows is the double-null-terminated string. If you have a bunch of strings and you want to build one of these elusive double-null-terminated strings out of it, it's no big deal. But what about the edge cases? What if you want to build a double-null-terminated string with no strings? Let's step back and lo...

Code
Oct 7, 2009
Post comments count0
Post likes count0

The ways people mess up IUnknown::QueryInterface, episode 3

Raymond Chen
Raymond Chen

Today we'll combine information you already know, some of which I covered in The ways people mess up IUnknown::QueryInterface with additional clues from The layout of a COM object. There's still not enough information for you to solve the problem entirely on your own, but maybe you can demonstrate your nascent psychic debugging powers and solve ...

Code
Oct 1, 2009
Post comments count0
Post likes count0

In the search for the subtle source of the problem, you sometimes overlook the obvious one

Raymond Chen
Raymond Chen

A customer was encountering a problem with lots of duplicate GUIDs. How is that possible? The whole point of the GUID generation algorithm is to work hard to avoid duplication. Was one of the fundamental assumptions of the algorithm broken? Maybe there was a duplicate MAC? Was the clock regressing? One of my colleagues pointed out that in the sea...

Code
Sep 30, 2009
Post comments count0
Post likes count0

Why do messages posted by PostThreadMessage disappear?

Raymond Chen
Raymond Chen

The only thread message you can meaningfully post to a thread displaying UI is , and even then, it's only because you want to wake up the message loop for some reason. A common problem I see is people who use to talk to a thread that is displaying UI and then wonder why the message never arrives. Oh, the message arrived all right. It arrived and...

Code
Sep 25, 2009
Post comments count0
Post likes count0

The ways people mess up IUnknown::QueryInterface, episode 2

Raymond Chen
Raymond Chen

Sadly, I get to add another entry to The ways people mess up : Blindly responding to everything. Some people are just too eager to please. No matter what the interface is, they say, "Sure, we do that!" Furthermore, no matter what you ask for, they always return the same interface. Even if it's not what you asked for. Exercise: Some pe...

Code
Sep 23, 2009
Post comments count0
Post likes count0

The COM marshaller uses the COM task allocator to allocate and free memory

Raymond Chen
Raymond Chen

It should be second nature to you that the code which allocates memory and the code which frees memory need to use the same allocator. Most of the time, you think of it as "If you allocate memory, you need to free it with the corresponding mechanism," but this sentence works in the reverse direction as well: If you hand memory to a function that w...

Code
Sep 21, 2009
Post comments count0
Post likes count0

What is the logic behind the thumb size and position calculation in scroll bars?

Raymond Chen
Raymond Chen

Commenter sarathc asks, "How do we implement a custom scroll bar as Windows does? What is the logic behind the thumb size and position calculation? How we could dynamically manage it?" Let's look at the three questions in turn. To implement a custom scroll bar... don't do it. It's just not worth the effort, and there will almost always be little...

Code
Sep 14, 2009
Post comments count0
Post likes count0

Can you create an information context for the display?

Raymond Chen
Raymond Chen

Adrian McCarthy asks, "Can you create an information context for the display? ... I can call CreateDC("DISPLAY"), but perhaps that wouldn't generalize for a multiple-monitor display with different settings on each screen. I'm trying to avoid constantly creating and destroying DCs when all I need to do is measure strings, check color depth, dpi, etc...

Code
Sep 11, 2009
Post comments count0
Post likes count0

If you're handling an out of memory exception, you probably shouldn't allocate memory

Raymond Chen
Raymond Chen

With the assistance of Application Verifier, specifically, low resource simulation (also known as fault injection), a tester found a stack overflow condition. As we learned earlier, the important thing to look at when studying a stack overflow is the repeating section. When this stack trace was shown to the development team, they instantly re...

Code