Finding the Unicast DNS and Gateway Address in UWP

0 votes

Hello everyone! I'm using Windows 10 IoT Core and I've been looking for a way to get the Unicast, DNS and Gateway Address. I thought the NetworkInterface.GetAllNetworkInterfaces() method could give me those values, but it seems to be missing in UWP. Is there another way for me to find them? It should be possible, I just don't know how. Please help!

Aug 20, 2018 in IoT (Internet of Things) by Bharani
• 4,660 points
924 views

1 answer to this question.

0 votes

Try the PInvoke api methods from Iphlpapi.dll. It has multiple methods that could contain the information you're looking for, like say GetInterfaceInfo(), GetAdaptersInfo(), GetAdaptersAddresses(), etc. You could get the Unicast, DNS and Gateway Address from here using more than one of these methods.
You could check out this sample code from PInvoke.Net as a reference for getting the values you need: 

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;
    }
}

answered Aug 20, 2018 by nirvana
• 3,130 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

How to get Unicast, Dns and Gateway Address in UWP?

Try this code Snippet I found here: https://social.msdn.microsoft.com/Forums/en-US/27a8b7a8-8071-4bc1-bbd4-e7c1fc2bd8d7/windows-10-iot-core-how-do-you-create-a-tcp-server-and-client?forum=WindowsIoT ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,231 views
0 votes
1 answer

Accessing the IP address list in contiki

You can access the list of clients ...READ MORE

answered Mar 4, 2019 in IoT (Internet of Things) by Shubham
• 13,490 points
1,414 views
0 votes
1 answer

Accessing the IP address list in contiki

Try something like this. #include "uip-ds6-nbr.h" #include "nbr-table.h" uip_ds6_nbr_t *nbr ...READ MORE

answered Jul 26, 2019 in IoT (Internet of Things) by Upasana
• 8,620 points
1,157 views
0 votes
1 answer
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,124 views
0 votes
1 answer

Firewall issue with Webserver in UWP on Raspberry Pi 3

Here's what could be happenning: You port hasn't ...READ MORE

answered Oct 12, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
1,133 views
0 votes
1 answer

Using Sleep()/Delay() in C# on Windows IoT

You'll be glad to know that C# ...READ MORE

answered Aug 28, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
2,297 views
0 votes
1 answer

Tap a Button to Push Notification

Generally, sending push notifications fall under the scope of ...READ MORE

answered Jan 8, 2019 in IoT (Internet of Things) by nirvana
• 3,130 points
662 views
0 votes
1 answer

Sending a POST in UWP using C#

I couldn't find any proper documentation and ...READ MORE

answered Jan 11, 2019 in IoT (Internet of Things) by nirvana
• 3,130 points
1,423 views
0 votes
1 answer

Setting-up a RFID RC522 chip in Raspberry Pi?

First, let me congratulate you on buying ...READ MORE

answered Jul 10, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,697 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP