Obtain the wince mac address and IP address Solution

Source: Internet
Author: User

My development environment is VS2008, and the mobile terminal version of the developed system is windows mobile 5.0. Because identity authentication is required and the MAC address of the mobile terminal needs to be obtained, you can search the Internet to obtain the MAC address in three ways. The record is as follows.

Method 1: Use ManagementClass to obtain it.
But there is no System. Management in WinCE, and this method won't work at all.

Method 2: Obtain the MAC address by searching for the Registry.
Here is the code for obtaining the Registry address:Copy codeThe Code is as follows: txtMAC1.Text = reg. ReadValue (YFReg. HKEY. HKEY_LOCAL_MACHINE, @ "Comm \ DM9CE1 \ Parms", "SoftwareMacAddress0 ");

I won't list other codes here. I didn't get the MAC address in this way. So I downloaded a registry viewing tool from the Internet, searched it on the mobile terminal, and found that there was no Comm \ DM9CE1 \ Parms path, and found another path, no SoftwareMacAddress node found. Okay, maybe this method can get the MAC address, but this version does not work for me.

Method 3: Obtain the MAC address through SendARP.
The Code is as follows:Copy codeThe 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>
/// Obtain the MAC address
/// </Summary>
/// <Returns> </returns>
Public string GetMac ()
{
Uint ip = 0;
String mac = string. Empty;
// Obtain the IP address list of the Local Machine
IPAddress [] ips = Dns. GetHostEntry (Dns. GetHostName (). AddressList;
// Obtain the local IP Address
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>
/// Obtain the local IP Address
/// </Summary>
/// <Returns> </returns>
Public string GetIpAddress ()
{
String strHostName = Dns. GetHostName (); // obtain the Host Name of the local machine.
IPHostEntry ipEntry = Dns. GetHostByName (strHostName); // obtain the local IP address.
String strAddr = ipEntry. AddressList [1]. ToString ();
Return strAddr;
}
}
}

An ARP request sent through SendARP in the IP Helper API can be used to obtain the MAC address of the specified IP address, which is simple and convenient, but cannot cross the gateway.
As for how to obtain the IP address, this article provides two methods, both of which are obtained through the method in the DNS class under NET.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.