Page 1 of 1

Listing Adapters with their IPs

Posted: Tue Aug 26, 2003 8:03 am
by AngelSoul
Code updated for 5.20+

For those who wanted this (myself included) i converted a VB code from AllApi.Net to PB to list adapters with their respective IPs:

Code: Select all

Declare GetAdaptersInfo()
Structure IP_ADDR_STRING
  Nxt.l
  IP.b[16]
  Mask.b[16]
  Context.l
EndStructure

Structure IP_ADAPTER_INFO
  Nxt.l
  ComboIndex.l
  AdapterName.b[260]
  Description.b[132]
  AddressLength.l
  Adr.b[8]
  Index.l
  Type.l
  DhcpEnabled.l
  CurrentIpAddress.l
  IpAddressList.IP_ADDR_STRING
  GatewayList.IP_ADDR_STRING
  DhcpServer.IP_ADDR_STRING
  HaveWins.w
  PrimaryWinsServer.IP_ADDR_STRING
  SecondaryWinsServer.IP_ADDR_STRING
  LeaseObtained.l
  LeaseExpires.l
EndStructure

Structure FIXED_INFO
  HostName.b[132]
  DomainName.b[132]
  CurrentDnsServer.l
  DnsServerList.IP_ADDR_STRING
  NodeType.l
  ScopeId.b[260]
  EnableRouting.l
  EnableProxy.l
  EnableDns.l
EndStructure

Structure IP_INFO
  AdapterName.s
  IP.s
  AdapterType.l
  AdapterTypeDesc.s
EndStructure

Global Dim Adapters.IP_INFO(10)

GetAdaptersInfo()
If Total=-1:MessageRequester("Error","An error occured accessing adapter info",0):End:EndIf
For gg=1 To Total
  Debug "Adapter: "+Adapters(gg)\AdapterName
  Debug "Type: "+Adapters(gg)\AdapterTypeDesc
  Debug "IP: "+Adapters(gg)\IP
  Debug Adapters(gg)\AdapterType
  Debug ""
Next

End


