Get wince mac address and IP address solution

  • 2020-05-07 20:20:22
  • OfStack

The development environment I use is VS2008, and the mobile terminal version of the developed system is windows mobile 5.0. Due to the need for authentication, the MAC address of the mobile terminal needs to be obtained. Therefore, I searched the Internet and mainly found three methods to obtain the MAC address, which are recorded as follows.

method 1 : get it using ManagementClass.
In fact, WinCE does not have System.Management, this method simply does not work.

method 2, : get the MAC address by looking up the registry.
This is the code to get the registry address:
 
txtMAC1.Text = reg.ReadValue(YFReg.HKEY.HKEY_LOCAL_MACHINE, @"Comm\DM9CE1\Parms", "SoftwareMacAddress0"); 

I won't list the rest of the code here, but I didn't get the MAC address in this way. Therefore, I downloaded a registry view tool from the Internet, searched in the mobile terminal, and found that there was no Comm\DM9CE1\Parms path, and found other paths, but I could not find any SoftwareMacAddress node. Well, maybe this method will get the MAC address, but my version won't.

third method : get the MAC address through SendARP.
The code is as follows:
 
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> 
///  To obtain MAC address  
/// </summary> 
/// <returns></returns> 
public string GetMac() 
{ 
uint ip = 0; 
string mac = string.Empty; 
// Take the machine IP The list of  
IPAddress[] ips = Dns.GetHostEntry(Dns.GetHostName()).AddressList; 
// Take the machine IP 
byte[] ipp = ips[1].GetAddressBytes(); 
ip = (uint)((ipp[0]) | (ipp[1] << 8) | (ipp[2] << 16) | (ipp[3] << 24)); 
// take 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> 
/// Access to the machine IP 
/// </summary> 
/// <returns></returns> 
public string GetIpAddress() 
{ 
string strHostName = Dns.GetHostName(); // Gets the host name of the machine  
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); // Get the machine IP 
string strAddr = ipEntry.AddressList[1].ToString(); 
return strAddr; 
} 
} 
} 

Sending an ARP request through SendARP in IP Helper API can be used to get the MAC address that specifies the IP address. It is simple and convenient, but it cannot cross the gateway.
As for getting the IP address, this article has given two methods, both of which are obtained through methods in the DNS class under NET.

Related articles: