Showing results for Code - The Old New Thing

Aug 5, 2013
Post comments count0
Post likes count0

Why does BitConverter.LittleEndian return false on my x86 machine?

Raymond Chen
Raymond Chen

Welcome to CLR Week 2013, returned from its two-year hiatus. A customer reported that when they checked with the debugger, reported even though they were running on an x86 machine, which is a little-endian architecture. The bytes are extracted in little-endian order, despite the claim that the machine is big-endian. "I don't get it." I d...

Code
Jul 29, 2013
Post comments count0
Post likes count0

A program for my nieces: The ABCs, part 4

Raymond Chen
Raymond Chen

Disabling autorepeat in my ABC program was largely sufficient to keep my nieces happy, given their instructions to press only one key at a time. Once in a while, though, they would hit the context menu key in the bottom right corner of the keyboard, and then they'd get stuck because they didn't know how to dismiss it. So let's disable that key ...

Code
Jul 25, 2013
Post comments count0
Post likes count0

What is the default cursor for a thread?

Raymond Chen
Raymond Chen

When we looked at the process by which the cursor is set, we neglected to discuss the case where nobody bothers to set the cursor. What is the ultimate default cursor? Let's write a program that refuses to set the cursor. Take the scratch program and add these lines: What we did was make the window explicitly refuse to set the cursor by mak...

Code
Jul 22, 2013
Post comments count0
Post likes count0

A program for my nieces: The ABCs, part 3

Raymond Chen
Raymond Chen

One problem I discovered when my nieces ran my initial ABC program was that they had a habit of holding down a key, thereby triggering autorepeat. I had instructed them not to mash the keyboard but rather to press only one key at a time, and while they were good at adhering to the "one key at a time" rule, they also interpreted it as "type really...

Code
Jul 19, 2013
Post comments count0
Post likes count0

Nasty gotcha: STGM_READ | STGM_WRITE does not grant read/write access

Raymond Chen
Raymond Chen

You might think that if you want to get read/write access, you could pass . You would be wrong. You have to pass . The three flags , , and are mutually exclusive. If you try to combine them, you get a weird mess. In particular, since the numerical value of is zero, passing is numerically equivalent to passing , which grants write-only access. ...

Code
Jul 18, 2013
Post comments count0
Post likes count0

If you're not using the command line interpreter, then the command line interpreter metacharacters mean nothing

Raymond Chen
Raymond Chen

A customer observed that the parameters passed to were not being interpreted correctly. The process is created successfully, but it prints the message ERROR: The system was unable to find the specified registry key or value.. Why aren't the parameters being parsed correctly by ? They work fine if I paste them into a command prompt. This is a...

Code
Jul 15, 2013
Post comments count0
Post likes count0

A program for my nieces: The ABCs, part 2, choosing a font

Raymond Chen
Raymond Chen

I added a feature to my ABC program that it turns out I never actually used: Change the font. I added this in case my nieces were somehow unhappy with the font I chose, and this was a little escape hatch to let me select a different one. The real work happens in the function. All I have to do is call it. I tell the common font dialog to ini...

Code
Jul 12, 2013
Post comments count0
Post likes count0

If you want to track whether the current thread owns a critical section, you can use the critical section itself to protect it

Raymond Chen
Raymond Chen

You may find yourself in the situation where you want to keep track of the owner of a critical section. This is usually for debugging or diagnostic purposes. For example, a particular function may have as a prerequisite that a particular critical section is held, and you want to assert this so that you can catch the problem when running the debug ...

Code
Jul 8, 2013
Post comments count0
Post likes count0

A program for my nieces: The ABCs, part 1

Raymond Chen
Raymond Chen

I'm going to spend the next few weeks developing a Little Program in several parts. This is a program I wrote for my nieces, who always wanted to play with my laptop (instead of playing with me). Initially, I fired up Notepad and maximized it, and cranked the font size, but that became cumbersome, because I had to reset the font size and Word Wr...

Code
Jul 1, 2013
Post comments count0
Post likes count0

Building on our program that draws content at a fixed screen position regardless of window position

Raymond Chen
Raymond Chen

Today's Little Program uses the technique we saw last week of drawing content at a fixed screen position, regardless of window position, but adds a little physics to it. Start with our scratch program and make these changes: The class simulates damped motion. Ask a physicist how it works. The rest point is the center of the window. T...

Code