C#

[C#/PDA]MAC Address 구하기 예제

선영아 사랑해 2016. 2. 17. 10:28


WINCE.NET/WINDOWS MOBILE PDA 제품의 MAC Address 구하는 함수입니다.


[System.Runtime.InteropServices.DllImport("iphlpapi.dll", SetLastError = true)]
        private static extern int GetAdaptersInfo(byte[] info, ref uint size);

        /// <summary>
        /// Gets the Mac Address
        /// </summary>
        /// <returns>the mac address or ""</returns>
        public string GetMacAddress()
        {
            uint num = 0u;

            GetAdaptersInfo(null, ref num);

            byte[] array = new byte[(int)((UIntPtr)num)];

            int adaptersInfo = GetAdaptersInfo(array, ref num);

            if (adaptersInfo == 0)
            {
                string macAddress = "";

                int macLength = BitConverter.ToInt32(array, 400);

                macAddress = BitConverter.ToString(array, 404, macLength);

                macAddress = macAddress.Replace("-", ":");

                return macAddress;
            }
            else
                return "";
        }