How To Call GetNetworkParams/GetAdaptersInfo From Visual Basic
Symptoms
This article illustrates how to programmatically retrieve IP configuration information similar to the IPCONFIG.EXE utility. It demonstrates how to use the IP Helper APIs GetNetworkParams() and GetAdaptersInfo() from Visual Basic.
The libraries called by the code sample in this article are only supportedon the following platforms:
Microsoft Windows 2000
Microsoft Windows 98
Microsoft Windows Millennium Edition (Me)Running it on any other platform results in an error.
Resolution
Start a new Visual Basic Standard EXE project. Form1 is created by default.On the Project menu, click Remove Form1.On the Project menu, click Add Module. Module1 is created by default.Paste the following code in the General Declarations section of Module1:
Public Const MAX_HOSTNAME_LEN = 132Public Const MAX_DOMAIN_NAME_LEN = 132Public Const MAX_SCOPE_ID_LEN = 260Public Const MAX_ADAPTER_NAME_LENGTH = 260Public Const MAX_ADAPTER_ADDRESS_LENGTH = 8Public Const MAX_ADAPTER_DESCRIPTION_LENGTH = 132Public Const ERROR_BUFFER_OVERFLOW = 111Public Const MIB_IF_TYPE_ETHERNET = 6Public Const MIB_IF_TYPE_TOKENRING = 9Public Const MIB_IF_TYPE_FDDI = 15Public Const MIB_IF_TYPE_PPP = 23Public Const MIB_IF_TYPE_LOOPBACK = 24Public Const MIB_IF_TYPE_SLIP = 28Type IP_ADDR_STRINGNext As LongIpAddress As String * 16IpMask As String * 16Context As LongEnd TypeType IP_ADAPTER_INFONext As LongComboIndex As LongAdapterName As String * MAX_ADAPTER_NAME_LENGTHDescription As String * MAX_ADAPTER_DESCRIPTION_LENGTHAddressLength As LongAddress(MAX_ADAPTER_ADDRESS_LENGTH – 1) As ByteIndex As LongType As LongDhcpEnabled As LongCurrentIpAddress As LongIpAddressList As IP_ADDR_STRINGGatewayList As IP_ADDR_STRINGDhcpServer As IP_ADDR_STRINGHaveWins As BytePrimaryWinsServer As IP_ADDR_STRINGSecondaryWinsServer As IP_ADDR_STRINGLeaseObtained As LongLeaseExpires As LongEnd TypeType FIXED_INFOHostName As String * MAX_HOSTNAME_LENDomainName As String * MAX_DOMAIN_NAME_LENCurrentDnsServer As LongDnsServerList As IP_ADDR_STRINGNodeType As LongScopeIdAs String * MAX_SCOPE_ID_LENEnableRouting As LongEnableProxy As LongEnableDns As LongEnd TypePublic Declare Function GetNetworkParams Lib “IPHlpApi.dll” _(FixedInfo As Any, pOutBufLen As Long) As LongPublic Declare Function GetAdaptersInfo Lib “IPHlpApi.dll” _(IpAdapterInfo As Any, pOutBufLen As Long) As LongPublic Declare Sub CopyMemory Lib “kernel32″ Alias “RtlMoveMemory” _(Destination As Any, Source As Any, ByVal Length As Long)Sub main()Dim error As LongDim FixedInfoSize As LongDim AdapterInfoSize As LongDim i As IntegerDim PhysicalAddressAs StringDim NewTime As DateDim AdapterInfo As IP_ADAPTER_INFODim AddrStr As IP_ADDR_STRINGDim FixedInfo As FIXED_INFODim Buffer As IP_ADDR_STRINGDim pAddrStr As LongDim pAdapt As LongDim Buffer2 As IP_ADAPTER_INFODim FixedInfoBuffer() As ByteDim AdapterInfoBuffer() As Byte’ Get the main IP configuration information for this machine’ using a FIXED_INFO structure.FixedInfoSize = 0error = GetNetworkParams(ByVal 0&, FixedInfoSize)If error <> 0 ThenIf error <> ERROR_BUFFER_OVERFLOW ThenMsgBox “GetNetworkParams sizing failed with error ” & errorExit SubEnd IfEnd IfReDim FixedInfoBuffer(FixedInfoSize – 1)error = GetNetworkParams(FixedInfoBuffer(0), FixedInfoSize)If error = 0 ThenCopyMemory FixedInfo, FixedInfoBuffer(0), FixedInfoSizeMsgBox “Host Name:” & FixedInfo.HostNameMsgBox “DNS Servers:” & FixedInfo.DnsServerList.IpAddresspAddrStr = FixedInfo.DnsServerList.NextDo While pAddrStr <> 0CopyMemory Buffer, ByVal pAddrStr, LenB(Buffer)MsgBox “DNS Servers:” & Buffer.IpAddresspAddrStr = Buffer.NextLoopSelect Case FixedInfo.NodeTypeCase 1MsgBox “Node type: Broadcast”Case 2MsgBox “Node type: Peer to peer”Case 4MsgBox “Node type: Mixed”Case 8MsgBox “Node type: Hybrid”Case ElseMsgBox “Unknown node type”End SelectMsgBox “NetBIOS Scope ID:” & FixedInfo.ScopeIdIf FixedInfo.EnableRouting ThenMsgBox “IP Routing Enabled “ElseMsgBox “IP Routing not enabled”End IfIf FixedInfo.EnableProxy ThenMsgBox “WINS Proxy Enabled “ElseMsgBox “WINS Proxy not Enabled “End IfIf FixedInfo.EnableDns ThenMsgBox “NetBIOS Resolution Uses DNS “ElseMsgBox “NetBIOS Resolution Does not use DNS”End IfElseMsgBox “GetNetworkParams failed with error ” & errorExit SubEnd If’ Enumerate all of the adapter specific information using the’ IP_ADAPTER_INFO structure.’ Note:IP_ADAPTER_INFO contains a linked list of adapter entries.AdapterInfoSize = 0error = GetAdaptersInfo(ByVal 0&, AdapterInfoSize)If error <> 0 ThenIf error <> ERROR_BUFFER_OVERFLOW ThenMsgBox “GetAdaptersInfo sizing failed with error ” & errorExit SubEnd IfEnd IfReDim AdapterInfoBuffer(AdapterInfoSize – 1)’ Get actual adapter informationerror = GetAdaptersInfo(AdapterInfoBuffer(0), AdapterInfoSize)If error <> 0 ThenMsgBox “GetAdaptersInfo failed with error ” & errorExit SubEnd If’ Allocate memoryCopyMemory AdapterInfo, AdapterInfoBuffer(0), AdapterInfoSizepAdapt = AdapterInfo.NextDoCopyMemory Buffer2, AdapterInfo, AdapterInfoSizeSelect Case Buffer2.TypeCase MIB_IF_TYPE_ETHERNETMsgBox “Adapter name: Ethernet adapter “Case MIB_IF_TYPE_TOKENRINGMsgBox “Adapter name: Token Ring adapter “Case MIB_IF_TYPE_FDDIMsgBox “Adapter name: FDDI adapter “Case MIB_IF_TYPE_PPPMsgBox “Adapter name: PPP adapter”Case MIB_IF_TYPE_LOOPBACKMsgBox “Adapter name: Loopback adapter “Case MIB_IF_TYPE_SLIPMsgBox “Adapter name: Slip adapter “Case ElseMsgBox “Adapter name: Other adapter “End SelectMsgBox “AdapterDescription: ” & Buffer2.DescriptionPhysicalAddress = “”For i = 0 To Buffer2.AddressLength – 1PhysicalAddress = PhysicalAddress & Hex(Buffer2.Address(i))If i < Buffer2.AddressLength – 1 ThenPhysicalAddress = PhysicalAddress & “-”End IfNextMsgBox “Physical Address: ” & PhysicalAddressIf Buffer2.DhcpEnabled ThenMsgBox “DHCP Enabled “ElseMsgBox “DHCP disabled”End IfMsgBox “IP Address: ” & Buffer2.IpAddressList.IpAddressMsgBox “Subnet Mask: ” & Buffer2.IpAddressList.IpMaskpAddrStr = Buffer2.IpAddressList.NextDo While pAddrStr <> 0CopyMemory Buffer, Buffer2.IpAddressList, LenB(Buffer)MsgBox “IP Address: ” & Buffer.IpAddressMsgBox “Subnet Mask: ” & Buffer.IpMaskpAddrStr = Buffer.NextIf pAddrStr <> 0 ThenCopyMemory Buffer2.IpAddressList, ByVal pAddrStr, _LenB(Buffer2.IpAddressList)End IfLoopMsgBox “Default Gateway: ” & Buffer2.GatewayList.IpAddresspAddrStr = Buffer2.GatewayList.NextDo While pAddrStr <> 0CopyMemory Buffer, Buffer2.GatewayList, LenB(Buffer)MsgBox “IP Address: ” & Buffer.IpAddresspAddrStr = Buffer.NextIf pAddrStr <> 0 ThenCopyMemory Buffer2.GatewayList, ByVal pAddrStr, _LenB(Buffer2.GatewayList)End IfLoopMsgBox “DHCP Server: ” & Buffer2.DhcpServer.IpAddressMsgBox “Primary WINS Server: ” & _Buffer2.PrimaryWinsServer.IpAddressMsgBox “Secondary WINS Server: ” & _Buffer2.SecondaryWinsServer.IpAddress’ Display time.NewTime = DateAdd(“s”, Buffer2.LeaseObtained, #1/1/1970#)MsgBox “Lease Obtained: ” & _CStr(Format(NewTime, “dddd, mmm d hh:mm:ss yyyy”))NewTime = DateAdd(“s”, Buffer2.LeaseExpires, #1/1/1970#)MsgBox “Lease Expires :” & _CStr(Format(NewTime, “dddd, mmm d hh:mm:ss yyyy”))pAdapt = Buffer2.NextIf pAdapt <> 0 ThenCopyMemory AdapterInfo, ByVal pAdapt, AdapterInfoSizeEnd IfLoop Until pAdapt = 0End Sub Press the F5 key to run the project, click OK on each of the message boxes that are displayed, and note the results.Running this sample as compiled, EXE gives the following error message at the end:
Runtime error 10: this array is fixed and temporary locked.Running this inside IDE generates IPF at VB6.EXE at the end.

Leave a Reply