Retrieving Windows DHCP servers list and scope information of each DHCP server using .NET
We are doing a small dashboard to retrieve and display the DHCP servers list in a LAN and then getting the Scope information like IPs used and unused and the count using .NET preferably C#. How could th开发者_如何学Goat be made possible.
I tried to do something similar once (with Windows 2003) and found that there werent any .net objects/classes for a DHCP server. I did find this http://bytes.com/topic/net/answers/574937-managing-dhcp-servers-using-c-another-net-lang but that was beyond my coding level..
Good luck
An excellent library of DHCP functions - DYNAMIC HOST CONFIGURATION PROTOCOL WEB SERVICES by Jason Rupard
He doesn't have the enumeration of DHCP servers in there, so I've shown how to do so below. Everything else you need should already be there.
[DllImport("dhcpsapi.dll", CharSet = CharSet.Unicode)]
internal static extern UInt32 DhcpEnumServers(
UInt32 Flags, // Should be 0
IntPtr IdInfo, // Should be null
out IntPtr Servers,
IntPtr CallbackFn, // Should be null
IntPtr CallbackData // Should be null
);
[DllImport("dhcpsapi.dll")]
internal static extern void DhcpRpcFreeMemory(IntPtr BuffPtr);
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct DHCP_SERVER_INFO_ARRAY
{
public UInt32 Flags;
public UInt32 NumElements;
public IntPtr Servers;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct DHCP_SERVER_INFO
{
public UInt32 Version;
[MarshalAsAttribute(UnmanagedType.LPWStr)]
public string ServerName;
public UInt32 ServerAddress;
public UInt32 Flags;
public UInt32 State;
[MarshalAsAttribute(UnmanagedType.LPWStr)]
public string DsLocation;
public UInt32 DsLocType;
}
public class DhcpServer
{
public string Name;
public DhcpIpAddress IpAddress;
public string AdLocation;
public static List<DhcpServer> EnumAll()
{
var servers = new List<DhcpServer>();
IntPtr retPtr;
var response = NativeMethods.DhcpEnumServers(0, IntPtr.Zero, out retPtr, IntPtr.Zero, IntPtr.Zero);
if (response != 0)
{
if (retPtr != IntPtr.Zero)
{
NativeMethods.DhcpRpcFreeMemory(retPtr);
}
throw new DhcpException(response);
}
var nativeArray = (DHCP_SERVER_INFO_ARRAY) Marshal.PtrToStructure(retPtr, typeof (DHCP_SERVER_INFO_ARRAY));
var current = nativeArray.Servers;
for (var i = 0; i < nativeArray.NumElements; ++i)
{
var nativeClient = (DHCP_SERVER_INFO) Marshal.PtrToStructure(current, typeof (DHCP_SERVER_INFO));
servers.Add(ConvertFromNative(nativeClient));
current = (IntPtr) ((int) current + Marshal.SizeOf(typeof(DHCP_SERVER_INFO)));
}
NativeMethods.DhcpRpcFreeMemory(retPtr);
return servers;
}
private static DhcpServer ConvertFromNative(DHCP_SERVER_INFO nativeServer)
{
var server = new DhcpServer
{
IpAddress = new DhcpIpAddress(nativeServer.ServerAddress),
Name = nativeServer.ServerName,
AdLocation = nativeServer.DsLocation
};
return server;
}
}
精彩评论