IP Address

For issues with communication ports, protocols, etc.
Post Reply
Ralir
Posts: 10
Joined: Oct 14, 2006 19:58
Contact:

IP Address

Post by Ralir »

hi

i was just wondering if there is a SIMPLE way to find the IP Address of the user's computer
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

127.0.0.1
{Nathan}
Posts: 301
Joined: Jun 04, 2005 15:18
Location: Ohio
Contact:

Post by {Nathan} »

In windows:
www.whatismyip.com

In linux:
ifconfig
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

In windows you can use ipconfig (NT) or winipcfg (9x).

I assume he means via FreeBASIC.
Ralir
Posts: 10
Joined: Oct 14, 2006 19:58
Contact:

Post by Ralir »

sorry, i meant via freebasic
i was tired and was trying to get done on my computer ASAP

but i DID need a way to normally find my IP address, so thanks for that anyway!
Oz
Posts: 586
Joined: Jul 02, 2005 14:21
Location: Waterloo, Ontario, Canada
Contact:

Post by Oz »

What library are you using for networking?

Oz~
xterm
Posts: 102
Joined: Jun 19, 2006 8:35

Post by xterm »

In windows and for the first network adapters first ip address this should work reliably. Be aware that there could be more than one network adapter in a system and each network adapter could have more than one ip address.

If there are more than one network adapters and more than one ip addresses in a system you need to walk through linked lists specified by:

@net_adapt_info.IpAddressList.IpAddress.Next (for the next ip address)
and
@net_adapt_info.Next (for the next network adapter)

Reference:
http://msdn.microsoft.com/library/defau ... rsinfo.asp

For more advanced, more crossplatform compatible and more freebasic like methods, I guess it could help to watch out for MichaelWs, D.J.Peters' and Chaos' postings.

Code: Select all

#include once "windows.bi"
#include once "win/iptypes.bi"
#include once "win/iphlpapi.bi"

Dim net_adapt_info    As IP_ADAPTER_INFO
dim as uinteger ui_len_net_adapt_info,ui_DhcpEnabled,ui_HaveWins
dim as any ptr buf_adapt_info,p_next_adapter 
dim as string s_my_ip_address,s_gateway_ip_address,s_dhcp_serv_ip_address
dim as string s_AdapterName,s_Description,s_mac_address,s_PrimaryWinsServer,s_SecondaryWinsServer
dim as long AddressLength, adapter_num

ui_len_net_adapt_info=0

GetAdaptersInfo (0, @ui_len_net_adapt_info)

if ui_len_net_adapt_info>0 then
    buf_adapt_info=callocate (ui_len_net_adapt_info)

    If GetAdaptersInfo(buf_adapt_info, @ui_len_net_adapt_info) = ERROR_SUCCESS Then

        adapter_num=1 

        CopyMemory(@net_adapt_info,buf_adapt_info,ui_len_net_adapt_info)

        s_my_ip_address=*cptr(zstring ptr,@net_adapt_info.IpAddressList.IpAddress)
        s_gateway_ip_address=*cptr(zstring ptr,@net_adapt_info.GatewayList.IpAddress)
        s_dhcp_serv_ip_address=*cptr(zstring ptr,@net_adapt_info.DhcpServer.IpAddress)
        s_AdapterName=*cptr(zstring ptr,@net_adapt_info.AdapterName)
        s_AdapterName=net_adapt_info.AdapterName
        s_Description=trim (net_adapt_info.Description)
        ui_DhcpEnabled=net_adapt_info.DhcpEnabled
        ui_HaveWins=net_adapt_info.HaveWins
        s_PrimaryWinsServer=*cptr(zstring ptr,@net_adapt_info.PrimaryWinsServer.IpAddress)
        s_SecondaryWinsServer=*cptr(zstring ptr,@net_adapt_info.SecondaryWinsServer.IpAddress)

        for i = 0 to (net_adapt_info.AddressLength-1)
            s_mac_address=s_mac_address & hex(cubyte (net_adapt_info.Address(i)),2) & "-"
        next i
        s_mac_address=left(s_mac_address,len(s_mac_address)-1)

        print "Adapter number: " & adapter_num
        print "AdapterName: " & s_AdapterName
        print "Description: " & s_Description
        print "IPaddress: " & s_my_ip_address
        print "Gateway: " & s_gateway_ip_address
        print "DhcpEnabled: " & ui_DhcpEnabled
        print "DhcpServer: " & s_dhcp_serv_ip_address
        print "MAC address: " & s_mac_address
        print "HaveWins: " & ui_HaveWins
        print "PrimaryWinsServer: " & s_PrimaryWinsServer
        print "SecondaryWinsServer: " & s_SecondaryWinsServer

    endif

