Showing tag results for Code

Sep 11, 2007
Post comments count0
Post likes count1

What's the difference between EM_UNDO and WM_UNDO?

Raymond Chen

Daniel Chait wonders why we have both and . You know, I wonder the same thing. But I'm going to make an educated guess. Actually, most of what I write is just a lot of educated guessing. Like my explanation of why has such complicated rules? A guess. Why address space granularity is 64KB? A guess. Why most messages are in the system message...

Code
Sep 10, 2007
Post comments count0
Post likes count1

If control-specific messages belong to the WM_USER range, why are messages like BM_SETCHECK in the system message range?

Raymond Chen

When I discussed which message numbers belong to whom, you may have noticed that the messages for edit boxes, buttons, list boxes, combo boxes, scroll bars, and static controls go into the system range even though they are control-specific. How did those messages end up there? They didn't start out there. In 16-bit windows, these control-spec...

Code
Sep 4, 2007
Post comments count0
Post likes count2

Does creating a thread from DllMain deadlock or doesn't it?

Raymond Chen

Let me get this out of the way up front: Creating a thread from is not recommended. The discussion here has to do with explaining the behavior you may observe if you violate this advice. Commenter Pete points out that "according to Usenet" creating a thread in is supposed to deadlock, but that's not what he saw. All he saw was that the thread ...

Code
Aug 29, 2007
Post comments count0
Post likes count1

Kernel handles are not reference-counted

Raymond Chen

Here's a question that floated past some time ago: In my code, I have multiple objects that want to talk to the same handle (via ). Each time I create an object, I use to increment the reference count on the handle. That way, when each object calls , only the last one actually closes the handle. However, when I run the code, I find as soon as t...

Code
Aug 17, 2007
Post comments count0
Post likes count1

What are these spurious nop instructions doing in my C# code?

Raymond Chen

Prerequisites: Basic understanding of assembly language. When you debug through some managed code at the assembly level, you may find that there are an awful lot of instructions scattered throughout your method. What are they doing there; isn't the JIT smart enough to remove them? Isn't this going to slow down execution of my program? It is my u...

Code
Aug 16, 2007
Post comments count0
Post likes count0

What are these strange cmp [ecx], ecx instructions doing in my C# code?

Raymond Chen

When you debug through some managed code at the assembly level, you'll find a whole lot of seemingly pointless instructions that perform a comparison but ignore the result. What's the point of comparing two values if you don't care what the result is? In C++, invoking an instance method on a pointer results in undefined behavior. In other words, ...

Code
Aug 15, 2007
Post comments count0
Post likes count1

C# static constructors are called on demand, not at startup

Raymond Chen

One of the differences between C++ and C# is when static constructors run. In C++, static constructors are the first thing run in a module, even before the function runs.¹ In C#, however, static constructors don't run until you use the class for the first time. If your static constructor has side effects, you may find yourself experiencing ...

Code
Aug 14, 2007
Post comments count0
Post likes count0

What is the order of evaluation in C#?

Raymond Chen

The C and C++ languages leave the order of evaluation generally unspecified aside from specific locations called sequence points. Side effects of operations performed prior to the sequence point are guaranteed visible to operations performed after it.¹ For example, the C comma operator introduces a sequence point. When you write f(), g(), the...

Code
Aug 9, 2007
Post comments count0
Post likes count2

How do I get the handle of the primary monitor?

Raymond Chen

The primary monitor by definition has its upper left corner at (0, 0). Therefore, you can use this function: To make double extra sure (belt and suspenders), we also tell it, "If you're not sure what monitor to return, give us the primary." Sure, this sounds obviously simple, but I actually found somebody who fumbled around in the dark passi...

Code