当前位置:  开发笔记 > 编程语言 > 正文

C# - 在Universal Apps中获取mac地址

如何解决《C#-在UniversalApps中获取mac地址》经验,为你挑选了2个好方法。

我正在开发一个Windows 10通用应用程序,我需要获取通用应用程序将运行的设备的网络适配器的mac地址.我看了一下MSDN,但我可以找到一种获取mac地址的方法,任何人都可以帮我解决代码,在Windows 10的通用应用程序中获取网络适配器的mac地址吗?

提前致谢.



1> Tomasz Pikć..:

嗯,我写了一些东西,但我没有在移动设备上测试它,因为我目前没有,但我在我的PC和Windows Mobile 10模拟器上进行了测试.

public static class AdaptersHelper
{
    const int MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
    const int ERROR_BUFFER_OVERFLOW = 111;
    const int MAX_ADAPTER_NAME_LENGTH = 256;
    const int MAX_ADAPTER_ADDRESS_LENGTH = 8;
    const int MIB_IF_TYPE_OTHER = 1;
    const int MIB_IF_TYPE_ETHERNET = 6;
    const int MIB_IF_TYPE_TOKENRING = 9;
    const int MIB_IF_TYPE_FDDI = 15;
    const int MIB_IF_TYPE_PPP = 23;
    const int MIB_IF_TYPE_LOOPBACK = 24;
    const int MIB_IF_TYPE_SLIP = 28;

    [DllImport("iphlpapi.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
    public static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref Int64 pBufOutLen);

    public static List GetAdapters()
    {
        var adapters = new List();

        long structSize = Marshal.SizeOf(typeof(IP_ADAPTER_INFO));
        IntPtr pArray = Marshal.AllocHGlobal(new IntPtr(structSize));

        int ret = GetAdaptersInfo(pArray, ref structSize);

        if (ret == ERROR_BUFFER_OVERFLOW) // ERROR_BUFFER_OVERFLOW == 111
        {
            // Buffer was too small, reallocate the correct size for the buffer.
            pArray = Marshal.ReAllocHGlobal(pArray, new IntPtr(structSize));

            ret = GetAdaptersInfo(pArray, ref structSize);
        }

        if (ret == 0)
        {
            // Call Succeeded
            IntPtr pEntry = pArray;

            do
            {
                var adapter = new AdapterInfo();

                // Retrieve the adapter info from the memory address
                var entry = (IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry, typeof(IP_ADAPTER_INFO));

                // Adapter Type
                switch (entry.Type)
                {
                    case MIB_IF_TYPE_ETHERNET:
                        adapter.Type = "Ethernet";
                        break;
                    case MIB_IF_TYPE_TOKENRING:
                        adapter.Type = "Token Ring";
                        break;
                    case MIB_IF_TYPE_FDDI:
                        adapter.Type = "FDDI";
                        break;
                    case MIB_IF_TYPE_PPP:
                        adapter.Type = "PPP";
                        break;
                    case MIB_IF_TYPE_LOOPBACK:
                        adapter.Type = "Loopback";
                        break;
                    case MIB_IF_TYPE_SLIP:
                        adapter.Type = "Slip";
                        break;
                    default:
                        adapter.Type = "Other/Unknown";
                        break;
                } // switch

                adapter.Name = entry.AdapterName;
                adapter.Description = entry.AdapterDescription;

                // MAC Address (data is in a byte[])
                adapter.MAC = string.Join("-", Enumerable.Range(0, (int)entry.AddressLength).Select(s => string.Format("{0:X2}", entry.Address[s])));

                // Get next adapter (if any)

                adapters.Add(adapter);

                pEntry = entry.Next;
            }
            while (pEntry != IntPtr.Zero);

            Marshal.FreeHGlobal(pArray);
        }
        else
        {
            Marshal.FreeHGlobal(pArray);
            throw new InvalidOperationException("GetAdaptersInfo failed: " + ret);
        }

        return adapters;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct IP_ADAPTER_INFO
    {
        public IntPtr Next;
        public Int32 ComboIndex;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_ADAPTER_NAME_LENGTH + 4)]
        public string AdapterName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_ADAPTER_DESCRIPTION_LENGTH + 4)]
        public string AdapterDescription;
        public UInt32 AddressLength;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_ADAPTER_ADDRESS_LENGTH)]
        public byte[] Address;
        public Int32 Index;
        public UInt32 Type;
        public UInt32 DhcpEnabled;
        public IntPtr CurrentIpAddress;
        public IP_ADDR_STRING IpAddressList;
        public IP_ADDR_STRING GatewayList;
        public IP_ADDR_STRING DhcpServer;
        public bool HaveWins;
        public IP_ADDR_STRING PrimaryWinsServer;
        public IP_ADDR_STRING SecondaryWinsServer;
        public Int32 LeaseObtained;
        public Int32 LeaseExpires;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct IP_ADDR_STRING
    {
        public IntPtr Next;
        public IP_ADDRESS_STRING IpAddress;
        public IP_ADDRESS_STRING IpMask;
        public Int32 Context;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct IP_ADDRESS_STRING
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
        public string Address;
    }
}

和我的AdapterInfo类:

public class AdapterInfo
{
    public string Type { get; set; }

    public string Description { get; set; }

    public string Name { get; set; }

    public string MAC { get; set; }
}

来源:http://www.pinvoke.net/default.aspx/iphlpapi/GetAdaptersInfo.html



2> Scott Chambe..:

引自MSDN Fourms帖子" Windows Store App如何访问网络适配器设备的MAC地址 "

通常,您无法通过设计从Windows应用商店应用获取系统特定信息.

有一个特殊情况支持:使用应用程序特定硬件ID(ASHWID)实现每个设备应用程序逻辑的指导

Chuck Walbourn - MSFT微软公司(MSFT)

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有