国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

獲取wince mac地址與IP地址解決方案

2020-01-24 03:38:03
字體:
來源:轉載
供稿:網友
本人所使用的開發環境是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類中方法獲取。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汤阴县| 九龙县| 江都市| 黔南| 女性| 定远县| 长泰县| 象州县| 鄂伦春自治旗| 多伦县| 扶余县| 梨树县| 虎林市| 三门峡市| 镇宁| 庆云县| 吉隆县| 南郑县| 措勤县| 西城区| 齐河县| 荆州市| 长兴县| 垦利县| 乌鲁木齐市| 邵武市| 南京市| 屯门区| 商水县| 社旗县| 京山县| 开平市| 罗江县| 吉木萨尔县| 宜章县| 方山县| 安丘市| 宜君县| 清河县| 古田县| 东海县|