Showing results for Code - The Old New Thing

Jul 10, 2014
Post comments count0
Post likes count0

Using the REFIID/void** pattern for returning COM objects for future-proofing and to avoid problems with dependencies

Raymond Chen
Raymond Chen

Suppose you have a function that creates a reference to a COM object: There are a few issues with this design. First of all, it requires that whoever uses your header file must have included first, since that's where is defined. You could solve that problem by putting at the top of , but that creates its own problems. For example, many hea...

Code
Jun 30, 2014
Post comments count0
Post likes count0

Getting the location of the Close button in the title bar, from Windows 2000 or Windows XP

Raymond Chen
Raymond Chen

Today's Little Program locates the × button in the corner of the window and displays a balloon tip pointing at it. We did this some time ago with the help of the message, which is new for Windows Vista. But what if you don't have that message available, say, because you're running on Windows 2000 or Windows XP or (gasp) Windows 9...

Code
Jun 23, 2014
Post comments count0
Post likes count0

Adding a sound to the Alt+Tab window

Raymond Chen
Raymond Chen

Today's Little Program plays a sound when the Alt+Tab window appears and disappears. The program registers an accessibility event hook for the and events. The Start event fires when an Alt+Tab operation begins, and the End event fires when an Alt+Tab operation completes. As noted in the documentation, you can get spurious End events, so we k...

Code
Jun 20, 2014
Post comments count0
Post likes count0

Once you go input-idle, your application is deemed ready to receive DDE messages

Raymond Chen
Raymond Chen

Feel free to stop using DDE. There was one customer who confessed that they were still using DDE, and they asked for help debugging a DDE problem. They found that sometimes, when their application was launched for DDE, it never received the message. Instead, the function returned . If launched from Explorer, the error message shown to the user ...

Code
Jun 16, 2014
Post comments count0
Post likes count0

Enumerating bit strings with a specific number of bits set (binomial coefficients strike again)

Raymond Chen
Raymond Chen

Today's Little Program prints all bit strings of length n subject to the requirement that the string have exactly k 1-bits. For example, all bit strings of length 4 with 2 bits set are 0011, 0101, 0110, 1001, 1010, and 1100. Let's write BitString(n, k) to mean all the bit strings of length n with exactly k bits set. Let's look at the last b...

Code
Jun 13, 2014
Post comments count0
Post likes count0

Non-classical processor behavior: How doing something can be faster than not doing it

Raymond Chen
Raymond Chen

Consider the following program: The program generates a lot of random integers in the range 0..9 and then counts how many are less than 0, less than 1, less than 2, and so on. It also prints how long the operation took in QPC units. We don't really care how big a QPC unit is; we're just interested in the relative values. (We print the number of...

Code
Jun 12, 2014
Post comments count0
Post likes count0

Can we continue to call FindNextFile() with the same handle that returned an error last time?

Raymond Chen
Raymond Chen

A customer wanted to know whether it was okay to call with the same handle that returned an error last time. In other words, consider the following sequence of events: The customer elaborated: Suppose that the directory contains four files, A, B, C, and D. We expect the following: After returns an error, can we continue to search with th...

Code
Jun 9, 2014
Post comments count0
Post likes count0

Improving the performance of CF_HDROP by providing file attribute information

Raymond Chen
Raymond Chen

The clipboard format is still quite popular, despite its limitation of being limited to files. You can't use it to represent virtual content, for example. For all of you still using , you can improve the performance of drag/drop operations by adding a little more information to your data object. Observe that the clipboard format is just a l...

Code