Listing Adapters with their IPs

Share your advanced PureBasic knowledge/code with the community.
AngelSoul
User
User
Posts: 55
Joined: Tue Jul 29, 2003 9:16 am
Location: Canada

Listing Adapters with their IPs

Post 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!
AMD 1.8 - 512mb - ATI All-In-Wonder Radeon 9000 pro - W2k Pro
BASIC programmers never die, they just return without gosub.
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Hmm, I get "Error at line 84 : Source memory pointer is 0."

Timo
quidquid Latine dictum sit altum videtur
AngelSoul
User
User
Posts: 55
Joined: Tue Jul 29, 2003 9:16 am
Location: Canada

Post by AngelSoul »

Replace line 82 with this:

Code: Select all

pAddrStr.l=@FixedInfo\DnsServerList\Nxt
tell me if it works
AMD 1.8 - 512mb - ATI All-In-Wonder Radeon 9000 pro - W2k Pro
BASIC programmers never die, they just return without gosub.
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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
quidquid Latine dictum sit altum videtur
User avatar
fsw
Addict
Addict
Posts: 1572
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

On Win2kSp3 both versions work nice.
@Freak which WIN version do you have :?:

I am to provide the public with beneficial shocks.
Alfred Hitshock
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

also Win2kSp3 (... or did I install Sp4 yet?)

Timo
quidquid Latine dictum sit altum videtur
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Works fine with WinXP Home SP1 :D
--Kale

Image
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Works well on XP Pro SP1 too
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
AngelSoul
User
User
Posts: 55
Joined: Tue Jul 29, 2003 9:16 am
Location: Canada

Post by AngelSoul »

Thanks for the feedback, i wondered where the thread went lol

Cheers!
AMD 1.8 - 512mb - ATI All-In-Wonder Radeon 9000 pro - W2k Pro
BASIC programmers never die, they just return without gosub.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Resolved. [I've fixed code]

Post by Thunder93 »

Is this normal that this code doesn’t retrieve systems secondary DNS server?
Zerosurf
New User
New User
Posts: 3
Joined: Fri Oct 14, 2005 8:18 pm

Re: Listing Adapters with their IPs

Post 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
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: Listing Adapters with their IPs

Post 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.
A+
Denis
Post Reply