开发者

Issue with gethostbyname on 64-bit Windows

开发者 https://www.devze.com 2022-12-18 03:12 出处:网络
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 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 = 0x0064bdd80开发者_运维百科0000000

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.


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 uses getaddrinfo() works fine across XP through Windows 7 on x86 and x64.


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消