Showing tag results for Code

Aug 12, 2013
Post comments count0
Post likes count1

How do I convert a synchronous file handle into an asynchronous one?

Raymond Chen

Say you opened a file in synchronous mode, and then you realize that you want to issue asynchronous I/O on it, too. One way to do this is to call a second time with the , but this requires you to know the file name, and the file name may not be readily available to the function that wants to do the conversion, or it may not even be valid any long...

Code
Aug 9, 2013
Post comments count0
Post likes count1

Why does the CLR report a NullReferenceException even if the referenced access is not exactly the null pointer?

Raymond Chen

We saw some time ago that before invoking a method on an object, the CLR will generate a instruction to force a null reference exception to be raised if you are trying to invoke a method on a null reference. But why does the CLR raise a if the faulting address is almost but not quite zero? When run, this program raises a rather than an . ...

Code
Aug 8, 2013
Post comments count0
Post likes count1

On partially-constructed objects, additional remarks, not as interesting

Raymond Chen

Don't worry. Our long national nightmare of CLR week is almost over. I had originally planned to write an article about partially-constructed objects, but in the time since I queued up the topic (back in November 2005) to the time I got around to writing it up, I found that Joe Duffy had already written it for me! On partially-constructed ob...

Code
Aug 7, 2013
Post comments count0
Post likes count1

Weak references have no effect on object lifetime

Raymond Chen

The class lets you retain a reference to an object while still permitting the object to be garbage collected. When that happens, then the property is and the property is . (Related discussion.) Note, however, that weak references do not alter the decision of the garbage collector whether or not an object is garbage. It merely lets you observe ...

Code
Aug 6, 2013
Post comments count0
Post likes count1

The mysterious ways of the params keyword in C#

Raymond Chen

If a parameter to a C# method is declared with the keyword, then it can match either itself or a comma-separated list of um itselves(?). Consider: This program prints The first call to does not take advantage of the keyword and passes the array explicitly (formally known as normal form). The second call, however, specifies the integers d...

Code
Aug 5, 2013
Post comments count0
Post likes count1

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

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 count1

A program for my nieces: The ABCs, part 4

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 count1

What is the default cursor for a thread?

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 count1

A program for my nieces: The ABCs, part 3

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 count1

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

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