How do my API wrapper functions avoid triggering linker errors?
July 10, 2013 Leave a comment
Now that we have a stable (VS2012+Update 3) solution for XP support, there is no longer a need for these wrappers.
I was going to make a general write-up on how all of my API wrapper classes work, but in searching around, I found out that Antony, who graciously contributed the x64 version of the xpsupport wrappers, already wrote up an excellent article a while back describing how the linker resolves symbols. Please read:
Thanks to Antony for his contributions to this project and his great description.
So using assembly allows us to avoid the dreaded “duplicate symbol” error, if we had tried to do this in C/C++. Interestingly, GCC has a much more elegant way of doing this:
notice the last line (edited to avoid use of a macro):
DWORD (WINAPI *__imp_GetTickCount)(void) asm(“__imp__GetTickCount@0”) = GetTickCount;
The asm keyword can be used to define a symbol that can be overridden by an arbitrary symbol. This functionality is not available in MSVC.
I’ll be talking more about wrappers in the near future, specifically in the area of forbidden Win32 APIs in Windows Store apps.