用于获取本地IP和子网掩码的Windows UWP c ++代码

时间:2017-11-10 05:31:34

标签: uwp uwp-xaml

如何使用Windows UWP API在Windows 10中获取本地计算机的子网掩码? " NetworkInformation" class只提供IP而不是掩码

2 个答案:

答案 0 :(得分:2)

从前缀长度获取掩码的简单方法。

public static System.Net.IPAddress CreateByPrefixLength(int prefixLength)
{
    UInt32 mask = 0xFFFFFFFF << (32 - prefixLength);
    byte[] binary = new byte[4];
    binary[0] = (byte)((mask & 0xFF000000) >> 24);
    binary[1] = (byte)((mask & 0x00FF0000) >> 16);
    binary[2] = (byte)((mask & 0x0000FF00) >>  8);
    binary[3] = (byte)((mask & 0x000000FF) >>  0);
    System.Net.IPAddress address = new System.Net.IPAddress(binary);
    return address;
}

C ++变体:

std::string get_mask_by_prefix(uint8_t prefix)
{
  uint32_t mask = 0xFFFFFFFF << (32 - lenght);
  std::string result = format("%u.%u.%u.%u"
                            , ((mask & 0xFF000000) >> 24)
                            , ((mask & 0x00FF0000) >> 16)
                            , ((mask & 0x0000FF00) >> 8)
                            ,  (mask & 0x000000FF));
  return result;
}

答案 1 :(得分:0)

  

&#34; NetworkInformation&#34; class只提供IP而不是掩码

目前,没有这样的api直接获得子网掩码。你可以改为获得PrefixLength。并按以下方式将PrefixLength转换为Subnet掩码。

var hosts = NetworkInformation.GetHostNames();

foreach (var host in hosts)
{
    if (host.Type == HostNameType.Ipv4 && host.IPInformation != null)
    {
        var submask = SubnetMask.CreateByNetBitLength((int)host.IPInformation.PrefixLength);
    }
}

子网助手

public static class SubnetMask
{
    public static readonly IPAddress ClassA = IPAddress.Parse("255.0.0.0");
    public static readonly IPAddress ClassB = IPAddress.Parse("255.255.0.0");
    public static readonly IPAddress ClassC = IPAddress.Parse("255.255.255.0");

    public static IPAddress CreateByHostBitLength(int hostpartLength)
    {
        int hostPartLength = hostpartLength;
        int netPartLength = 32 - hostPartLength;

        if (netPartLength < 2)
            throw new ArgumentException("Number of hosts is to large for IPv4");

        Byte[] binaryMask = new byte[4];

        for (int i = 0; i < 4; i++)
        {
            if (i * 8 + 8 <= netPartLength)
                binaryMask[i] = (byte)255;
            else if (i * 8 > netPartLength)
                binaryMask[i] = (byte)0;
            else
            {
                int oneLength = netPartLength - i * 8;
                string binaryDigit =
                    String.Empty.PadLeft(oneLength, '1').PadRight(8, '0');
                binaryMask[i] = Convert.ToByte(binaryDigit, 2);
            }
        }
        return new IPAddress(binaryMask);
    }

    public static IPAddress CreateByNetBitLength(int netpartLength)
    {
        int hostPartLength = 32 - netpartLength;
        return CreateByHostBitLength(hostPartLength);
    }

    public static IPAddress CreateByHostNumber(int numberOfHosts)
    {
        int maxNumber = numberOfHosts + 1;

        string b = Convert.ToString(maxNumber, 2);

        return CreateByHostBitLength(b.Length);
    }
}