GetAdaptersInfo

Just starting out? Need help? Post your questions and find answers here.
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

GetAdaptersInfo

Post by PWS32 »

please tranlate this code to PB4.0 im have no experience to do this, THX

Code: Select all

; GetNetworkAdapters - 09/24/2003 updated/modified by TerryHough
; from PB forums by AngelSoul
; post http://jconserv.net/purebasic/viewtopic.php?t=7347

; The Lease Obtained and Lease Expired fields are incorrect.  Need to know
; how to convert them from long date to date.

Global Hostname$.s
Global DNSServer$.s
Global Adapter$.s

Declare GetAdaptersInfo(Total)
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.b[4]
  LeaseExpires.b[4]
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

Dim Adapters.IP_INFO(10)

GetAdaptersInfo(Total)
If Total = -1
  MessageRequester("Error","An error occured accessing adapter info",#MB_ICONERROR)
  End
Else
  MessageRequester("IP Configuration Report",Adapter$,#MB_ICONINFORMATION)
EndIf
End

Procedure GetAdaptersInfo(Total)
  Shared Total
  Shared Adapter$
  res=GetNetworkParams_(0,@FixedInfoSize.l)
  If res<>0
    If res<>#ERROR_BUFFER_OVERFLOW
      Total=-1
      ProcedureReturn Total
    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)
    Adapter$ = "Host name: " + Hostname$ + Chr(10)
    Domain$  = PeekS(@FixedInfo\DomainName)
    Adapter$ = Adapter$ + "Domain name: " + Domain$ + Chr(10)
    DNSServer$=PeekS(@FixedInfo\DnsServerList\IP)
    Adapter$ = Adapter$ + "DNS Servers: " + DNSServer$ + Chr(10)
    pAddrStr.l=PeekL(@FixedInfo\DnsServerList\Nxt)
    Repeat
      CopyMemory(pAddrStr,@Buffer.IP_ADDR_STRING,SizeOf(IP_ADDR_STRING))
      pAddrStr =Buffer\Nxt
    Until pAddrStr=0
    Select PeekL(@FixedInfo\NodeType)
      Case 1 : Adapter$ + "Node Type: Broadcast" + Chr(10)
      Case 2 : Adapter$ + "Node Type: Peer to Peer" + Chr(10)
      Case 4 : Adapter$ + "Node Type: Mixed" + Chr(10)
      Case 8 : Adapter$ + "Node Type: Hybrid" + Chr(10)
      Default: Adapter$ + "Node Type: Unknown" + Chr(10)
    EndSelect
    Select PeekL(@FixedInfo\EnableRouting)
      Case 0 : Adapter$ + "IP Routing: Not Enabled" + Chr(10)
      Default: Adapter$ + "Node Type: Enabled" + Chr(10)
    EndSelect
    Select PeekL(@FixedInfo\EnableProxy)
      Case 0 : Adapter$ + "WINS Proxy: Not enabled" + Chr(10)
      Default: Adapter$ + "WINS Proxy: Enabled" + Chr(10)
    EndSelect
    Select PeekL(@FixedInfo\EnableDns)
      Case 0 : Adapter$ + "NetBIOS does not use DNS" + Chr(10)
      Default: Adapter$ + "NetBIOS uses DNS" + Chr(10)
    EndSelect
    Adapter$ = Adapter$ + Chr(10)
  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))
    pAddrStr=Buffer2\IpAddressList\Nxt
    Repeat
      CopyMemory(@Buffer2\IpAddressList,@Buffer,SizeOf(IP_ADDR_STRING))
      Total=Total+1
      Adapter$ + "Adapter: " + PeekS(@Buffer2\Description) + Chr(10)
      Select Buffer2\Type
        Case  1 :Adapter$ + "Type: " + Str(Buffer2\Type) + " Ethernet Adapter" + Chr(10)
        Case  2 :Adapter$ + "Type: " + Str(Buffer2\Type) + " Token Ring Adapter" + Chr(10)
        Case  3 :Adapter$ + "Type: " + Str(Buffer2\Type) + " FDDI Adapter" + Chr(10)
        Case  4 :Adapter$ + "Type: " + Str(Buffer2\Type) + " PPP Adapter" + Chr(10)
        Case  5 :Adapter$ + "Type: " + Str(Buffer2\Type) + " Loopback Adapter" + Chr(10)
        Case  6 :Adapter$ + "Type: " + Str(Buffer2\Type) + " Slip Adapter" + Chr(10)
        Case 23 :Adapter$ + "Type: " + Str(Buffer2\Type) + " PPPoE Adapter" + Chr(10)
        Default :Adapter$ + "Type: " + Str(Buffer2\Type) + " Unknown Adapter" + Chr(10)
      EndSelect
      Adapter$ + "IP Address: " + PeekS(@Buffer\IP) + Chr(10)
      Adapter$ + "Subnet Mask: " + PeekS(@Buffer\mask) + Chr(10)
      pAddrStr=Buffer\Nxt
      If pAddrStr<>0
        CopyMemory(pAddrStr,@Buffer2\GatewayList,SizeOf(IP_ADDR_STRING))
      EndIf
    Until pAddrStr=0
    If PeekS(@Buffer2\GatewayList\IP)
      Adapter$ + "Default Gateway: " +PeekS(@Buffer2\GatewayList\IP) + Chr(10)
    EndIf
    
    pAdapt=Buffer2\Nxt
    If pAdapt<>0
      CopyMemory(pAdapt,@AdapterInfo,SizeOf(IP_ADAPTER_INFO))
    EndIf
    If PeekS(@Buffer2\DhcpServer\IP)
      Adapter$ + "DHCP Server: " +PeekS(@Buffer2\DhcpServer\IP) + Chr(10)
    EndIf
    If PeekS(@Buffer2\PrimaryWinsServer)
      Adapter$ + "Primary WINS Server: " +PeekS(@Buffer2\PrimaryWinsServer) + Chr(10)
    EndIf
    If PeekS(@Buffer2\SecondaryWinsServer)
      Adapter$ + "Secondary WINS Server: " +PeekS(@Buffer2\SecondaryWinsServer) + Chr(10)
    EndIf
    Adapter$ + "Lease obtained: " + FormatDate("%mm/%dd/%yyyy %hh;%mm:%ss",PeekL(@Buffer2\LeaseObtained)) + Chr(10)
    Adapter$ + "Lease expires: "  + FormatDate("%mm/%dd/%yyyy %hh;%mm:%ss",PeekL(@Buffer2\LeaseExpires))  + Chr(10)
    Adapter$ + Chr(10)
    
  Until pAdapt=0
