The Old New Thing

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

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

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

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

A program for my nieces: The ABCs, part 1

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

It’s the address space, stupid

Nowadays, computers have so much memory that running out of RAM is rarely the cause for an "out of memory" error. Actually, let's try that again. For over a decade, hard drive have been so large (and cheap) that running out of swap space is rarely the cause for an "out of memory" error. In user-mode, the term memory refers to virtual ...

The default error mode (SetErrorMode) is not zero

A customer put the following code at the start of their program: // If this assertion fires, then somebody else changed the error mode // and I just overwrote it with my error mode. ASSERT(SetErrorMode(SEM_FAILCRITICALERRORS) == 0); The customer wanted to know whether it was a valid assumption that the initial error mode for a process is ...

Drawing content at a fixed screen position, regardless of window position

Today's Little Program draws content at a fixed screen position. The idea is that the window is really a viewport into some magical world. Unfortunately, our magical world just has a sign that says "Booga booga." Creating a more interesting magical world is left as an exercise. Start with our scratch program and make these changes: void ...

Of what use is the RDW_INTERNALPAINT flag?

For motivational purposes, let's start with a program that displays a DWM thumbnail. Start with the scratch program and add the following: #include <dwmapi.h> HWND g_hwndThumbnail; HTHUMBNAIL g_hthumb; void UpdateThumbnail(HWND hwndFrame, HWND hwndTarget) { if (g_hwndThumbnail != hwndTarget) { g_hwndThumbnail = hwndTarget; ...

AttachThreadInput is like taking two threads and pooling their money into a joint bank account, where both parties need to be present in order to withdraw any money

Consider this code: // Code in italics is wrong foregroundThreadId = ::GetWindowThreadProcessId(::GetForegroundWindow(), 0); myThreadId = GetCurrentThreadId(); if (foregroundThreadId != myThreadId) { AttachThreadInput(foregroundThreadId, myThreadId, TRUE); BringWindowToTop(myWindowHandle); If you try to ...