GetAddrInfo identifier not found

2019-07-20 05:14发布

I'm getting a couple errors when attempting to compile a DLL for use in a program that will run on Windows XP. They are both similar: 'GetAddrInfo: identifier not found', and 'FreeAddrInfo: identifier not found'. I'm limited in what I can do (I must modify existing code that hasn't been updated since 2012. I'm a co-op, so my degree in computer science is not yet complete, and I may not know certain things). I'm using MFC in a static library. My platform toolset is Visual Studio 2013 - Windows XP (v120_xp). I am using Microsoft Visual Studio Professional 2013. My StdAfx.h includes the following code:

#ifndef WINVER
#define WINVER 0x0501
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#define VC_EXTRALEAN
//...
#include <WinSock2.h>

The Foo.cpp file I'm getting the error from includes the following headers:

#include "stdafx.h"
#include <stdio.h>
#include <WinIoctl.h>
#include <process.h>    
#include <Windows.h>
#include <MMSystem.h>
#include <Mstcpip.h>
#include <WS2tcpip.h>

My version of WS2tcpip.h is from the Windows 7.1A SDK. If I open it, I can see that GetAddrInfo() and FreeAddrInfo() are defined, but for whatever reason, VS2013 is not finding those definitions (though it can find the header file itself and has no trouble including them).

Additional note: if I change the #defines in StdAfx.h to:

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600

and use the regular Visual Studio 2013 toolset, I have no problems compiling the DLL. Unfortunately, the DLL must work with Windows XP. I've been unable to find any information about why this might be. Could my includes be in the wrong place?

2条回答
叛逆
2楼-- · 2019-07-20 05:21

From the getaddrinfo documentation page :

Minimum supported client | Windows 8.1, Windows Vista

However :

The getaddrinfo function was added to the Ws2_32.dll on Windows XP and later.

You just need to use getaddrinfo instead of GetAddrInfo on XP before SP2. On XP SP2 and later, your code just works, nothing to do.
As a side note, GetAddrInfo in ASCII mode is an alias to GetAddrInfoA, which is actually an alias of getaddrinfo (even on recent Windows), so using one or the other doesn't matter.

Bonus :

On versions before XP (for example, Windows 2000), You need to include an additional header, Wspiapi.h, to emulate getaddrinfo :

To execute an application that uses this function on earlier versions of Windows, then you need to include the Ws2tcpip.h and Wspiapi.h files. When the Wspiapi.h include file is added, the getaddrinfo function is defined to the WspiapiGetAddrInfo inline function in the Wspiapi.h file.

查看更多
家丑人穷心不美
3楼-- · 2019-07-20 05:34

Simply because GetAddrInfo is not supported on Windows XP. If you must use this function, use GetProcAddress to get this function's address for higher version of OS, and call it dynamically.

查看更多
登录 后发表回答