-
C#教程之获取wince mac地址与IP地址解决方案
本人所使用的开发环境是VS2008,开发的系统所在移动终端版本为windows mobile 5.0。由于需要进行身份的验证,需要获取移动终端的MAC地址,于是在网上进行搜索,主要看到了三种方法来实现获取MAC地址,现记录如下。
第一种方法:使用ManagementClass 来获取。
殊不知,WinCE下并没有System.Management,这种方法根本行不通。
第二种方法:通过查找注册表来获取MAC地址。
这是获取注册表地址的代码:
txtMAC1.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress0");
其他的代码我这里就不列出来了,用这种方法我并没有获取到MAC地址。于是在网上下载了一个注册表查看工具,在移动终端中找,找遍了,发现并没有Comm\DM9CE1\Parms路径,再找其他的路径,都没找到有SoftwareMacAddress节点的。好吧,可能这种方法能获取MAC地址,但是我这个版本的不行。
第三种方法:通过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 WirelessRouteSystem
{
class SysInfo
{
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);
/// <summary>
/// 获取MAC地址
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
///获取本机IP
/// </summary>
/// <returns></returns>
public string GetIpAddress()
{
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP
string strAddr = ipEntry.AddressList[1].ToString();
return strAddr;
}
}
}
通过 IP Helper API 中的 SendARP 发送 ARP 请求可以用来获取指定IP地址的MAC 地址,简单方便,缺点是不能跨越网关。
至于获取IP地址,本文已经给出了两种方法,都是通过NET下DNS类中方法获取。
第一种方法:使用ManagementClass 来获取。
殊不知,WinCE下并没有System.Management,这种方法根本行不通。
第二种方法:通过查找注册表来获取MAC地址。
这是获取注册表地址的代码:
复制代码 代码如下:
txtMAC1.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress0");
其他的代码我这里就不列出来了,用这种方法我并没有获取到MAC地址。于是在网上下载了一个注册表查看工具,在移动终端中找,找遍了,发现并没有Comm\DM9CE1\Parms路径,再找其他的路径,都没找到有SoftwareMacAddress节点的。好吧,可能这种方法能获取MAC地址,但是我这个版本的不行。
第三种方法:通过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 WirelessRouteSystem
{
class SysInfo
{
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);
/// <summary>
/// 获取MAC地址
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
///获取本机IP
/// </summary>
/// <returns></returns>
public string GetIpAddress()
{
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP
string strAddr = ipEntry.AddressList[1].ToString();
return strAddr;
}
}
}
通过 IP Helper API 中的 SendARP 发送 ARP 请求可以用来获取指定IP地址的MAC 地址,简单方便,缺点是不能跨越网关。
至于获取IP地址,本文已经给出了两种方法,都是通过NET下DNS类中方法获取。
最新更新
Objective-C语法之代码块(block)的使用
VB.NET eBook
Add-in and Automation Development In VB.NET 2003 (F
Add-in and Automation Development In VB.NET 2003 (8
Add-in and Automation Development in VB.NET 2003 (6
Add-in and Automation Development In VB.NET 2003 (5
AddIn Automation Development In VB.NET 2003 (4)
AddIn And Automation Development In VB.NET 2003 (2)
Addin and Automation Development In VB.NET 2003 (3)
AddIn And Automation Development In VB.NET 2003 (1)
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
武装你的WEBAPI-OData入门
武装你的WEBAPI-OData便捷查询
武装你的WEBAPI-OData分页查询
武装你的WEBAPI-OData资源更新Delta
5. 武装你的WEBAPI-OData使用Endpoint 05-09
武装你的WEBAPI-OData之API版本管理
武装你的WEBAPI-OData常见问题
武装你的WEBAPI-OData聚合查询
OData WebAPI实践-OData与EDM
OData WebAPI实践-Non-EDM模式