public static IP_INTERFACE_INFO FromByteArray(Byte[] buffer)
 {
     unsafe
     {
         IP_INTERFACE_INFO rv = new IP_INTERFACE_INFO();
         int iNumAdapters     = 0;
         Marshal.Copy(buffer, 0, new IntPtr(&iNumAdapters), 4);
         IP_ADAPTER_INDEX_MAP[] adapters = new IP_ADAPTER_INDEX_MAP[iNumAdapters];
         rv.NumAdapters = iNumAdapters;
         rv.Adapter     = new IP_ADAPTER_INDEX_MAP[iNumAdapters];
         int offset = sizeof(int);
         for (int i = 0; i < iNumAdapters; i++)
         {
             IP_ADAPTER_INDEX_MAP map = new IP_ADAPTER_INDEX_MAP();
             IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(map));
             Marshal.StructureToPtr(map, ptr, false);
             Marshal.Copy(buffer, offset, ptr, Marshal.SizeOf(map));
             map = (IP_ADAPTER_INDEX_MAP)Marshal.PtrToStructure(ptr, typeof(IP_ADAPTER_INDEX_MAP));
             Marshal.FreeHGlobal(ptr);
             rv.Adapter[i] = map;
             offset       += Marshal.SizeOf(map);
         }
         return(rv);
     }
 }
 public static bool IpReleaseAddress(IP_ADAPTER_INDEX_MAP adapter)
 {
     if (PInvokes.IpReleaseAddress(ref adapter) == PInvokes.ERROR_SUCCESS)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #3
0
        public void IpReleaseRenewAddressTest()
        {
            var i = GetInterfaceInfo().First();

            Assert.That(IpReleaseAddress(ref i), Is.Zero);
            Assert.That(IpRenewAddress(ref i), Is.Zero);
            var x = new IP_ADAPTER_INDEX_MAP()
            {
                Name = "Bogus"
            };

            Assert.That(IpReleaseAddress(ref x), Is.EqualTo(Win32Error.ERROR_INVALID_PARAMETER));
            Assert.That(IpRenewAddress(ref x), Is.EqualTo(Win32Error.ERROR_INVALID_PARAMETER));
        }
Example #4
0
        /// <summary>
        /// Acquisition d'une adresse IP pour l'interface <paramref name="interfaceIP"/>.
        /// </summary>
        /// <param name="interfaceIP">Interface IP.</param>
        /// <returns></returns>
        public bool RenewIpAddress(int interfaceIP)
        {
            int ret = -1;

            try
            {
                IP_ADAPTER_INDEX_MAP ipAdpt = GetInternalInterfaceInfoFromIndex(interfaceIP);
                ret = NativeMethods.IpRenewAddress(ref ipAdpt);
            }
            catch (Exception)
            {
            }

            return(ret == 0 ? true : false);
        }
Example #5
0
        /// <summary>
        /// Obtient des infos. basiques sur une interface IP.
        /// </summary>
        /// <param name="interfaceIP">Index de l'interface IP.</param>
        /// <returns></returns>
        private IP_ADAPTER_INDEX_MAP GetInternalInterfaceInfoFromIndex(int index)
        {
            IP_ADAPTER_INDEX_MAP ipAdpIdxName = new IP_ADAPTER_INDEX_MAP();
            int iResSize = 0;

            //1ier appel pour déterminer la taille des infos à lire.
            NativeMethods.GetInterfaceInfo(IntPtr.Zero, ref iResSize);

            IntPtr pInfo = Marshal.AllocHGlobal(iResSize);

            //Obtient les infos.
            int ret = NativeMethods.GetInterfaceInfo(pInfo, ref iResSize);

            if (ret == 0)
            {
                //Nombre d'entrées.
                int iEntries = Marshal.ReadInt32(pInfo);

                //Pointeur sur la 1ière entrée.
                IntPtr pInfo2 = new IntPtr(pInfo.ToInt32() + Marshal.SizeOf(typeof(int)));

                //Lecture des infos.
                for (int i = 0; i < iEntries; i++)
                {
                    IP_ADAPTER_INDEX_MAP ipAdapMap = (IP_ADAPTER_INDEX_MAP)
                                                     Marshal.PtrToStructure(pInfo2, typeof(IP_ADAPTER_INDEX_MAP));

                    //Si index demandé = index de l'entrée lut on récupère ces infos et ont sort.
                    if (index == ipAdapMap.Index)
                    {
                        ipAdpIdxName = ipAdapMap;
                        break;
                    }

                    //Pointeur sur entrée suivante.
                    pInfo2 = new IntPtr(pInfo2.ToInt32() + Marshal.SizeOf(typeof(IP_ADAPTER_INDEX_MAP)));
                }
            }

            Marshal.FreeHGlobal(pInfo);

            return(ipAdpIdxName);
        }
Example #6
0
 public static extern int IpReleaseAddress(ref IP_ADAPTER_INDEX_MAP AdapterInfo);
Example #7
0
 static extern int IpRenewAddress(ref IP_ADAPTER_INDEX_MAP AdapterInfo);
Example #8
0
 public static extern uint IpRenewAddress(
     IP_ADAPTER_INDEX_MAP adapterInfo
     );