GetAddrInfo identifier not found

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

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?

回答1:

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.



回答2:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!