|
-
Jan 5th, 2001, 12:21 AM
#1
Thread Starter
Frenzied Member
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, 06:26 AM
#2
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.
-
Jan 5th, 2001, 07:59 AM
#3
Frenzied Member
Here's a part from the API-Guide in VB, I think you'll be a ble to translate it (I'm not *yet* )
Code:
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
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Jan 5th, 2001, 09:33 AM
#4
Thread Starter
Frenzied Member
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
-
Jan 5th, 2001, 09:40 AM
#5
Frenzied Member
Well they declare it as long
Code:
Dim lpHost As Long
lpHost = gethostbyname(sHostName)
I don't know C++ very well, so good luck
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Jan 5th, 2001, 12:08 PM
#6
Thread Starter
Frenzied Member
That's because a long in VB is the same as an int in C++...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|