我在尝试打开 Page Heap 的应用程序验证器下测试应用程序时遇到了问题。事实证明,即使对于像“localhost”这样的合法主机名,gethostbyname API 也总是失败。这个问题在我尝试过的每个 Win-7 或 Server 2008 R2 上都会重现,即使是使用 gethostbyname 的非常简单的测试应用程序。
复制步骤:在 appverifier 中检查“page heap”和“UseLFGGuard...”复选框,使用 gethostbyname(..) 运行任何应用程序。
应用程序代码示例(appverifier 关闭时打印“127.0.0.1”,appverifier 开启时打印“getaddrinfo failed”):
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
void
Exercise()
{
int i = 0;
struct hostent *remoteHost;
struct in_addr addr;
remoteHost = gethostbyname("localhost");
if (remoteHost == NULL)
{
printf("gethostbyname(localhost) failed\n");
}
else
{
if (remoteHost->h_addrtype == AF_INET)
{
i=0;
while (remoteHost->h_addr_list[i] != 0)
{
addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
printf("\tIP Address #%d: %s\n", i, inet_ntoa(addr));
}
}
else
{
printf("unexpected address type\n");
}
}
}
int
main()
{
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
for(int i=0; i<1000; i++)
{
Exercise();
Sleep(1000);
}
return 0;
}
最不寻常的是,我在互联网上找不到任何东西。这是一个已知问题吗?任何解决方法?