Procedure GetAdaptersInfo()
  Shared Total
  
  res=GetNetworkParams_(0,@FixedInfoSize.l)
  If res<>0
    If res<>#ERROR_BUFFER_OVERFLOW
      Total=-1:ProcedureReturn
      End
    EndIf
  EndIf
  Dim FixedInfoBuffer.b(FixedInfoSize-1)
  res = GetNetworkParams_(@FixedInfoBuffer(0), @FixedInfoSize)
  If res=0
    CopyMemory(@FixedInfoBuffer(0),@FixedInfo.FIXED_INFO,SizeOf(FIXED_INFO))
    Hostname$=PeekS(@FixedInfo\Hostname, -1, #PB_Ascii)
    DNSServer$=PeekS(@FixedInfo\DnsServerList\IP, -1, #PB_Ascii)
    pAddrStr = PeekL(@FixedInfo\DnsServerList\Nxt)
    Repeat
      CopyMemory(pAddrStr,@Buffer.IP_ADDR_STRING,SizeOf(IP_ADDR_STRING))
      pAddrStr =Buffer\Nxt
    Until pAddrStr=0
  EndIf
  
  AdapterInfoSize.l=0
  res=GetAdaptersInfo_(0, @AdapterInfoSize)
  If res<>0
    If res<>#ERROR_BUFFER_OVERFLOW
      Total=-1:ProcedureReturn
    EndIf
  EndIf
  Dim AdapterInfoBuffer.b(AdapterInfoSize - 1) ;OK
  res=GetAdaptersInfo_(@AdapterInfoBuffer(0), @AdapterInfoSize.l) ;OK
  
  CopyMemory(@AdapterInfoBuffer(0),@AdapterInfo.IP_ADAPTER_INFO,SizeOf(IP_ADAPTER_INFO))
  pAdapt=AdapterInfo\Nxt
  
  Repeat
    CopyMemory(@AdapterInfo,@Buffer2.IP_ADAPTER_INFO,SizeOf(IP_ADAPTER_INFO))
    Select Buffer2\Type
      Case 1:AdType$="Ethernet Adapter"
      Case 2:AdType$="Token Ring Adapter"
      Case 3:AdType$="FDDI Adapter"
      Case 4:AdType$="PPP Adapter"
      Case 5:AdType$="Loopback Adapter"
      Case 6:AdType$="Slip Adapter"
      Case 23:AdType$="PPPoE Adapter"
      Default:AdType$="Unknown Adapter"
    EndSelect
    
    
    pAddrStr=Buffer2\IpAddressList\Nxt
    Repeat
      CopyMemory(@Buffer2\IpAddressList,@Buffer,SizeOf(IP_ADDR_STRING))
      Total=Total+1
      Adapters(Total)\AdapterName=PeekS(@Buffer2\Description, -1, #PB_Ascii)
      Adapters(Total)\AdapterType=Buffer2\Type
      Adapters(Total)\IP=PeekS(@Buffer\IP, -1, #PB_Ascii)
      Adapters(Total)\AdapterTypeDesc=AdType$
      pAddrStr=Buffer\Nxt
      If pAddrStr<>0:CopyMemory(pAddrStr,@Buffer2\GatewayList,SizeOf(IP_ADDR_STRING)):EndIf
    Until pAddrStr=0
    
    pAdapt=Buffer2\Nxt
    If pAdapt<>0:CopyMemory(pAdapt,@AdapterInfo,SizeOf(IP_ADAPTER_INFO)):EndIf
    
  Until pAdapt=0
EndProcedure
Cheers!

Posted: Tue Aug 26, 2003 11:45 am
by freak
Hmm, I get "Error at line 84 : Source memory pointer is 0."

Timo

Posted: Tue Aug 26, 2003 3:11 pm
by AngelSoul
Replace line 82 with this:

Code: Select all

pAddrStr.l=@FixedInfo\DnsServerList\Nxt
tell me if it works

Posted: Tue Aug 26, 2003 3:19 pm
by freak
Yep, works fine now.

btw: moved to the Tricks'n' Tips section, because it looks like a good tip to me 8)

Timo

Posted: Tue Aug 26, 2003 5:18 pm
by fsw
On Win2kSp3 both versions work nice.
@Freak which WIN version do you have :?:

Posted: Tue Aug 26, 2003 6:20 pm
by freak
also Win2kSp3 (... or did I install Sp4 yet?)

Timo

Posted: Tue Aug 26, 2003 11:59 pm
by Kale
Works fine with WinXP Home SP1 :D

Posted: Wed Aug 27, 2003 12:30 am
by Karbon
Works well on XP Pro SP1 too

Posted: Thu Aug 28, 2003 6:30 am
by AngelSoul
Thanks for the feedback, i wondered where the thread went lol

Cheers!

Resolved. [I've fixed code]

Posted: Fri May 19, 2006 11:14 pm
by Thunder93
Is this normal that this code doesn’t retrieve systems secondary DNS server?

Re: Listing Adapters with their IPs

Posted: Thu Jan 02, 2020 4:16 pm
by Zerosurf
Hi, I tried to run this code and following error occurs:
Line 2: Structure already declared: IP_ADDR_STRING (in a resident file).
Could some one please explain me, what that means?

SIncerely

Re: Listing Adapters with their IPs

Posted: Thu Jan 02, 2020 5:43 pm
by Denis
Zerosurf wrote:Hi, I tried to run this code and following error occurs:
Line 2: Structure already declared: IP_ADDR_STRING (in a resident file).
Could some one please explain me, what that means?

SIncerely

There is 720 structures predefined in PB inside resident file (.res) with the last PB version.
IP_ADDR_STRING is declared in a resident file (), so you can remove the declaration in PB code or set the structure as a comment like this

Code: Select all

; Structure IP_ADDR_STRING
;   Nxt.l
;   IP.b[16]
;   Mask.b[16]
;   Context.l
; EndStructure
structure could not be declared multiple times.