The Old New Thing

Practical development throughout the evolution of Windows.

Latest posts

Aug 31, 2012
Post comments count 0
Post likes count 1

Adventures in undefined behavior: The premature downcast

Raymond Chen

A customer encountered the following problem: The function converts the cookie back to a object, and then checks if the object is a object by calling . If so, then it does some more stuff to figure out what type of paint to buy. (Note to nitpickers: The actual scenario was not like this, but I presented it this way to illustrate the point. If you say "You should've used RTTI" or "You should've had a BuyPaint method on the Shape class", then you're missing the point.) The programmers figured they'd save some typing by casting the result of to a right away, because after all, since is a virtual method...

Aug 30, 2012
Post comments count 0
Post likes count 1

I'm not calling a virtual function from my constructor or destructor, but I'm still getting a __purecall error

Raymond Chen

Some time ago, I described what is for: It's to detect the cases where you call a virtual method with no implementation (a so-called pure virtual method). This can happen during object constructor or destruction, since those are times when you can validly have a partially-implemented object. Well, there's another case this can happen: If the object has already been destructed. If you call a method on an object that has already been destructed, the behavior is undefined. In practice, what happens is that the method runs on whatever leftover values are in memory where the object used to be. Depending on how luc...

Aug 29, 2012
Post comments count 0
Post likes count 1

Psychic debugging: Why Chinese characters don't work

Raymond Chen

Here's a walkthrough of an actual case of psychic debugging. I'm using the internal XYZ table-generating tool, but it appears to be useless in my case. Does this tool work? Right now, I'm just resorting to the manual method of generating my data table (Chinese and English). Hooray for vague questions! "It appears to be useless." But there were enough tiny clues in the question that made it possible to make a psychic guess. First of all, the parenthetical "Chinese and English" suggests that there are Chinese characters in the data being passed to the XYZ table-generating tool. Second of all, one might make a...

Aug 28, 2012
Post comments count 0
Post likes count 1

Command line tool to manage Windows 7 Libraries, with source code

Raymond Chen

A customer asked if there was a command-line tool for managing Windows 7 Libraries so that they could create and deploy libraries across their organization. Not only is there such a tool, it even comes with source code. The Shell Library Command Line Sample shows how to manage Windows 7 libraries programmatically via the interface. And it's actually a useful program on its own. For example, to see all the folders that are part of your Documents library, use the command Each of the commands has further sub-options. And the source code shows how to perform each of these actions programmati...

Aug 28, 2012
Post comments count 0
Post likes count 1

Well at least nobody's parking there any more

Raymond Chen

There is a field next to the Microsoft building I used to work in, and for a time, people parked their cars out on the field, presumably because finding a proper parking space in the garage became difficult due to overcrowding. To prevent people from parking in the field, Security placed a large log across the access to the field. The technique worked: Nobody parked in the field any more. Some months later, our building had a fire drill, and everybody dutifully filed out of the building and waited until the all-clear signal was given to return. Normally, people would wait in the field, because that is the design...

Aug 27, 2012
Post comments count 0
Post likes count 1

How does the taskbar decide whether to show Recent or Frequent documents on the jump list?

Raymond Chen

Some jump lists show Recent documents, and others show Frequent documents. (Images stolen from Sasha Goldshtein.) Our friend xpclient wanted to know how the taskbar decides which one to use, because it seemed random. (Ironically, xpclient is asking a question about the Window 7 client.) The default is to show Recent documents. But an application can customize its jump list, and in that case, the application can select which category to show based on the guidance in the documentation. (Or the application might choose to ignore the guidance in the documentation and show both.) Okay, but what about Expl...

Aug 24, 2012
Post comments count 0
Post likes count 1

Dumping a hash table with external chaining from the debugger

Raymond Chen

I was doing some crash dump debugging, as I am often called upon to do, and one of the data structure I had to grovel through was something that operated basically like an atom table, so that's what I'll call it. Like an atom table, it manages a collection of strings. You can add a string to the table (getting a unique value back, which we will call an atom), and later you can hand it the atom and it will give you the string back. It looked something like this: (It didn't actually look like this; I've reduced it to the smallest example that still illustrates my point.) As part of my debugging, I had an atom...

Aug 23, 2012
Post comments count 0
Post likes count 1

Wait, you never said that I had to initialize the object before I used it!

Raymond Chen

A customer reported that they were having trouble creating slim reader/writer locks at runtime. They simplified the issue to a short program: "What is the correct way of creating an via the operator?" It wasn't long before somebody noted that nowhere in the code is the function called. "Oh, yeah, thanks for catching that. It looks like one needs to initialize SRW locks which are created via the operator. Otherwise it's not required." No, the function is always required. It's just that you got lucky in the local variable case and the initial stack garbage looks enough like an initialized SRW lock th...

Aug 22, 2012
Post comments count 0
Post likes count 1

The shifting sands of "Run as different user"

Raymond Chen

A customer liaison asked the following question on behalf of his customer. When I do a shift-right-click on a shortcut to a program, I see the following: The option to run a program as another user (other than Administrator) was present in Windows XP, but it was lost in Windows Vista. It appears that we responded to those complaints by restoring the functionality in Windows 7. Is that right? The odd thing is that my customer has the Run as different user option available on their Windows 7 machines, but not on their Windows Server 2008 R2 machines. Does whether you have access t...