views:

223

answers:

2

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.

A: 

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?

Chris Lively
A: 

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.

Len Holgate
getaddrinfo() appears to be working for me, I'm not sure what the problem was with gethostbyname, it very well could just be a bug.
bde