Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than dhcpsapi, prefix the name with the module name and a period.
Declare Unicode Function DhcpEnumSubnets Lib "Dhcpsapi" (ByVal ServerIpAddress As String, ByRef ResumeHandle As Integer, ByVal PreferredMaximum As Integer, ByRef EnumInfo As IntPtr, ByRef ElementsRead As Integer, ByRef ElementsTotal As Integer) As Integer
ServerIpAddress - [in] Unicode string that specifies the IP address of the DHCP server
ResumeHandle - [in,out] Pointer to a DHCP_RESUME_HANDLE value that identifies the enumeration operation. Initially, this value should be zero, with a successful call returning the handle value used for subsequent enumeration requests. For example, if PreferredMaximum is set to 100, and 200 subnet addresses are stored on the server, the resume handle can be used after the first 100 subnets are retrieved to obtain the next 100 on a subsequent call, and so forth.
PreferredMaximum - [in] Specifies the preferred maximum number of subnet addresses to return. If the number of remaining unenumerated options is less than this value, then that amount will be returned
EnumInfo - [out] Pointer to a DHCP_IP_ARRAY [IntPtr] structure that contains the subnet IDs available on the DHCP server. If no subnets are defined, this value will be null.
ElementsRead - [out] Pointer to a DWORD value that specifies the number of subnet addresses returned in EnumInfo.
ElementsTotal - [out] Pointer to a DWORD value that specifies the number of subnets defined on the DHCP server that have not yet been enumerated
Tips & Tricks:
Make sure that ResumeHandle is 0 (zero) the first time you call this function.
int size = (int)ipArray.NumElements;
IntPtr outArray = ipArray.IPAddresses;
DHCP_IP_ADDRESS[] ipAddressArray = new DHCP_IP_ADDRESS[size];
IntPtr current = outArray;
for (int i = 0; i < size; i++)
{
ipAddressArray[i] = new DHCP_IP_ADDRESS();
Marshal.PtrToStructure(current, ipAddressArray[i]);
Marshal.DestroyStructure(current, typeof(DHCP_IP_ADDRESS));
current = (IntPtr)((int)current + Marshal.SizeOf(ipAddressArray[i]));
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_IP_ARRAY
{
public uint NumElements;
public IntPtr IPAddresses;
}
/// This is a custom type/class
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DHCP_IP_ADDRESS
{
public UInt32 IPAddress;
}
public static string UInt32IPAddressToString(UInt32 ipAddress)
{
IPAddress ipA = new IPAddress(ipAddress);
string[] sIp = ipA.ToString().Split('.');
Declare Unicode Function DhcpEnumSubnets Lib "Dhcpsapi" (ByVal ServerIpAddress As String, ByRef ResumeHandle As UInteger, ByVal PreferredMaximum As UInteger, ByRef EnumInfo As IntPtr, ByRef ElementsRead As UInteger, ByRef ElementsTotal As UInteger) As UInteger
Sub main()
Dim ServerIpAddress As String = "192.168.0.250"
Dim DHCPResult As UInt32 = 0
Dim ips As IntPtr
Dim nr As UInteger = 0
Dim total As UInteger = 0
Dim resumeHandle As UInteger = 0
DHCPResult = DhcpEnumSubnets(ServerIpAddress, resumeHandle, 1000, ips, nr, total)
If DHCPResult = 0 Then
Dim ipArray As DHCP_IP_ARRAY = CType(Marshal.PtrToStructure(ips, GetType(DHCP_IP_ARRAY)), DHCP_IP_ARRAY)
Dim size As Integer = CType(ipArray.NumElements, Integer)
Dim outArray As IntPtr = ipArray.IPAddresses
Dim ipAddressArray() As DHCP_IP_ADDRESS = New DHCP_IP_ADDRESS((size) - 1) {}
Dim current As IntPtr = outArray
Dim i As Integer = 0
Do While (i < size)
ipAddressArray(i) = New DHCP_IP_ADDRESS
Marshal.PtrToStructure(current, ipAddressArray(i))
Marshal.DestroyStructure(current, GetType(DHCP_IP_ADDRESS))
current = CType((CType(current, Integer) + Marshal.SizeOf(ipAddressArray(i))), IntPtr)
Console.WriteLine("{0}", UInt32IPAddressToString(ipAddressArray(i).IPAddress))
i = (i + 1)
Loop
Marshal.FreeCoTaskMem(outArray)
Console.WriteLine("Elements read {0} of total {1}", nr, total)
Else
Dim code As Integer = 0
code = CType(DHCPResult, Integer)
Dim winex As Win32Exception = New Win32Exception(code)
Console.WriteLine((winex.NativeErrorCode + (" : " + winex.Message)))
End If
End Sub
<StructLayout(LayoutKind.Sequential)>
Public Structure DHCP_IP_ARRAY
Public NumElements As UInteger
Public IPAddresses As IntPtr
End Structure
' This is a custom type/class
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Class DHCP_IP_ADDRESS
Public IPAddress As UInt32
End Class
Public Shared Function UInt32IPAddressToString(ByVal ipAddress As UInt32) As String
Dim ipA As Net.IPAddress = New Net.IPAddress(ipAddress)
Dim sIp() As String = ipA.ToString.Split(Microsoft.VisualBasic.ChrW(46))
Return (sIp(3) + ("." + (sIp(2) + ("." + (sIp(1) + ("." + sIp(0)))))))
End Function
The DhcpEnumSubnets function returns an enumerated list of subnets defined on the DHCP server. This function returns ERROR_SUCCESS upon a successful call. If a call is made with the same ""ResumeHandle"" value and all items on the server have been enumerated, this method returns ERROR_NO_MORE_ITEMS with ""ElementsRead"" and ""ElementsTotal"" set to 0. Otherwise, it returns one of the DHCP Server Management API Error Codes.
10/25/2018 12:53:48 AM - Jet0JLH-195.226.121.69
TODO - a short description
3/16/2007 8:16:03 AM - -163.5.255.60
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).