[DllImport("dhcpsapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern uint DhcpAddSubnetElementV5(string ServerIpAddress, uint SubnetAddress, ref DHCP_SUBNET_ELEMENT_DATA_V5 AddElementInfo);
const uint ERROR_SUCCESS = 0;
[StructLayout(LayoutKind.Sequential)]
struct DHCP_SUBNET_ELEMENT_DATA_V5 {
public DHCP_SUBNET_ELEMENT_TYPE ElementType;
public IntPtr ElementValue;
}
[StructLayout(LayoutKind.Sequential)]
struct DHCP_IP_RESERVATION_V4 {
public uint ReservedIpAddress;
public IntPtr ReservedForClient;
public DHCP_CLIENT_TYPE bAllowedClientTypes;
}
[StructLayout(LayoutKind.Sequential)]
struct DHCP_BINARY_DATA {
public uint DataLength;
public IntPtr Data;
}
enum DHCP_SUBNET_ELEMENT_TYPE : uint {
DhcpIpRanges,
DhcpSecondaryHosts,
DhcpReservedIps,
DhcpExcludedIpRanges,
DhcpIpRangesDhcpOnly,
DhcpIpRangesDhcpBootp,
DhcpIpRangesBootpOnly
}
enum DHCP_CLIENT_TYPE : byte {
CLIENT_TYPE_DHCP,
CLIENT_TYPE_BOOTP,
CLIENT_TYPE_BOTH
}
Do you know one? Please contribute it!
None.
Please add some!
void AddReservedIp(byte[] hardwareAddress, uint address) {
NativeMethods.DHCP_BINARY_DATA uid = new NativeMethods.DHCP_BINARY_DATA();
uid.Data = MemoryManager.AllocByteArray(hardwareAddress);
uid.DataLength = (uint) hardwareAddress.Length;
NativeMethods.DHCP_IP_RESERVATION_V4 reservation = new NativeMethods.DHCP_IP_RESERVATION_V4();
reservation.ReservedIpAddress = address;
reservation.ReservedForClient = MemoryManager.AllocStructure(uid);
reservation.bAllowedClientTypes = NativeMethods.DHCP_CLIENT_TYPE.CLIENT_TYPE_BOTH;
NativeMethods.DHCP_SUBNET_ELEMENT_DATA_V5 element = new NativeMethods.DHCP_SUBNET_ELEMENT_DATA_V5();
element.ElementType = NativeMethods.DHCP_SUBNET_ELEMENT_TYPE.DhcpReservedIps;
element.ElementValue = MemoryManager.AllocStructure(reservation);
uint result = 0;
try {
result = NativeMethods.DhcpAddSubnetElementV5(server.Address, subnet, ref element);
if (result != NativeMethods.ERROR_SUCCESS)
throw new DhcpException(result, "DhcpAddSubnetElementV5");
}
finally {
MemoryManager.Free();
}
}