Hello World

C#(WINFORM, WINCE, WINDOWS MOBILE) 실전 활용 예제소스 및 프로그램 개발

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

댓글 0

C#

2016. 2. 17.


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