WinCE読み取りMacアドレス実践テスト

5794 ワード

WinCEがMacアドレスを読み込んだのはレジストリ内であることを誤って理解しているため、どのように読み取ってもデータが読めず、ここでテストプロセスを記録する.
1.レジストリからMacアドレスを読み込む
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using SmartDeviceProjectWtms;
using Microsoft.Win32;
using System.Windows.Forms;

namespace SmartDeviceProjectWtms
{
    class MacReader
    {
        public enum HKEY { HKEY_LOCAL_MACHINE = 0, HKEY_CLASSES_ROOT = 1, HKEY_CURRENT_USER = 2, HKEY_USERS = 3 };

        private RegistryKey[] reg = new RegistryKey[4];

        public MacReader()
        {
            reg[(int)HKEY.HKEY_LOCAL_MACHINE] = Registry.LocalMachine;
            reg[(int)HKEY.HKEY_CLASSES_ROOT] = Registry.ClassesRoot;
            reg[(int)HKEY.HKEY_CURRENT_USER] = Registry.CurrentUser;
            reg[(int)HKEY.HKEY_USERS] = Registry.Users;
        }

        // 
        public string ReadValue(HKEY Root, string SubKey, string ValueName)
        {
            RegistryKey subKey = reg[(int)Root];
            if (ValueName.Length == 0) return "[ERROR]";
            try
            {
                if (SubKey.Length > 0)
                {
                    string[] strSubKey = SubKey.Split('\\');
                    foreach (string strKeyName in strSubKey)
                    {
                        subKey = subKey.OpenSubKey(strKeyName);
                    }
                }
                string strKey = subKey.GetValue(ValueName).ToString();
                subKey.Close();
                return strKey;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return "[ERROR]";
            }
        }

        // mac 
        public static string GetMacAddr()
        {
            MacReader reg = new MacReader();
            string mac = reg.ReadValue(MacReader.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "NetWorkAddress").ToUpper();
            if (mac.Length == 12)
            {
                mac = mac.Substring(0, 2) + "-" + mac.Substring(2, 2) + "-" + mac.Substring(4, 2) + "-" +
                      mac.Substring(6, 2) + "-" + mac.Substring(8, 2) + "-" + mac.Substring(10, 2);
            }
            return mac;
        }
    }
}

注意:インテリジェントデバイスでは、@「CommDM 9 CE 1Parms」アドレスに従ってmac値が読み込まれません.「NetWorkAddress」または「NetWorkAddress 0」を使用しても、パスが見つからないためです.
2.SendARPによるMACアドレスの取得
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
using System.Security.Cryptography;
using System.Net; 

namespace SmartDeviceProjectWtms
{
    class SysInfoReader
    {
        private static string[] strEncrypt = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM", "AN", "AO", "AP" };

        private static Int32 METHOD_BUFFERED = 0;

        private static Int32 FILE_ANY_ACCESS = 0;

        private static Int32 FILE_DEVICE_HAL = 0x00000101;

        private const Int32 ERROR_NOT_SUPPORTED = 0x32;

        private const Int32 ERROR_INSUFFICIENT_BUFFER = 0x7A;

        private static Int32 IOCTL_HAL_GET_DEVICEID = ((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14) | ((21) << 2) | (METHOD_BUFFERED);

        [DllImport("coredll.dll", SetLastError = true)]
        private static extern bool KernelIoControl(Int32 dwIoControlCode, IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf, Int32 nOutBufSize, ref   Int32 lpBytesReturned);

        [DllImport("Iphlpapi.dll", EntryPoint = "SendARP")]
        public static extern uint SendARP(uint DestIP, uint SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);

        ///  MAC  
        public string GetMac()
        {
            uint ip = 0;
            string mac = string.Empty;
            // IP  
            IPAddress[] ips = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            // IP 
            byte[] ipp = ips[1].GetAddressBytes();
            ip = (uint)((ipp[0]) | (ipp[1] << 8) | (ipp[2] << 16) | (ipp[3] << 24));
            // MAC 
            byte[] MacAddr = new byte[6];
            uint PhyAddrLen = 6;
            uint hr = SendARP(ip, 0, MacAddr, ref PhyAddrLen);
            if (MacAddr[0] != 0 || MacAddr[1] != 0 || MacAddr[2] != 0 || MacAddr[3] != 0 || MacAddr[4] != 0 || MacAddr[5] != 0)
            {
                mac = MacAddr[0].ToString("X2") + ":" + MacAddr[1].ToString("X2") + ":" + MacAddr[2].ToString("X2") + ":" + MacAddr[3].ToString("X2") + ":" + MacAddr[4].ToString("X2") + ":" + MacAddr[5].ToString("X2");
            }
            return mac;
        }

        /// IP
        public string GetIpAddress() 
        { 
            string strHostName = Dns.GetHostName(); //  
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);//Dns.GetHostByName(strHostName); // IP , 
            string strAddr = ipEntry.AddressList[1].ToString(); 
            return strAddr;
        } 
    }
}

この法則はmacアドレスの取得に成功した.