October 4th, 2013

What's the difference between CopyIcon and DuplicateIcon?

There are two functions that can be used to create one icon that is identical to another. One of them is Copy­Icon. The other is Duplicate­Icon. What’s the difference? There isn’t any difference. Both functions clone an icon. In fact, their implementations are basically line-for-line identical. Originally, there was just one function to clone an icon: Copy­Icon. Windows 3.0 introduced Program Manager, and the developers of Program Manager wrote their own function called Duplicate­Icon. Why? I have no idea. My guess is that they didn’t realize that such a function already existed, so they inadvertently reinvented the wheel. Windows NT 3.1 came along, and the team that ported Program Manager to 32-bit Windows also ported the Duplicate­Icon function, and they figured, “This function is so useful, we’ll export it for anybody to use!” Meanwhile, the original Copy­Icon function is sitting there saying, “What am I, chopped liver?” Anyway, it’s a sad story, but that’s how we ended up with two functions that do exactly the same thing. Personally, I would recommend using the Copy­Icon function. It’s in user32.dll, which you are almost certainly already linked to if you’re doing anything with icons in the first place, so the incremental cost is much lower.

Update: Joshua points out that the two functions are not identical. Duplicate­Icon takes an extra instance handle parameter. Now it makes sense. The shell version is an enhancement to the user version in that it can also transfer icon ownership to another module. (Hence the new first parameter.) This was important in 16-bit Windows because icons were resources which were associated with modules. If you wanted to use an icon after the module was unloaded, you needed to copy it and transfer ownership. But this ownership transfer step is not needed in Win32 because, as we saw yesterday, icons are no longer tied to the underlying resources. So the functions started out different but now they’re the same.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.