endif
deallocate(buf_adapt_info)
sleep
littlebigman
Posts: 62
Joined: May 21, 2010 11:47

Re: IP Address

Post by littlebigman »

Hello,

Resurrecting a very old thread.

I'd like to write a simple GUI alternative to ipconfig.

After adding "Dim i as uinteger", the code above does compile on Windows, but crashes when run.

Is there a simple way to get the IP + mask + gateway of all Ethernet adapters on the local host ("Ethernet adapter Local Area Connection x")?

Thank you.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: IP Address

Post by badidea »

I do not know what you mean with 'simple GUI alternative', but here is a partial translation of https://docs.microsoft.com/en-us/window ... aptersinfo:

Code: Select all

#include once "win/winsock2.bi"
#include once "win/iphlpapi.bi"

dim as PIP_ADAPTER_INFO pAdapterInfo, pAdapter 'pointers

pAdapterInfo = cast(IP_ADAPTER_INFO ptr, allocate(sizeof(IP_ADAPTER_INFO)))
if pAdapterInfo = 0 then
	print "Error allocating memory needed to call GetAdaptersinfo"
	end 1
end if

' Make an initial call to GetAdaptersInfo to get
' the necessary size into the ulOutBufLen variable
dim as ulong ulOutBufLen = sizeof(IP_ADAPTER_INFO)
if GetAdaptersInfo(pAdapterInfo, @ulOutBufLen) = ERROR_BUFFER_OVERFLOW then
	deallocate pAdapterInfo
	pAdapterInfo = cast(IP_ADAPTER_INFO ptr, allocate(ulOutBufLen))
	if pAdapterInfo = 0 then
		print "Error allocating memory needed to call GetAdaptersinfo"
		end 1
	end if
end if

dim as ulong dwRetVal = GetAdaptersInfo(pAdapterInfo, @ulOutBufLen)
if dwRetVal <> NO_ERROR then
	print "GetAdaptersInfo failed with error: " & dwRetVal
else 'no error
	pAdapter = pAdapterInfo
	while pAdapter
		print "ComboIndex: ", pAdapter->ComboIndex
		print "Adapter Name: ", pAdapter->AdapterName
		print "Adapter Desc: ", pAdapter->Description
		print "Adapter Addr:", 'MAC address
		for i as integer = 0 to pAdapter->AddressLength - 1
			if i = pAdapter->AddressLength - 1 then
				print hex(pAdapter->Address(i), 2)
			else
				print hex(pAdapter->Address(i), 2) & "-";
			end if
		next
		print "Index: ", pAdapter->Index
		print "Type: ",
		select case pAdapter->Type
		case MIB_IF_TYPE_OTHER:
			print "Other"
		case MIB_IF_TYPE_ETHERNET
			print "Ethernet"
		case MIB_IF_TYPE_TOKENRING
			print "Token Ring"
		case MIB_IF_TYPE_FDDI
			print "FDDI"
		case MIB_IF_TYPE_PPP
			print "PPP"
		case MIB_IF_TYPE_LOOPBACK
			print "Lookback"
		case MIB_IF_TYPE_SLIP
			print "Slip"
		case else
			print "Unknown type: ", pAdapter->Type
		end select

		print "IP Address: ", pAdapter->IpAddressList.IpAddress.String
		print "IP Mask: ", pAdapter->IpAddressList.IpMask.String
		print "Gateway: ", pAdapter->GatewayList.IpAddress.String

		pAdapter = pAdapter->Next
		print "-----------------------"
	wend
end if

if pAdapterInfo then deallocate pAdapterInfo
print "End."
getkey
Tested on Windows10 with fbc 1.06.0 32 & 64 bit.
Now quickly back to linux...
littlebigman
Posts: 62
Joined: May 21, 2010 11:47

Re: IP Address

Post by littlebigman »

I wanted a GUI alternative to Windows' ipconfig, and "simple" because I only need IP + mask + gateway infos.

I'll add a dialog box, and it'll be fine.

Thanks much.
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: IP Address

Post by marcov »

I did this once in Delphi, and I saved the URL of the bit of VBA that I translated it from:

https://docs.microsoft.com/en-us/window ... figuration

Besides the above wmi and the iphlpapi fragment from badidea, there is also info in the register, specially some info (GUIDs of "network connection") that allow you to tie bits and pieces together.
Post Reply