关键词搜索

源码搜索 ×
×

WinCE 读取Mac地址实践测试

发布2014-02-13浏览6036次

详情内容

     由于错误的理解WinCE 读取Mac地址就是在注册表中所以怎么读都读不到数据,在此记录测试过程。

1、通过注册表中读取Mac地址

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using SmartDeviceProjectWtms;
  6. using Microsoft.Win32;
  7. using System.Windows.Forms;
  8. namespace SmartDeviceProjectWtms
  9. {
  10. class MacReader
  11. {
  12. public enum HKEY { HKEY_LOCAL_MACHINE = 0, HKEY_CLASSES_ROOT = 1, HKEY_CURRENT_USER = 2, HKEY_USERS = 3 };
  13. private RegistryKey[] reg = new RegistryKey[4];
  14. public MacReader()
  15. {
  16. reg[(int)HKEY.HKEY_LOCAL_MACHINE] = Registry.LocalMachine;
  17. reg[(int)HKEY.HKEY_CLASSES_ROOT] = Registry.ClassesRoot;
  18. reg[(int)HKEY.HKEY_CURRENT_USER] = Registry.CurrentUser;
  19. reg[(int)HKEY.HKEY_USERS] = Registry.Users;
  20. }
  21. //读指定变量值
  22. public string ReadValue(HKEY Root, string SubKey, string ValueName)
  23. {
  24. RegistryKey subKey = reg[(int)Root];
  25. if (ValueName.Length == 0) return "[ERROR]";
  26. try
  27. {
  28. if (SubKey.Length > 0)
  29. {
  30. string[] strSubKey = SubKey.Split('\\');
  31. foreach (string strKeyName in strSubKey)
  32. {
  33. subKey = subKey.OpenSubKey(strKeyName);
  34. }
  35. }
  36. string strKey = subKey.GetValue(ValueName).ToString();
  37. subKey.Close();
  38. return strKey;
  39. }
  40. catch(Exception ex)
  41. {
  42. MessageBox.Show(ex.Message);
  43. return "[ERROR]";
  44. }
  45. }
  46. //获取mac地址
  47. public static string GetMacAddr()
  48. {
  49. MacReader reg = new MacReader();
  50. string mac = reg.ReadValue(MacReader.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "NetWorkAddress").ToUpper();
  51. if (mac.Length == 12)
  52. {
  53. mac = mac.Substring(0, 2) + "-" + mac.Substring(2, 2) + "-" + mac.Substring(4, 2) + "-" +
  54. mac.Substring(6, 2) + "-" + mac.Substring(8, 2) + "-" + mac.Substring(10, 2);
  55. }
  56. return mac;
  57. }
  58. }
  59. }

注:智能设备中按照@“Comm\DM9CE1\Parms”地址读取不到所谓的mac值,无论使用“NetWorkAddress”或是“NetWorkAddress0”,应该是找不到路径所致。

2、通过SendARP获取MAC地址

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Collections;
  9. using System.Diagnostics;
  10. using System.Runtime.InteropServices;
  11. using System.IO;
  12. using System.Security.Cryptography;
  13. using System.Net;
  14. namespace SmartDeviceProjectWtms
  15. {
  16. class SysInfoReader
  17. {
  18. private static string[] strEncrypt = new string[] { "1", "https://cdn.jxasp.com:9143/image/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" };
  19. private static Int32 METHOD_BUFFERED = 0;
  20. private static Int32 FILE_ANY_ACCESS = 0;
  21. private static Int32 FILE_DEVICE_HAL = 0x00000101;
  22. private const Int32 ERROR_NOT_SUPPORTED = 0x32;
  23. private const Int32 ERROR_INSUFFICIENT_BUFFER = 0x7A;
  24. private static Int32 IOCTL_HAL_GET_DEVICEID = ((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14) | ((21) << 2) | (METHOD_BUFFERED);
  25. [DllImport("coredll.dll", SetLastError = true)]
  26. private static extern bool KernelIoControl(Int32 dwIoControlCode, IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf, Int32 nOutBufSize, ref Int32 lpBytesReturned);
  27. [DllImport("Iphlpapi.dll", EntryPoint = "SendARP")]
  28. public static extern uint SendARP(uint DestIP, uint SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);
  29. /// 获取MAC地址
  30. public string GetMac()
  31. {
  32. uint ip = 0;
  33. string mac = string.Empty;
  34. //取本机IP列表
  35. IPAddress[] ips = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
  36. //取本机IP
  37. byte[] ipp = ips[1].GetAddressBytes();
  38. ip = (uint)((ipp[0]) | (ipp[1] << 8) | (ipp[2] << 16) | (ipp[3] << 24));
  39. //取MAC
  40. byte[] MacAddr = new byte[6];
  41. uint PhyAddrLen = 6;
  42. uint hr = SendARP(ip, 0, MacAddr, ref PhyAddrLen);
  43. if (MacAddr[0] != 0 || MacAddr[1] != 0 || MacAddr[2] != 0 || MacAddr[3] != 0 || MacAddr[4] != 0 || MacAddr[5] != 0)
  44. {
  45. 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");
  46. }
  47. return mac;
  48. }
  49. ///获取本机IP
  50. public string GetIpAddress()
  51. {
  52. string strHostName = Dns.GetHostName(); //得到本机的主机名
  53. IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);//Dns.GetHostByName(strHostName); //取得本机IP ,此方法已过时
  54. string strAddr = ipEntry.AddressList[1].ToString();
  55. return strAddr;
  56. }
  57. }
  58. }

此法则成功获取到了mac地址。


相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载