DhcpRemoveSubnetElementV5 (dhcpsapi)
Last changed: JonGrant-205.223.239.6

.
Summary
TODO - a short description

C# Signature:

[DllImport("dhcpsapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern uint DhcpRemoveSubnetElementV5(
    string ServerIpAddress,
    uint SubnetAddress,
    ref DHCP_SUBNET_ELEMENT_DATA_V5 RemoveElementInfo,
    DHCP_FORCE_FLAG ForceFlag);

User-Defined Types:

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
}

enum DHCP_FORCE_FLAG : uint {
    DhcpFullForce,
    DhcpNoForce
}

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Completed library DHCP functions.

http://www.rupj.net/portfolio/dhcp-web-services.html

Sample Code:

static List<IntPtr> _allocations = new List<IntPtr>();

static IntPtr AllocStructure(object structure) {
    IntPtr result = Marshal.AllocHGlobal(Marshal.SizeOf(structure));
    Marshal.StructureToPtr(structure, result, false);
    _allocations.Add(result);
    return result;
}

static IntPtr AllocByteArray(byte[] array) {
    IntPtr result = Marshal.AllocHGlobal(array.Length);
    Marshal.Copy(array, 0, result, array.Length);
    _allocations.Add(result);
    return result;
}

static void Free() {
    foreach (IntPtr allocation in _allocations)
        Marshal.FreeHGlobal(allocation);

    _allocations.Clear();
}

void RemoveReservedIp(byte[] hardwareAddress, uint address) {
    NativeMethods.DHCP_BINARY_DATA uid = new NativeMethods.DHCP_BINARY_DATA();
    uid.Data = AllocByteArray(hardwareAddress);
    uid.DataLength = (uint) hardwareAddress.Length;

    NativeMethods.DHCP_IP_RESERVATION_V4 reservation = new NativeMethods.DHCP_IP_RESERVATION_V4();
    reservation.ReservedIpAddress = address;
    reservation.ReservedForClient = 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 = AllocStructure(reservation);

    try {
        uint result = NativeMethods.DhcpRemoveSubnetElementV5(server.Address, subnet, ref element, NativeMethods.DHCP_FORCE_FLAG.DhcpFullForce);
        if (result != NativeMethods.ERROR_SUCCESS)
            throw new DhcpException(result, "DhcpRemoveSubnetElementV5");
        }
        finally {
            Free();
        }
    }
}

Documentation