Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[BETA] Retrieving the local IP address

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> Internet Programming
View previous topic :: View next topic  
Author Message
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Nov 3rd, 2010, 10:18am    Post subject: [BETA] Retrieving the local IP address Reply with quote

Below find an example of receiving the local IP address.

I would like to know, if there is a better way to retrieve the IP address list items?

It would be so much easier, if LB would support handling of arrays of pointers natively.

Thanks in advance.

Please use the code in the next post!

Code:

    'GetIP.bas

    open "Ws2_32.dll" for dll as #Ws2

    struct lpWSAData,_
        wVersion       as word,_
        wHighVersion   as word,_
        szDescription  as char[257],_
        szSystemStatus as char[129],_
        iMaxSockets    as ushort,_
        iMaxUdpDg      as ushort,_
        lpVendorInfo   as ptr

    wVersionRequested = hexdec("22")

    ' initialize wesock
    calldll #Ws2, "WSAStartup",_
        wVersionRequested as word,_
        lpWSAData         as struct,_
        Result            as long

    if Result = 0 then
        length = 100
        hostname$ = space$(length+1)

        ' get computer name
        calldll #Ws2, "gethostname",_
            hostname$ as ptr,_
            length    as long,_
            Result    as long

        struct hostent,_
            hname     as ptr,_
            haliases  as ptr,_
            haddrtype as short,_
            hlength   as short,_
            haddrlist as ptr

        hostentLen = len(hostent.struct)

        ' dummy to receive pointer to ip address array item
        struct haddr,_
            ip as ptr

        haddrLen = len(haddr.struct)

        ' get ip information
        calldll #Ws2, "gethostbyname",_
            hostname$ as ptr,_
            phostent  as ulong

        ' copy memory to struct
        calldll #kernel32, "RtlMoveMemory", _
            hostent    as struct,_
            phostent   as ulong, _
            hostentLen as ulong, _
            Result     as void

        pointer = hostent.haddrlist.struct
        iplength = hostent.hlength.struct

        IpAddressArray$ = winstring(pointer)

        ' cycle through ip address array pointers
        while IpAddressArray$ <> ""
            ' copy ip address to dumy structure
            calldll #kernel32, "RtlMoveMemory", _
                haddr    as struct,_
                pointer  as ulong, _
                haddrLen as ulong, _
                Result   as void

            paddr = haddr.ip.struct

            ' convert to dotted format
            for i = 0 to iplength-1
                if i = 0 then
                    IPaddress$ = str$(asc(left$(winstring(paddr),1)))
                else
                    IPaddress$ = IPaddress$; "."; asc(left$(winstring(paddr+i),1))
                end if
            next

            print IPaddress$

            ' advance to next array item
            pointer = pointer + 4
            IpAddressArray$ = winstring(pointer)
        wend

        ' close wesock
        calldll #Ws2, "WSACleanup",_
            Result as long
    end if

    close #Ws2

_________________
Stefan

Any code I post can be freely used, just give credit.


Last edited by STPendl on Nov 4th, 2010, 9:13pm; edited 2 times in total
Back to top
View user's profile Send private message
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Nov 3rd, 2010, 9:02pm    Post subject: Re: [BETA] Retrieving the local IP address Reply with quote

Below find an updated version, which does not use RtlMoveMemory.

Thanks to Brent for the inspiration of his server/client code.

I would be interested, if this leads to any errors and if the results are correct.

Code:

    'GetIP.bas
    'Author: Stefan Pendl
    'Date: 02.11.10

    open "Ws2_32.dll" for dll as #Ws2

    struct lpWSAData,_
        wVersion       as word,_
        wHighVersion   as word,_
        szDescription  as char[257],_
        szSystemStatus as char[129],_
        iMaxSockets    as ushort,_
        iMaxUdpDg      as ushort,_
        lpVendorInfo   as ulong

    wVersionRequested = hexdec("0202")

    ' initialize wesock
    calldll #Ws2, "WSAStartup",_
        wVersionRequested as word,_
        lpWSAData         as struct,_
        Result            as long

    if Result = 0 then
        length = 100
        hostname$ = space$(length+1)

        ' get computer name
        calldll #Ws2, "gethostname",_
            hostname$ as ptr,_
            length    as long,_
            Result    as long

        struct hostent,_
            hname     as ulong,_
            haliases  as ulong,_
            haddrtype as short,_
            hlength   as short,_
            haddrlist as ulong

        ' dummy to receive pointer to ip address array item
        struct haddr,_
            ip as ulong

        ' get ip information
        calldll #Ws2, "gethostbyname",_
            hostname$ as ptr,_
            phostent  as ulong

        ' copy memory to struct
        hostent.struct = phostent

        pointer = hostent.haddrlist.struct
        iplength = hostent.hlength.struct

        IpAddressArray$ = winstring(pointer)

        ' cycle through ip address array pointers
        while IpAddressArray$ <> ""
            ' copy ip address to dumy structure
            haddr.struct = pointer

            paddr = haddr.ip.struct

            ' convert to dotted format
            for i = 0 to iplength-1
                if i = 0 then
                    IPaddress$ = str$(asc(left$(winstring(paddr),1)))
                else
                    IPaddress$ = IPaddress$; "."; asc(left$(winstring(paddr+i),1))
                end if
            next

            print IPaddress$

            ' advance to next array item
            pointer = pointer + 4
            IpAddressArray$ = winstring(pointer)
        wend

        ' close wesock
        calldll #Ws2, "WSACleanup",_
            Result as long
    end if

    close #Ws2

_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
JanetTerra
Junior Member


Joined: 26 Apr 2008
Posts: 11
Location: Massachusetts, USA

PostPosted: Nov 3rd, 2010, 10:10pm    Post subject: Re: [BETA] Retrieving the local IP address Reply with quote

Gives me the expected number, at least the number I get when I hover over my wireless connection icon. I didn't encounter any errors.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> Internet Programming All times are GMT
Page 1 of 1
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group