GetAdapterInfo

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Always Here
Always Here
Posts: 5040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

GetAdapterInfo

Post by idle »

Can't find the original post of this, adapted it to run on windows x64

edit : See here also for a more complete implementation from Hexor
https://www.purebasic.fr/german/viewtopic.php?p=258748

Code: Select all

; ABBKlaus Mon May 17, 2004 20:16
; http://msdn2.microsoft.com/en-us/library/aa365917.aspx
; with the help of IPHlpAPI.inc from the Powerbasic include-file for the structures
; Microsoft isn´t quite a help here :-)
; modified by PSW Wed Sep 06, 2006 07:49
; PB4.02 compatible since 29.05.2007 19:48
; PB4.10 compatible on 23.12.2007 21:03
; added more commands 31.12.2007 17:09
; updated to PB6.0 x86 and x64 idle  
; Added DNSlist  3/10/22

CompilerIf #PB_Compiler_Version < 610 
  ImportC "msvcrt.lib" 
  CompilerElse 
    ImportC "" 
 CompilerEndIf      
   asctime.i(a.i)
   localtime.i(a.i)
   strftime.i(a.i,b.i,c.p-ascii,d.i)
EndImport

Structure TM
  tm_sec.l
  tm_min.l
  tm_hour.l
  tm_mday.l
  tm_mon.l
  tm_year.l
  tm_wday.l
  tm_yday.l
  tm_isdst.l
EndStructure

#MAX_ADAPTER_NAME=128
#MAX_ADAPTER_NAME_LENGTH=256
#MAX_ADAPTER_DESCRIPTION_LENGTH=128
#MAX_ADAPTER_ADDRESS_LENGTH=8
#MAX_HOSTNAME_LEN=128
#MAX_DOMAIN_NAME_LEN=128
#MAX_SCOPE_ID_LEN = 256 

#MIB_IF_TYPE_OTHER     = 1
#MIB_IF_TYPE_ETHERNET  = 6
#MIB_IF_TYPE_TOKENRING = 9
#MIB_IF_TYPE_PPP       = 23
#MIB_IF_TYPE_LOOPBACK  = 24
#MIB_IF_TYPE_SLIP      = 28 
#IF_TYPE_IEEE80211     = 71 

Structure _IP_ADDR_STRING Align #PB_Structure_AlignC 
  *pnext._IP_ADDR_STRING ;
  IPAddress.IP_ADDRESS_STRING;
  Ipmask.IP_MASK_STRING;
  Context.l            ;
EndStructure   

