@msdn=http://search.microsoft.com/search/results.aspx?qu=$$$ @pinvoke=http://pinvoke.net/$$$.htm Summary: 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. !!!!C# Signature: [DllImport("dhcpsapi.dll", SetLastError = true, CharSet = CharSet.Unicode)] public static extern uint DhcpEnumSubnets( string ServerIpAddress, ref uint ResumeHandle, uint PreferredMaximum, out IntPtr EnumInfo, ref uint ElementsRead, ref uint ElementsTotal ); !!!!VB Signature: 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 !!!!User-Defined Types: [DHCP_IP_ARRAY] !!!!Alternative Managed API: Do you know one? Please contribute it! !!!!Notes: '''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. Completed library DHCP functions. http://www.rupj.net/portfolio/dhcp-web-services.html !!!!Sample Code: static void Main() { String ServerIpAddress = "192.168.0.250"; UInt32 DHCPResult = 0; IntPtr ips; uint nr = 0; uint total = 0; uint resumeHandle = 0; DHCPResult = DhcpEnumSubnets(ServerIpAddress, ref resumeHandle, 1000, out ips, ref nr, ref total); if (DHCPResult == 0) { DHCP_IP_ARRAY ipArray = (DHCP_IP_ARRAY)Marshal.PtrToStructure(ips, typeof(DHCP_IP_ARRAY)); 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])); Console.WriteLine("{0}", UInt32IPAddressToString(ipAddressArray[i].IPAddress)); } Marshal.FreeCoTaskMem(outArray); Console.WriteLine("Elements read {0} of total {1}", nr, total); } else { int code = 0; unchecked { code = (int)DHCPResult; } Win32Exception winex = new Win32Exception(code); Console.WriteLine(winex.NativeErrorCode + " : " + winex.Message); } } [DllImport("dhcpsapi.dll", SetLastError = true, CharSet = CharSet.Unicode)] public static extern uint DhcpEnumSubnets( string ServerIpAddress, ref uint ResumeHandle, uint PreferredMaximum, out IntPtr EnumInfo, ref uint ElementsRead, ref uint ElementsTotal ); [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('.'); return sIp[3] + "." + sIp[2] + "." + sIp[1] + "." + sIp[0]; } Documentation: DhcpEnumSubnets@msdn on MSDN
Edit dhcpsapi.DhcpEnum...
You do not have permission to change this page. If you feel this is in error, please send feedback with the contact link on the main page.