如何在UWP中获取单播,Dns和网关地址? [英] How to get Unicast, Dns and Gateway Address in UWP?

查看:60
本文介绍了如何在UWP中获取单播,Dns和网关地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows IOT中找到单播,Dns和网关地址. 通常,我可以使用NetworkInterface.GetAllNetworkInterfaces()方法访问这些值.

I'm trying to find Unicast, Dns and Gateway Address in windows IOT. Normally I can access these values with NetworkInterface.GetAllNetworkInterfaces() method.

但是在UWP中,缺少该方法.

But in UWP, that method is missing.

获取这些值是否还有其他选择?

Is there any alternative for getting these values?

推荐答案

您可以尝试使用Iphlpapi.dll中的PInvoke方法.有几种方法可能包含您要查找的 单播,Dns和网关 信息,例如GetInterfaceInfo()GetAdaptersInfo()GetAdaptersAdresses()等.请请在此处查看可用方法的完整列表:

You could try to PInvoke methods from Iphlpapi.dll. There are several methods that may contain the Unicast, Dns and Gateway info you're looking for, like GetInterfaceInfo(), GetAdaptersInfo(), GetAdaptersAdresses(), etc. Please see a complete list of available methods here: IP Helper Functions - MSDN. Eventually more than one method might be necessary.

有关如何执行此操作的示例,以下是 PInvoke的示例代码.net 在我的本地计算机中检索接口名称,该接口名称是作为标准UWP应用实现的:

As an example on how to do it, here's some sample code from PInvoke.Net retrieving interface names in my local computer, implemented as a standard UWP app:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        List<string> list = new List<string>();

        IP_INTERFACE_INFO ips = Iphlpapi.GetInterfaceInfo();

        list.Add(string.Format("Adapter count = {0}", ips.NumAdapters));

        foreach (IP_ADAPTER_INDEX_MAP ip in ips.Adapter)
            list.Add(string.Format("Index = {0}, Name = {1}", ip.Index, ip.Name));

        listView1.ItemsSource = list;
    }
}

// PInvoke
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct IP_ADAPTER_INDEX_MAP
{
    public int Index;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = PInvokes.MAX_ADAPTER_NAME)]
    public String Name;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct IP_INTERFACE_INFO
{
    public int NumAdapters;
    public IP_ADAPTER_INDEX_MAP[] Adapter;

    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));
                map = Marshal.PtrToStructure<IP_ADAPTER_INDEX_MAP>(ptr);
                Marshal.FreeHGlobal(ptr);
                rv.Adapter[i] = map;
                offset += Marshal.SizeOf(map);
            }
            return rv;
        }
    }
}

internal class PInvokes
{
    public const int MAX_ADAPTER_NAME = 128;

    public const int ERROR_INSUFFICIENT_BUFFER = 122;
    public const int ERROR_SUCCESS = 0;

    [DllImport("Iphlpapi.dll", CharSet = CharSet.Unicode)]
    public static extern int GetInterfaceInfo(Byte[] PIfTableBuffer, ref int size);

    [DllImport("Iphlpapi.dll", CharSet = CharSet.Unicode)]
    public static extern int IpReleaseAddress(ref IP_ADAPTER_INDEX_MAP AdapterInfo);
}

public class Iphlpapi
{
    public static IP_INTERFACE_INFO GetInterfaceInfo()
    {
        int size = 0;
        int r = PInvokes.GetInterfaceInfo(null, ref size);
        Byte[] buffer = new Byte[size];
        r = PInvokes.GetInterfaceInfo(buffer, ref size);
        if (r != PInvokes.ERROR_SUCCESS)
            throw new Exception("GetInterfaceInfo returned an error.");
        IP_INTERFACE_INFO info = IP_INTERFACE_INFO.FromByteArray(buffer);
        return info;
    }
}

这篇关于如何在UWP中获取单播,Dns和网关地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