PDA

Click to See Complete Forum and Search --> : help with using structure


softwareguy74
Jan 4th, 2001, 11:21 PM
hi,

I'm trying to use the gethostbyname() function from the winsock library.

A successful call to gethostbyname() will return a pointer to the HOSTENT structure.

So, I call it as follows:

HOSTENT Hostinfo;

Hostinfo = gethostbyname("www.yahoo.com");

But, it doesn't like that for some reason. The compiler error I get is:

"error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct hostent *' (or there is no acceptable conversion)"

Am I calling it wrong? I have used structures before without a problem but this one seems to be giving me a problem..

Any ideas?

Jan 5th, 2001, 05:26 AM
this might be your problem,

I took this out of MSDN


The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls.

Jop
Jan 5th, 2001, 06:59 AM
Here's a part from the API-Guide in VB, I think you'll be a ble to translate it (I'm not *yet* ;))


Public Function GetIPAddress() As String
Dim sHostName As String * 256
Dim lpHost As Long
Dim HOST As HOSTENT
Dim dwIPAddr As Long
Dim tmpIPAddr() As Byte
Dim I As Integer
Dim sIPAddr As String
If Not SocketsInitialize() Then
GetIPAddress = ""
Exit Function
End If
If gethostname(sHostName, 256) = SOCKET_ERROR Then
GetIPAddress = ""
MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & " has occurred. Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If
sHostName = Trim$(sHostName)
lpHost = gethostbyname(sHostName)
If lpHost = 0 Then
GetIPAddress = ""
MsgBox "Windows Sockets are not responding. " & "Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If
CopyMemoryIP HOST, lpHost, Len(HOST)
CopyMemoryIP dwIPAddr, HOST.hAddrList, 4
ReDim tmpIPAddr(1 To HOST.hLen)
CopyMemoryIP tmpIPAddr(1), dwIPAddr, HOST.hLen
For I = 1 To HOST.hLen
sIPAddr = sIPAddr & tmpIPAddr(I) & "."
Next
GetIPAddress = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
SocketsCleanup
End Function

softwareguy74
Jan 5th, 2001, 08:33 AM
Okay,

In Visual Basic, it works fine. But in C++, when I try to save the return value of gethostbyname() to an int, it tells me:

cannot convert from 'struct hostent *' to 'int'

I called it as:

int pHost;
HOSTENT Host;

pHost = gethostbyname("www.yahoo.com);

I guess I need to do some sort of conversion from a struct to an int. How can this be acheived?

Thanks,

Dan

Jop
Jan 5th, 2001, 08:40 AM
Well they declare it as long


Dim lpHost As Long
lpHost = gethostbyname(sHostName)


I don't know C++ very well, so good luck :(

softwareguy74
Jan 5th, 2001, 11:08 AM
That's because a long in VB is the same as an int in C++...