当前位置: 移动技术网 > IT编程>开发语言>c# > 获取wince mac地址与IP地址解决方案

获取wince mac地址与IP地址解决方案

2019年07月18日  | 移动技术网IT编程  | 我要评论
本人所使用的开发环境是vs2008,开发的系统所在移动终端版本为windows mobile 5.0。由于需要进行身份的验证,需要获取移动终端的mac地址,于是在网上进行搜
本人所使用的开发环境是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类中方法获取。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网