문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top