獲取wince mac地址與IP地址解決方案
更新時(shí)間:2012年12月02日 15:46:45 作者:
由于需要進(jìn)行身份的驗(yàn)證,需要獲取移動(dòng)終端的MAC地址,于是在網(wǎng)上進(jìn)行搜索整理一番,現(xiàn)在將實(shí)現(xiàn)獲取MAC地址的方法與大家共享
本人所使用的開發(fā)環(huán)境是VS2008,開發(fā)的系統(tǒng)所在移動(dòng)終端版本為windows mobile 5.0。由于需要進(jìn)行身份的驗(yàn)證,需要獲取移動(dòng)終端的MAC地址,于是在網(wǎng)上進(jìn)行搜索,主要看到了三種方法來(lái)實(shí)現(xiàn)獲取MAC地址,現(xiàn)記錄如下。
第一種方法:使用ManagementClass 來(lái)獲取。
殊不知,WinCE下并沒有System.Management,這種方法根本行不通。
第二種方法:通過(guò)查找注冊(cè)表來(lái)獲取MAC地址。
這是獲取注冊(cè)表地址的代碼:
txtMAC1.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress0");
其他的代碼我這里就不列出來(lái)了,用這種方法我并沒有獲取到MAC地址。于是在網(wǎng)上下載了一個(gè)注冊(cè)表查看工具,在移動(dòng)終端中找,找遍了,發(fā)現(xiàn)并沒有Comm\DM9CE1\Parms路徑,再找其他的路徑,都沒找到有SoftwareMacAddress節(jié)點(diǎn)的。好吧,可能這種方法能獲取MAC地址,但是我這個(gè)版本的不行。
第三種方法:通過(guò)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;
//取本機(jī)IP列表
IPAddress[] ips = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
//取本機(jī)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>
///獲取本機(jī)IP
/// </summary>
/// <returns></returns>
public string GetIpAddress()
{
string strHostName = Dns.GetHostName(); //得到本機(jī)的主機(jī)名
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本機(jī)IP
string strAddr = ipEntry.AddressList[1].ToString();
return strAddr;
}
}
}
通過(guò) IP Helper API 中的 SendARP 發(fā)送 ARP 請(qǐng)求可以用來(lái)獲取指定IP地址的MAC 地址,簡(jiǎn)單方便,缺點(diǎn)是不能跨越網(wǎng)關(guān)。
至于獲取IP地址,本文已經(jīng)給出了兩種方法,都是通過(guò)NET下DNS類中方法獲取。
第一種方法:使用ManagementClass 來(lái)獲取。
殊不知,WinCE下并沒有System.Management,這種方法根本行不通。
第二種方法:通過(guò)查找注冊(cè)表來(lái)獲取MAC地址。
這是獲取注冊(cè)表地址的代碼:
復(fù)制代碼 代碼如下:
txtMAC1.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress0");
其他的代碼我這里就不列出來(lái)了,用這種方法我并沒有獲取到MAC地址。于是在網(wǎng)上下載了一個(gè)注冊(cè)表查看工具,在移動(dòng)終端中找,找遍了,發(fā)現(xiàn)并沒有Comm\DM9CE1\Parms路徑,再找其他的路徑,都沒找到有SoftwareMacAddress節(jié)點(diǎn)的。好吧,可能這種方法能獲取MAC地址,但是我這個(gè)版本的不行。
第三種方法:通過(guò)SendARP獲取MAC地址。
代碼如下:
復(fù)制代碼 代碼如下:
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;
//取本機(jī)IP列表
IPAddress[] ips = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
//取本機(jī)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>
///獲取本機(jī)IP
/// </summary>
/// <returns></returns>
public string GetIpAddress()
{
string strHostName = Dns.GetHostName(); //得到本機(jī)的主機(jī)名
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本機(jī)IP
string strAddr = ipEntry.AddressList[1].ToString();
return strAddr;
}
}
}
通過(guò) IP Helper API 中的 SendARP 發(fā)送 ARP 請(qǐng)求可以用來(lái)獲取指定IP地址的MAC 地址,簡(jiǎn)單方便,缺點(diǎn)是不能跨越網(wǎng)關(guān)。
至于獲取IP地址,本文已經(jīng)給出了兩種方法,都是通過(guò)NET下DNS類中方法獲取。
相關(guān)文章

c#棧變化規(guī)則圖解示例(棧的生長(zhǎng)與消亡)
多數(shù)情況下我們不需要關(guān)心棧的變化,下文會(huì)給出一個(gè)具體的示例。另外,理解棧的變化對(duì)于理解作用域也有一定的好處,因?yàn)镃#的局部變量作用域是基于棧的。
2013-11-11 
C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法
這篇文章主要介紹了C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法,涉及C#線程與隊(duì)列的相關(guān)操作技巧,需要的朋友可以參考下
2015-07-07 
C#使用Socket發(fā)送和接收TCP數(shù)據(jù)實(shí)例
這篇文章主要介紹了C#使用Socket發(fā)送和接收TCP數(shù)據(jù)的實(shí)現(xiàn)方法,以實(shí)例的形式詳細(xì)講述了C#實(shí)現(xiàn)socket通信的完整實(shí)現(xiàn)過(guò)程,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
2014-10-10