Results 1 to 6 of 6

Thread: help with using structure

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    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?


    Visual Studio 2010

  2. #2
    Guest
    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.

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    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

    Visual Studio 2010

  5. #5
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    That's because a long in VB is the same as an int in C++...

    Visual Studio 2010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width