Hi,
I want to get the IP address of my machine in my program and for that i
have written the following code.
I don't know why the gethostbyname() is not returning the correct
hostent structure.
hp = new hostent[];
TCHAR str[MAX_COMPUTERNAME_LENGTH+1];
TCHAR result[MAX_COMPUTERNAME_LENGTH+1];
GetComputerName(str,&len);
hp = gethostbyname(str);
if(hp!=NULL)
{
memcpy(result, hp->h_name, sizeof(result));
OR
result=(TCHAR*)hp->h_name;
}
The result that i am getting the hp=NULL.
Thanks,
Saurabh Aggrawal
<games60@hotmail.com> wrote in message
news:1120131331.929300.236040@f14g2000cwb.googlegroups.com...
Hi,
I want to get the IP address of my machine in my program and for that
i
have written the following code.
I don't know why the gethostbyname() is not returning the correct
hostent structure.
hp = new hostent;
I think this shouldn't compile, and MinGW agrees. You can't omit the
size of an array allocated using new. You don't want to allocate it
anyway. You just want a pointer:
hostent* hp;
···
TCHAR str[MAX_COMPUTERNAME_LENGTH+1];
TCHAR result[MAX_COMPUTERNAME_LENGTH+1];
GetComputerName(str,&len);
hp = gethostbyname(str);
if(hp!=NULL)
{
memcpy(result, hp->h_name, sizeof(result));
OR
result=(TCHAR*)hp->h_name;
}
The result that i am getting the hp=NULL.
Hi,
Now the code is:
hostent* hp;
TCHAR result[MAX_COMPUTERNAME_LENGTH+1];
hp = gethostbyname("localhost");
if(hp!=NULL)
{
result=(TCHAR*)hp->h_name;
}
I am still getting the same output. hp=NULL.