getaddrinfo() breaks applications migrated from Visual Studio 6.0

Here’s a problem CoolSoft Inc faced when migrating CoolApp - their flagship product.
CoolApp is an MFC application that sends some data to a server. It was developed in VC6 and winsock2 and it’s been running for quite a while now. CoolSoft wanted to upgrade CoolApp and recompiled the app in Visual Studio.Net 2003. The migration went fine. The app was also running perfectly. That’s when someone found that CoolApp doesn’t work anymore in Windows 2000 and Windows 98. In fact CoolApp worked only in Windows XP and Windows 2003. It used to work on almost all flavors in its earlier life. CoolApp threw up the following error message in Windows 2000.
“The procedure entry point getaddrinfo could not be located in the dynamic link library WS2_32.dll”
CoolSoft wants to know what the issue is, why wasn’t it occurring earlier and how can this be fixed ?

As per the MSDN Documentation for getaddrinfo, In Windows 2003 and Windows XP the function is defined in ws2tcpip.h and for all other OSs it is defined in wspiapi.h
The ws2tcpip.h file declares the getaddrinfo function and expects the application to link with the ws2_32.dll. However, wspiapi.h provides a hack for this function by defining the function as a macro.

#define getaddrinfo WspiapiGetAddrInfo

The ws2tcpip.h file that was part of Visual Studio 6 or the platform SDKS that came before Visual studio.Net did not have the getaddrinfo function defined. So the definition from wspiapi.h was automatically included. So everything works fine. But with VS.NET 2003 the new ws2tcpip.h is included and the resulting application will depend on getaddrinfo being available in ws2_32.dll. The ws2_32.dll doesn’t have a ‘getaddrinfo’ function in its non XP and non Windows 2003 versions. So when the app is run in any of the pre XP platforms, we get the error message.

To fix this, include the wspiapi.h file explicitly in the source files that call getaddrinfo.

Note : The issue and the workaround applies to the following scenarios too
1) The ‘freeaddrinfo’ function.
2) Apps written and compiled in VS.NET 2002 or higher.