Пример #1
0
        internal static         String[] NativeGet(Sigar sigar)
        {
            Type   type = typeof(NetInterfaceList);
            IntPtr ptr  = Marshal.AllocHGlobal(Marshal.SizeOf(type));

            int status =
                sigar_net_interface_list_get(sigar.sigar.Handle, ptr);

            if (status != Sigar.OK)
            {
                Marshal.FreeHGlobal(ptr);
                throw Sigar.FindException(sigar, status);
            }

            NetInterfaceList ifPtr =
                (NetInterfaceList)Marshal.PtrToStructure(ptr, type);

            String[] iflist = new String[ifPtr.number];

            IntPtr iptr = ifPtr.data;

            for (int i = 0; i < ifPtr.number; i++)
            {
                IntPtr str = Marshal.ReadIntPtr(iptr, i * IntPtr.Size);
                iflist[i] = Marshal.PtrToStringAnsi(str);
            }

            sigar_net_interface_list_destroy(sigar.sigar.Handle, ptr);

            Marshal.FreeHGlobal(ptr);

            return(iflist);
        }
Пример #2
0
        internal static string[] NativeGet(Sigar sigar)
        {
            System.Type   typeFromHandle = typeof(NetInterfaceList);
            System.IntPtr intPtr         = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(typeFromHandle));
            int           num            = NetInterfaceList.sigar_net_interface_list_get(sigar.sigar.Handle, intPtr);

            if (num != 0)
            {
                System.Runtime.InteropServices.Marshal.FreeHGlobal(intPtr);
                throw Sigar.FindException(sigar, num);
            }
            NetInterfaceList netInterfaceList = (NetInterfaceList)System.Runtime.InteropServices.Marshal.PtrToStructure(intPtr, typeFromHandle);

            string[]      array = new string[netInterfaceList.number];
            System.IntPtr ptr   = netInterfaceList.data;
            int           num2  = 0;

            while ((long)num2 < (long)((ulong)netInterfaceList.number))
            {
                System.IntPtr ptr2 = System.Runtime.InteropServices.Marshal.ReadIntPtr(ptr, num2 * System.IntPtr.Size);
                array[num2] = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(ptr2);
                num2++;
            }
            NetInterfaceList.sigar_net_interface_list_destroy(sigar.sigar.Handle, intPtr);
            System.Runtime.InteropServices.Marshal.FreeHGlobal(intPtr);
            return(array);
        }