EndProcedure 
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Code: Select all

; English forum: http://purebasic.myforums.net/viewtopic.php?t=7347
; Author: AngelSoul
; Date: 26. August 2003
; Update for PB4 by ts-soft
; List adapters with their respective IPs
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) 
    DNSServer$=PeekS(@FixedInfo\DnsServerList\IP) 
    pAddrStr.l=@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) 
      Adapters(Total)\AdapterType=Buffer2\Type 
      Adapters(Total)\IP=PeekS(@Buffer\IP) 
      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 
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

Post by PWS32 »

thx, this code do not run by pb4.0, the error message : Array index out of bounds
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Thanks for 4.0.. no errors here.

- np
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Thanks ts-soft
PWS32 wrote:thx, this code do not run by pb4.0, the error message : Array index out of bounds
PWS32, if you know the number of adaptors, just change the dimension. If you don't know the number of adaptors then try this:

Code: Select all

Global Dim Adapters.IP_INFO(10)
; Right after the initial dimension of Adapters (the line above)
; insert the line below.
Global curDim = 10

; then

      Total=Total+1
; at about line 122 you get the above
; Insert this code right after it
      If Total > curDim
        curDim * 2
        ReDim Adapters.IP_INFO(curDim)
      EndIf
Dare2 cut down to size
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

Post by PWS32 »

Hi ts-soft and Dare
many thanks for your help!, now runnig this code
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

Post by PWS32 »

Hi,
im have found this code here in this Forum and have the code a little bit extended

Code: Select all

; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphlp/getadaptersinfo.asp
; with the help of IPHlpAPI.inc from the Powerbasic include-file for the structures
; Microsoft isn´t quite a help here :-)

DHCPServerEnabled.s

Structure IP_ADDR_STRING
  pNext.l
  IpAddress.b[16]
  IpMask.b[16]
  Context.l
EndStructure

Structure IP_ADAPTER_INFO
Next.l
ComboIndex.l
AdapterName.b[260] ; MAX_ADAPTER_NAME_LENGTH + 4
Description.b[132] ; MAX_ADAPTER_DESCRIPTION_LENGTH + 4
AdressLength.l
Address.b[8] ; MAX_ADAPTER_ADDRESS_LENGTH
Index.l
Type.l
DhcpEnabled.l
CurrentIpAddressPTR.l
IpAddressList.IP_ADDR_STRING
GatewayList.IP_ADDR_STRING
DhcpServer.IP_ADDR_STRING
HaveWins.l
PrimaryWinsServer.IP_ADDR_STRING
SecondaryWinsServer.IP_ADDR_STRING
LeaseObtained.l
LeaseExpires.l
EndStructure

length.l=0
Result=GetAdaptersInfo_(0,@length) ; Get the length for Buffer
If Result=#ERROR_BUFFER_OVERFLOW
  *Buffer=AllocateMemory(length)
  Result=GetAdaptersInfo_(*Buffer,@length)
  If Result=#ERROR_SUCCESS
    adapters=length/SizeOf(IP_ADAPTER_INFO)
    For x=0 To adapters-1
      Debug "AdapterNr : "+Str(x+1)
      tempipinfo.IP_ADAPTER_INFO
      For i=0 To SizeOf(IP_ADAPTER_INFO)-1
        byte.b=PeekB(*Buffer+(x*640)+i)
        PokeB(tempipinfo+i,byte)
      Next
      Debug "Adapter : "+PeekS(@tempipinfo\Description)
      Debug "IP-Adress : "+PeekS(@tempipinfo\IpAddressList+4)
      Debug "Gateway : "+PeekS(@tempipinfo\GatewayList+4)
     
      Debug "P-Wins : "+PeekS(@tempipinfo\PrimaryWinsServer+4)
      Debug "S-Wins : "+PeekS(@tempipinfo\SecondaryWinsServer+4)
      
      
      ;Debug "DHCP-Server : "+PeekS(@tempipinfo\DhcpServer+4)
      If PeekS(@tempipinfo\DhcpServer+4) = "255.255.255.255"
        Debug "STATIC IP Configuration"
       Else
        Debug "DHCP Server Adress : " +PeekS(@tempipinfo\DhcpServer+4)
      EndIf
      
      
      mac$=""
      For i=0 To 5
        byte.b=PeekB(@tempipinfo\Address+i)
        If byte>=0
          mac$+RSet(Hex(byte),2,"0")
        Else
          mac$+RSet(Hex(byte+256),2,"0")
        EndIf
        If i<5
          mac$+":"
        EndIf
      Next
      Debug "MAC-Address "+mac$
    Next
  Else
    Debug "Error : "+Str(Result)
  EndIf
EndIf 
Post Reply