Visual Basic Network Services
GetAdaptersInfo: Determine if DHCP is Enabled
Posted:   Wednesday April 25, 2001
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   Windows 98, Windows 2000, Windows XP
Author:   VBnet - Randy Birch


GetAdaptersInfo: Determine if DHCP is Enabled
GetAdaptersInfo: Get the DHCP Server IP
GetAdaptersInfo: Get the IPs for all DHCP Servers
GetAdaptersInfo: Get the Network Adapter IP Address
GetAdaptersInfo: Get IP Addresses for All Installed Network Adapters

GetNetworkParams: Determine Current and Available DNS Servers
One of the operating systems listed under OS Restrictions above.

GetAdaptersInfo returns adapter information for the local computer.  The call returns a buffer that has received a linked list of IP_ADAPTER_INFO structures. The IP_ADAPTER_INFO structure contains information about a particular network adapter on the local computer. In a multiple NIC machine, the call returns one IP_ADAPTER_INFO structure for each installed adapter. Each adapter is returned in series, and the structure's dwNext parameter points to the address of the start of next adapter entry.

This demo wraps a call to GetAdaptersInfo in the function DhcpIsEnabled, which checks each IP_ADAPTER_INFO's uDhcpEnabled value returning True if an adapter is using DHCP.

 BAS Module Code

 Form Code
To a form add a command button (Command1), and a Text box (Text1).  Add the following to the form:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Const MAX_ADAPTER_NAME_LENGTH        As Long = 256
Private Const MAX_ADAPTER_ADDRESS_LENGTH     As Long = 8
Private Const ERROR_SUCCESS  As Long = 0

    IpAddr(0 To 15)  As Byte
End Type

    IpMask(0 To 15)  As Byte
End Type

    dwNext     As Long
    IpMask     As IP_MASK_STRING
    dwContext  As Long
End Type

  dwNext                As Long
  ComboIndex            As Long  'reserved
  sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3))        As Byte
  sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
  dwAddressLength       As Long
  sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1))       As Byte
  dwIndex               As Long
  uType                 As Long
  uDhcpEnabled          As Long
  CurrentIpAddress      As Long
  IpAddressList         As IP_ADDR_STRING
  GatewayList           As IP_ADDR_STRING
  DhcpServer            As IP_ADDR_STRING
  bHaveWins             As Long
  PrimaryWinsServer     As IP_ADDR_STRING
  SecondaryWinsServer   As IP_ADDR_STRING
  LeaseObtained         As Long
  LeaseExpires          As Long
End Type

Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
  (pTcpTable As Any, _
   pdwSize As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (dst As Any, _
   src As Any, _
   ByVal bcount As Long)

Private Sub Command1_Click()

   Text1.Text = DhcpIsEnabled()

End Sub

Public Function DhcpIsEnabled() As Boolean
  'api vars
   Dim cbRequired   As Long
   Dim buff()       As Byte
   Dim Adapter      As IP_ADAPTER_INFO
   Dim AdapterStr   As IP_ADDR_STRING
  'working vars
   Dim ptr1         As Long
   Dim flag         As Long
   Call GetAdaptersInfo(ByVal 0&, cbRequired)

   If cbRequired > 0 Then
      ReDim buff(0 To cbRequired - 1) As Byte
      If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
        'get a pointer to the data stored in buff()
         ptr1 = VarPtr(buff(0))
         Do While ptr1
           'copy the data from the pointer to the
           'first adapter into the IP_ADAPTER_INFO type
            CopyMemory Adapter, ByVal ptr1, LenB(Adapter)
           'retrieve the adapter data
            With Adapter
              'set a flag indicating whether
              'DHCP is enabled for the adapter
               flag = .uDhcpEnabled = 1
              'if the flag was set, we have the info
              'so we can exit the function
               If flag = True Then Exit Do
              'flag must be false, so check for
              'additional adapters
               ptr1 = .dwNext
            End With

      End If
   End If
   DhcpIsEnabled = flag
End Function


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter