64 位 Windows 上的 gethostbyname 问题
我正在尝试将一些代码从 32 位 Windows(XP 和 Server 2003)迁移到 64 位 Windows 7,但在 gethostbyname 方面遇到了一个奇怪的问题。
我正在做这样的事情:
struct hostent *hp;
hp = gethostbyname( host );
调用 gethostbyname 后,指针 hp->h_addr_list 无效。它具有所有正确的数字,但看起来 32 位指针不知何故被塞到了错误的空间中。
例如,我得到的是
hp->h_addr_list = 0x0064bdd800000000
如果我手动交换前半部分和后半部分,以便 hp->h_addr_list = 0x000000000064bdd8 则指针有效并指向正确的数据。
有时我也得到 baadf00d 而不是零(即 hp->h_addr_list = 0x0064bdd8baadf00d),
据我所知,所有内容都链接到正确的 64 位版本的 Winsock 库,所以我不确定是什么导致了这种情况类型的问题。
I am trying to migrate some code from 32-bit Windows (XP and Server 2003) to 64-bit Windows 7, and I am having a weird problem with gethostbyname.
I'm doing something like this:
struct hostent *hp;
hp = gethostbyname( host );
After the call to gethostbyname, the pointer hp->h_addr_list is invalid. It has all the right numbers, but it looks like a 32-bit pointer got stuffed into the wrong space somehow.
For example, what I get is
hp->h_addr_list = 0x0064bdd800000000
If I manually swap the first and last half so that hp->h_addr_list = 0x000000000064bdd8 then the pointer is valid and points to the correct data.
Sometimes I get baadf00d instead of zeros too (i.e. hp->h_addr_list = 0x0064bdd8baadf00d),
As far as I can tell everything is linked to the correct 64-bit version of the winsock libraries, so I'm not sure what could cause this kind of problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想尝试使用
getaddrinfo()
代替。 gethostbyname 的文档指出其用法不鼓励并且现在已弃用(因此其中很可能存在错误)。我对您遇到的问题没有任何经验,但我知道我使用 getaddrinfo() 的代码在 x86 和 x64 上的 XP 到 Windows 7 上运行良好。You might want to try using
getaddrinfo()
instead . The docs for gethostbyname point out that its usage is discouraged and that it's deprecated now (so there may well be bugs in it). I haven't had any experience with the problem that you're having but I know that the code that I have that usesgetaddrinfo()
works fine across XP through Windows 7 on x86 and x64.Microsoft 使用 baadf00d 来指示未初始化的分配堆内存,因此将其清零可能是一个不错的选择主意。进行测试以确定。
至于交换高位和低位,你是对的,它确实被放到了错误的位置。漏洞?
baadf00d is used by Microsoft to indicate uninitialized allocated heap memory, so zeroing that out is probably a good idea. Test to make sure.
As far as swapping the high and low bits, your right, it did get put into the wrong spot. Bug?
我遇到了同样的问题。问题是在项目设置中,结构成员对齐选项设置为 4 字节 (/Zp4)。我将此选项恢复为默认值,这解决了问题。
I encountered the same issue. The problem was that in the project settings the Struct Member Alignment option was set to 4 bytes (/Zp4). I returned this option to Default and this resolved the issue.