This project has moved. For the latest updates, please go here.

HOw can we use DhcpCreateSubnetV6() function in C#

Aug 11, 2011 at 11:38 AM

Hi All,

I am trying to create subnet for DHCPv6 using DhcpCreateSubnetV6() API. Below is the same code for the same. But it always giving me Error code:87. Can you please let me know how I can improve and run this program to create IPv6 subnet?

public static void CreateSubnetMask()
            UInt32 Response =UInt32.MinValue;
            String IpAddress = "fc00:1234:7899:9abc::2";
                String SAubnetMask = "fc00:1234:7899:9abc::1";
                DHCP_IPV6_ADDRESS stAddress = new DHCP_IPV6_ADDRESS();
                ulong[] iparray = ConvertIPAddressToUInt64Array("fc00:1234:7899:9abc::2");
                stAddress.LowOrderBits = iparray[1];
                stAddress.HighOrderBits = iparray[0];
                //stAddress.HighOrderBits = SAubnetMask.ToUint64();
                //stAddress.LowOrderBits = 0;
                LDHCP_SUBNET_INFO_V6 stSubnetInfo = new LDHCP_SUBNET_INFO_V6();
                stSubnetInfo.SubnetComment = "IPv6 Test";
                stSubnetInfo.SubnetAddress = stAddress;
                stSubnetInfo.SubnetName = "AnotherSubnet";
                String Prefix = "fc00:1234:7899:9abc::";
                stSubnetInfo.Prefix = 64;
                stSubnetInfo.Preference = 0;
                //stSubnetInfo.ScopeId = Prefix.ToUint64();
                //stSubnetInfo.State = 0; //Enabled             

                Response = DhcpCreateSubnetV6(IpAddress, stAddress, ref stSubnetInfo);  //Returns Error code: 87
                Console.WriteLine("returned Code is  : " + Response.ToString());
                if (Response.ToString().Equals("1722"))
                    Console.WriteLine("IPAddress is already used :");
                else if (Response.ToString().Equals("5"))
                    Console.WriteLine("Access Denied");

            catch (Exception ex)
                Console.WriteLine("Exception Occurred: " + ex.Message);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct DHCP_IPV6_ADDRESS
            public UInt64 HighOrderBits; //value containing the higher 64 bits of the IPv6 address
            public UInt64 LowOrderBits; //value containing the lower 64 bits of the IPv6 address

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct LDHCP_SUBNET_INFO_V6
            public DHCP_IPV6_ADDRESS SubnetAddress;
            public UInt64 Prefix;
            public UInt16 Preference;

            public String SubnetName;

            public String SubnetComment;
            public Double State;
            public Double ScopeId;

        static extern UInt32 DhcpCreateSubnetV6([MarshalAs(UnmanagedType.LPWStr)]
                                                 String ServerIpAddress, DHCP_IPV6_ADDRESS SubnetAddress,
                                                 ref LDHCP_SUBNET_INFO_V6 SubnetInfo);

        private static ulong[] ConvertIPAddressToUInt64Array(string ipAddress)
            byte[] addrBytes = IPAddress.Parse(ipAddress).GetAddressBytes();
            if (System.BitConverter.IsLittleEndian)
                //little-endian machines store multi-byte integers with the
                //least significant byte first. this is a problem, as integer
                //values are sent over the network in big-endian mode. reversing
                //the order of the bytes is a quick way to get the BitConverter
                //methods to convert the byte arrays in big-endian mode.
                System.Collections.Generic.List<byte> byteList = new System.Collections.Generic.List<byte>(addrBytes);
                addrBytes = byteList.ToArray();
            ulong[] addrWords = new ulong[2];
            if (addrBytes.Length > 8)
                addrWords[0] = System.BitConverter.ToUInt64(addrBytes, 8);
                addrWords[1] = System.BitConverter.ToUInt64(addrBytes, 0);
                addrWords[0] = 0;
                addrWords[1] = System.BitConverter.ToUInt32(addrBytes, 0);
            return addrWords;

Let me know whats going wrong in above program.