The Old New Thing

How do I get the handle of the primary monitor?

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 ...

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

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 ...

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

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 ...

How to check for errors from SetFilePointer

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 ...

The forgotten common controls: The ShowHideMenuCtl function

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...

QueryPerformanceCounter is not a source for unique identifiers

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 ...

If the system says that an embedded string could not be converted from Unicode to ANSI, maybe it's trying to tell you something

It's probably trying to tell you that an embedded string could not be converted from Unicode to ANSI. One of our programs is throwing the exception "Type could not be marshaled because an embedded string could not be converted from Unicode to ANSI." It happens only if we use the Chinese version of the program. Why are we getting this ...

Don't forget to pass the current directory along with the command line to your single-instance program

If you make a single-instance program, and somebody runs a second copy of the program and passes a command line, the most common way of handling this is to hand the command line to the first copy of the program and let the first copy deal with it. When you do this, don't forget about the current directory. If somebody passes a relative path...