The Old New Thing

Using accessibility to monitor windows as they come and go

Today's Little Program monitors windows as they come and go. When people contemplate doing this, they come up with ideas like installing a WH_CBT hook or a WH_SHELL hook, but one of the major problems with those types of hooks is that they are injected hooks. Injection is bad for a number of reasons. It forces the hook to be in a DLL so it ...

When will GetMessage return -1?

A source of great consternation is the mysterious return value from : If there is an error, the return value is −1. For example, the function fails if is an invalid window handle or is an invalid pointer. That paragraph has caused all sorts of havoc, because it throws into disarray the standard message pump: But don't worry, the ...

Playing with the Windows Animation Manager: Moving lots of stuff around

We saw last time a sample program that moved a circle around. Today I'll try to build the classic demo of animating a lot of objects in a list. This isn't the prettiest code, but I wanted to make as few changes as possible. Start with the Timer-Driven Animation, and make these changes to the Main­Window.h header file. struct Item...

Playing with the Windows Animation Manager: Fixing a sample

Windows 7 provides a component known as the Windows Animation Manager, known to some people by its acronym WAM, pronounced "wham". There are some nice sample programs for WAM on MSDN, but for some reason, the authors of the samples decided to animate the three color components of a resultant color. Because apparently the authors of ...

Closing holes in the update notification pattern

Suppose you have a function that is registered to be called the next time something gets updated, and suppose that the notification is a one-shot notification and needs to be re-armed each time you want to wait for the next notification. (For example, the Reg­Notify­Change­Key­Value function behaves this way.) Consider the ...

How can I see what files and shares are being accessed remotely, and the general usage pattern for the NetXxx functions

Today's Little Program is a command line version of the Shared Folders MMC snap-in. Why? Because it illustrates the usage pattern for the Net­Xxx family of functions. (It's also a clone of the networking portion of the openfiles tool.) The Net­Xxx family of functions generally work like this: You pass in some parameters that ...

What are the conventions for managing standard handles?

Consider this function: void ChangeConsoleColor(WORD wColor) { HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); if (h != INVALID_HANDLE_VALUE) { SetConsoleTextAttribute(h, wColor); CloseHandle(h); } } "When I call this function, it works the first time, but when I call it a second time, Get­Std­Handle returns a handle ...

What are the dire consequences of not selecting objects out of my DC?

The convention when working with device contexts is to restore them to the way you found them. If a drawing function selects a bitmap into a device context, then it should select the original bitmap into the device context before returning. Same for fonts, pens, all that stuff. But what if you decide to violate that convention? For example, ...