Structure IP_ADAPTER_INFO Align #PB_Structure_AlignC 
  *pNext
  ComboIndex.l
  AdapterName.a[#MAX_ADAPTER_NAME_LENGTH+4]
  Description.a[#MAX_ADAPTER_DESCRIPTION_LENGTH+4]
  AddressLength.l
  Address.b[#MAX_ADAPTER_ADDRESS_LENGTH]
  Index.l
  Type.l
  DhcpEnabled.l
  *CurrentIpAddressPTR._IP_ADDR_STRING
  IpAddressList._IP_ADDR_STRING
  GatewayList._IP_ADDR_STRING
  DhcpServer._IP_ADDR_STRING
  HaveWins.l
  PrimaryWinsServer._IP_ADDR_STRING
  SecondaryWinsServer._IP_ADDR_STRING
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
    LeaseObtained.l
    LeaseExpires.l
  CompilerElse
    LeaseObtained.q
    LeaseExpires.q
  CompilerEndIf
EndStructure

Structure IP_ADAPTER_INDEX_MAP
  Index.l
  Name.w[#MAX_ADAPTER_NAME]
EndStructure

Structure IP_INTERFACE_INFO
  NumAdapters.l
  Adapter.IP_ADAPTER_INDEX_MAP[1]
EndStructure

Structure MyIP_ADAPTER_INFO
  Index.l
  Dhcp.l
  AdapterName.s
  Description.s
  MACAddress.s
  IPAdress.s
  GateWayAdress.s
  IPMask.s
  ;
  LeaseObtained.s
  LeaseExpires.s
EndStructure

Structure MyIP_INTERFACE_INFO
  Index.l
  Name.s
EndStructure

Structure FIXED_INFO
  HostName.a[#MAX_HOSTNAME_LEN + 4];
  DomainName.a[#MAX_DOMAIN_NAME_LEN + 4];
  *CurrentDnsServer;
  DnsServerList.IP_ADDR_STRING;
  NodeType.l;
  ScopeId.a[#MAX_SCOPE_ID_LEN + 4];
  EnableRouting.l;
  EnableProxy.l;
  EnableDns.l  ;
  
EndStructure 

Global NewList DNSinfoList.MyIP_INTERFACE_INFO() 
Global NewList MyIPAdapterList.MyIP_ADAPTER_INFO() 

Procedure GetDNSInfo() 
  
  Protected *mem, *info.FIXED_INFO,len=1024,index=1  
  Protected *pIPAddr._IP_ADDR_STRING
  
  ClearList(DNSinfoList()) 
  
  *mem = AllocateMemory(len)
  
  res = GetNetworkParams_(*mem,@len) 
  If res = 111 
    *mem = ReAllocateMemory(*mem,len) 
    res = GetNetworkParams_(*mem,@len)  
  EndIf 
  
  If res = 0 
    *info = *mem  
    
    AddElement(DNSinfoList()) 
    DNSinfoList()\Name = PeekS(@*info\DnsServerList\IpAddress\String,-1,#PB_Ascii) 
    
    *pIPAddr = *Info\DnsServerList\pNext;
    While *pIPAddr
      index+1 
      AddElement(DNSinfoList())  
      DNSinfoList()\Name = PeekS(@*pIPAddr\IpAddress\String,-1,#PB_Ascii) 
      DNSinfoList()\Index = index 
      *pIPAddr = *pIPAddr\pnext;
    Wend   
    
  EndIf 
  
  FreeMemory(*mem)
  
EndProcedure   

Procedure GetAdaptersInfo()
  Protected length.l=0,Result.l,*Buffer,*Buffer2,*ipinfo.IP_ADAPTER_INFO,*iplist._IP_ADDR_STRING
  Protected mac$,i.l,byte.b
  
  ClearList(MyIPAdapterList())
  
  Result=GetAdaptersInfo_(0,@length) ; Get the length for Buffer
  If Result=#ERROR_BUFFER_OVERFLOW And length
    *Buffer=AllocateMemory(length)
    If *Buffer And GetAdaptersInfo_(*Buffer,@length)=#ERROR_SUCCESS
      *ipinfo.IP_ADAPTER_INFO=*Buffer
      ;Global NewList MyIPAdapterList.MyIP_ADAPTER_INFO() ; declare list here
      While *ipinfo
        AddElement(MyIPAdapterList()) ; add one element
        
        MyIPAdapterList()\Index=*ipinfo\Index
        MyIPAdapterList()\AdapterName=PeekS(@*ipinfo\AdapterName,-1,#PB_Ascii)
        MyIPAdapterList()\Description=PeekS(@*ipinfo\Description,-1,#PB_Ascii)
        MyIPAdapterList()\Dhcp=*ipinfo\DhcpEnabled 
                    
        ;IP-Adress
        *iplist._IP_ADDR_STRING=*ipinfo\IpAddressList
        While *iplist
          MyIPAdapterList()\IPAdress+PeekS(@*iplist\IpAddress,-1,#PB_Ascii) 
          MyIPAdapterList()\IPMask+PeekS(@*iplist\Ipmask,-1,#PB_Ascii) 
          *iplist._IP_ADDR_STRING= *iplist\pNext
        Wend
              
        ;Gateway
        *iplist._IP_ADDR_STRING=*ipinfo\GatewayList
        While *iplist
          MyIPAdapterList()\GateWayAdress+PeekS(@*iplist\IpAddress,-1,#PB_Ascii)  
          *iplist._IP_ADDR_STRING=*iplist\pNext
        Wend
        ;Wins
        If *ipinfo\HaveWins
          ;PrimaryWinsServer
          *iplist._IP_ADDR_STRING=*ipinfo\PrimaryWinsServer
          While *iplist
            *iplist._IP_ADDR_STRING=*iplist\pNext
          Wend
          ;SecondaryWinsServer
          *iplist._IP_ADDR_STRING=*ipinfo\SecondaryWinsServer
          While *iplist
            *iplist._IP_ADDR_STRING=*iplist\pNext
          Wend
        EndIf
        ;DHCP
        If *ipinfo\DhcpEnabled
          ;DhcpServer
          *iplist._IP_ADDR_STRING=*ipinfo\DhcpServer
          While *iplist
             *iplist._IP_ADDR_STRING=*iplist\pNext
          Wend
          ;LeaseObtained
          *Buffer2=AllocateMemory(#MAXCHAR)
          If *Buffer2
            strftime(*Buffer2,#MAXCHAR,"%d.%m.%Y %H:%M:%S",localtime(@*ipinfo\LeaseObtained))
            MyIPAdapterList()\LeaseObtained = PeekS(*Buffer2, -1, #PB_Ascii) 
            FreeMemory(*Buffer2)
            *Buffer2=0
          EndIf
          ;LeaseExpires
          *Buffer2=AllocateMemory(#MAXCHAR)
          If *Buffer2
            strftime(*Buffer2,#MAXCHAR,"%d.%m.%Y %H:%M:%S",localtime(@*ipinfo\LeaseExpires))
            MyIPAdapterList()\LeaseExpires = PeekS(*Buffer2, -1, #PB_Ascii) 
            FreeMemory(*Buffer2)
            *Buffer2=0
          EndIf
        Else
           
        EndIf
         
        If *ipinfo\AddressLength
          mac$=""
          For i=0 To *ipinfo\AddressLength-1
            If i
              mac$+":"
            EndIf
            byte.b=PeekB(@*ipinfo\Address+i)
            If byte>=0
              mac$+RSet(Hex(byte),2,"0")
            Else
              mac$+RSet(Hex(byte+256),2,"0")
            EndIf
          Next
           
          MyIPAdapterList()\MACAddress=mac$
        EndIf
        *ipinfo.IP_ADAPTER_INFO=*ipinfo\pNext
      Wend
    EndIf
    If *Buffer
      FreeMemory(*Buffer)
      *Buffer=0
    EndIf
  EndIf
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  
  GetAdaptersInfo()
  
  ForEach MyIPAdapterList.MyIP_ADAPTER_INFO() 
    If MyIPAdapterList()\GateWayAdress <> "0.0.0.0"  
      
      Debug "Name " +  MyIPAdapterList()\AdapterName 
      Debug "Description " + MyIPAdapterList()\Description
      Debug "DHCP " + Str(MyIPAdapterList()\Dhcp) 
      Debug "Address " + MyIPAdapterList()\IPAdress 
      Debug "Mask " + MyIPAdapterList()\IPMask 
      Debug "Gateway " + MyIPAdapterList()\GateWayAdress 
      Debug "Index " +  MyIPAdapterList()\Index  
      Debug "mac address " + MyIPAdapterList()\MACAddress
      If MyIPAdapterList()\Dhcp 
        Debug "DHCP: LeaseObtained " + MyIPAdapterList()\LeaseObtained
        Debug "DHCP: LeaseExpires " + MyIPAdapterList()\LeaseExpires
      EndIf 
      Debug ""
    EndIf      
  Next 
  
  GetDNSInfo() 
  Debug "DNS servers list" 
  Debug ""
  
  ForEach DNSinfoList() 
    Debug DNSinfoList()\Name 
  Next 
  
CompilerEndIf 




outputs active adapters like
Name {157991F7-422E-4A99-9F5D-A3EFFC0CB6E9}
Description Intel(R) Ethernet Connection (14) I219-V
DHCP 1
Address 192.168.1.122
Mask 255.255.255.0
Gateway 192.168.1.1
Index 4
mac address FC:34:97:B8:73:8F
DHCP: LeaseObtained 03.10.2022 08:57:55
DHCP: LeaseExpires 04.10.2022 08:57:55

Name {0CA112EB-9D58-4953-A09A-6BA842C0753}
Description Intel(R) Wi-Fi 6 AX200 160MHz
DHCP 1
Address 192.168.1.123
Mask 255.255.255.0
Gateway 192.168.1.1
Index 3
mac address 44:AF:28:CE:3C:1D
DHCP: LeaseObtained 03.10.2022 11:06:07
DHCP: LeaseExpires 04.10.2022 11:06:07

DNS servers list

127.0.0.1
1.1.1.1
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: GetAdapterInfo

Post by RASHAD »

Hi idle
Fantastic :D
Thanks so much
I did not know how I missed that one or maybe I didn't but forgot about it :D
Works with Windows 11 x64 like a charm
Egypt my love
User avatar
idle
Always Here
Always Here
Posts: 5040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: GetAdapterInfo

Post by idle »

RASHAD wrote: Sat Oct 01, 2022 4:21 am Hi idle
Fantastic :D
Thanks so much
I did not know how I missed that one or maybe I didn't but forgot about it :D
Works with Windows 11 x64 like a charm
It's been sitting in the todo box for a while! :shock:
User avatar
mk-soft
Always Here
Always Here
Posts: 5334
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: GetAdapterInfo

Post by mk-soft »

Nice :wink:

Small fix dhcp date time. on x64 its a quad value

Update

Code: Select all

; Its updated on top
Last edited by mk-soft on Sat Oct 01, 2022 9:41 pm, edited 1 time in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
HeX0R
Addict
Addict
Posts: 980
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: GetAdapterInfo

Post by HeX0R »

That reminded me of my old code here: https://www.purebasic.fr/german/viewtop ... 48#p258748 (which I now ported to PB6)

But aren't your constants here wrong?

Code: Select all

#MIB_IF_TYPE_OTHER     = 0
#MIB_IF_TYPE_ETHERNET  = 1
#MIB_IF_TYPE_TOKENRING = 2
#MIB_IF_TYPE_FDDI      = 3
#MIB_IF_TYPE_PPP       = 4
#MIB_IF_TYPE_LOOPBACK  = 5
#MIB_IF_TYPE_SLIP      = 6
MSDN: https://learn.microsoft.com/en-us/windo ... apter_info
User avatar
idle
Always Here
Always Here
Posts: 5040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: GetAdapterInfo

Post by idle »

Thanks Mk-soft & Hexor updated first post

I will add link to hexor's in 1st post.
I didn't know where those constants came from, I couldn't find them in the headers.
User avatar
idle
Always Here
Always Here
Posts: 5040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: GetAdapterInfo

Post by idle »

Added DNS Server addresses
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: GetAdapterInfo

Post by infratec »

Post Reply