Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

Has anyone one used the DHcpCreateSubnetV6 version for IPv6 subnet?
I have tried it on C# using interopservices but it didn't worked.

PLease let me know if you have any reference links for the same as I did not find any samples or usage of these API;s on MSDN also.
Following is the code:
C++
public static void CreateSubnetMask()
        {
            UInt32 Response =UInt32.MinValue;
            String IpAddress = "fc00:1234:7899:9abc::2";
            try
            {
                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);
                //Error code is always returned as 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;

            [MarshalAs(UnmanagedType.LPWStr)]
            public String SubnetName;

            [MarshalAs(UnmanagedType.LPWStr)]
            public String SubnetComment;
            public Double State;
            public Double ScopeId;
        };

        [DllImport("dhcpsapi.dll")]
        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);
                byteList.Reverse();
                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);
            }
            else
            {
                addrWords[0] = 0;
                addrWords[1] = System.BitConverter.ToUInt32(addrBytes, 0);
            }
            return addrWords;
        }
</byte></byte>


Any kind of help will be highly appreciated.

-Thanks
Posted
Updated 11-Aug-11 1:38am
v4
Comments
Emilio Garavaglia 11-Aug-11 2:14am    
Saying "but it didn't worked" without saying WHAT and HOW didn't worked, how you did it and what you've instead got, will simple keep people away from this question, with very few chances to get an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900