turn off Visual Studio 2005 automatic manifest resource generation in converted projects

When Visual Studio 2005 upgrades an old project, it turns on a setting which tells the linker to create a manifest resource in your program file. This resource seems to express a dependency on a DLL version of the C/C++ standard libraries. This causes the system to refuse to load your program unless said DLL is present. Your users will get an error indicating something about an invalid configuration and suggesting they reinstall. Apparently, the relevant libraries are not present on Windows XP SP2 (I haven't checked Vista), which causes the system to believe it should not even try to run some otherwise perfectly good programs.

This is a particularly vexing because some applications, including some bundled with Windows, install these libraries, and it can be difficult to figure out why some systems fail and others do not. One such application is, you guessed it, VS2005, so you will never be able to reproduce this problem on the system with which you built your program. Another seems to be Internet Explorer 7, so anybody who's got automatic updates turned on won't be able to reproduce this problem, either. Anybody who's still running IE6 (or earlier) without VS2005 probably will not be able to run your program.

Adding insult to injury, my programs already contain carefully constructed manifest resources and/or have carefully chosen linker options so that they don't take external dependencies which may or may not be present on deployment systems.

Fortunately, the fix was simple. There's a new linker option which controls generating this manifest resource. I turned it off and things went back to normal. It seems I could also have directed users to the Microsoft web page where they may download the C/C++ runtime libraries from VS2005. (Perhaps Microsoft ought to add these libraries to Windows Update.) Or, it seems, I could have redistributed these libraries with my programs (after deciding I am compatible with the relevant legalese).

I understand why Microsoft would want to add the capability of generating a manifest resource to the linker, but I don't understand why they would want to turn it on by default in such an annoying way. This cost me hours. It's partly my fault for not reading the upgrade report carefully; it did mention something about manifests. Had I been super-careful, I would have looked deeper into this potential problem and perhaps found the actual problem earlier. But since it didn't cause any trouble right away, I was lulled into a false sense of security that Microsoft had not done anything risky to my project.

VS2005 and vswprintf

When you upgrade an existing C++ program to Visual Studio 2005, the compiler will tell you any calls to vswprintf lack an argument specified by the C++ standard. Through the magic of C++ function overloading, your code still works, because Microsoft provides both the old-style proprietary three-argument function and the new-style standard four-argument function. The compiler tells you about the new function by means of a warning, and if you're a smart programmer, you've told the compiler to treat warnings as if they were errors in the sense that they prevent an object module from being generated, so there is a fair amount of motivation to adopt the new function so that the warning will go away.

The new parameter is the count of characters in the output buffer. After reading the documentation for this parameter, you might assume that, like the return value, the parameter does not include the terminating 0 character. But that assumption would be wrong; if you pass the number of characters you expect as a return value, the function will return -1 and errno will be set to ERANGE. You need to add 1 to the parameter (after, of course, making sure your buffer is large enough for the terminating 0). MSDN does not document this, and after having guessed at it myself, I searched the web until I found a Linux man page which told me my guess actually coincided with somebody's reality. I've chosen to assume Microsoft plays by the same rules.

Microsoft hell

This story confirms the sense I have gotten over the past few years that the Microsoft work environment has descended into hell. I personally know demonstrably bad people who've been hired there, and there is of course the ongoing suppurating sore that is Mini-Microsoft, and Ballmer is a blatant loser dork who desperately cries out for amputation. But the story puts it in terms geeks can understand.

mouse "moves" when it doesn't move

MSDN claims you'll receive WM_MOUSEMOVE when the mouse moves, and that's true as far as it goes. However, you'll also receive WM_MOUSEMOVE at a number of other times, notably when your window activates or another window moving reveals part of yours. So the real meaning of WM_MOUSEMOVE is "please act as if the mouse moved." That's fine most of the time, even though it means you might do some redundant work. But if you really want to know whether the mouse moved, you need to cache the last known mouse location, call GetMessagePos, and compare the result to your cached value. If the mouse really did move, then update your cache in addition to the other work you do.

Update 7 Dec 2006: St. Chen has already covered this topic. Big surprise.

tooltip message handling nightmare abated

I recently spent a boggling amount of time — a couple of weeks, on and off — second-guessing Microsoft's tooltips message handling code. Not only does it have at least one nasty bug, but it seems designed to make easy cases easy, which is a diplomatic way of saying it tends to make anything beyond that very difficult. Do try it, and see if it works for you, but if you spend more than a day scratching your head and trying to overcome limitations, my recommendation is to skip TTM_RELAYEVENT, turn off all tool flags other than TTF_TRACK and TTF_ABSOLUTE, and get real cozy with TTM_TRACKACTIVATE and TTM_TRACKPOSITION. Oh, and write your own equivalent of TTF_SUBCLASS. Once I realized I ought to give this approach a try, I was out of the woods in just a couple days, and I've written some C++ classes which will relieve me from ever having to deal with this madness again.

IONotificationPortGetRunLoopSource isn't as generic as you might think

Of IONotificationPortGetRunLoopSource, its header says only: "Returns a CFRunLoopSource to be used to listen for notifications." You might think from this generic description that the function would be useful for listening to any old notification. However, it calls CFMachPortCreateWithPort, passing an internal callback function which works well if the only notifications you care about are the ones you can create with high-level functions such as IOServiceAddMatchingNotification. If you want to field any other notification, you're out of luck, because that internal callback function will most likely ignore or misinterpret the 'type' field of your message header. So, rather than IONotificationPortGetRunLoopSource, call CFMachPortCreateWithPort and CFMachPortCreateRunLoopSource.

CFMachPortCreateWithPort performs a copy

For reasons I have not yet attempted to determine, CFMachPortCreateWithPort is backed by a cache whose key is the mach_port_t which the client provides. The function first searches this cache for previously created instances of CFMachPortRef with the specified mach_port_t. If one is present, the function passes the CFMachPortRef to CFRetain and returns it. A Create function thus returns a copy despite its name. If no matching CFMachPortRef is present in the cache, CFMachPortCreateWithPort creates one and adds it to the cache before returning it.