Showing tag results for 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
Jul 27, 2007
Post comments count0
Post likes count1

Don't just grab the foreground window and host UI on it

Raymond Chen

I was asked to look at an application compatibility bug in a program from a major vendor. But that's actually irrelevant; what I'm writing about today has nothing to do with application compatibility. That's just what drew my attention to the program in the first place. At some point during the install, the setup program encountered an error and w...

Code
Jul 12, 2007
Post comments count0
Post likes count1

If you want a modeless dialog, then create a modeless dialog already

Raymond Chen

Somebody I'd never heard of sent me email with a general programming question. (This is one of the top ways of instantly making me not interested in talking to you.) I have a modal dialog box which I hide with , and I want to enable the parent window so the parent window will get focus and keyboard input. Is this possible? If so, how do I do it?...

Code
Jul 11, 2007
Post comments count0
Post likes count1

How to check for errors from SetFilePointer

Raymond Chen

The function reports an error in two different ways, depending on whether you passed as the parameter. The documentation in MSDN is correct, but I've discovered that people prefer when I restate the same facts in a different way, so here comes the tabular version of the documentation. I'd show some sample code, but the documentation in MSDN ...

Code
Jul 10, 2007
Post comments count0
Post likes count1

The forgotten common controls: The ShowHideMenuCtl function

Raymond Chen

The function is one of those functions everybody tries to pretend doesn't exist. You thought was bad; is even worse. The idea behind was that you had a window with a menu as well as controls, and some of the menu items were checkable, indicating whether the corresponding control should be shown. For example, on your View menu you might have...

Code
Jul 5, 2007
Post comments count0
Post likes count1

QueryPerformanceCounter is not a source for unique identifiers

Raymond Chen

This article happened to catch my eye: I needed to generate some unique number in my application. I could use GUID, but it was too large for me (I need to keep lots of unique identifiers). I found something like this: This code generates Int64 (long) unique number (at least I hope it is unique). The uniqueness is in the scope of process. So...

Code