Get wince MAC address and IP address solution _c# Tutorial

Source: Internet
Author: User
I am using the development environment is VS2008, the development of the system where the mobile terminal version of Windows Mobile 5.0. Because of the need for identity verification, the need to obtain the mobile terminal MAC address, so the search on the internet, mainly saw three ways to achieve the MAC address, is recorded below.

The first of these methods: Use ManagementClass to get.
As everyone knows, wince there is no system.management, this method is not feasible.

The second method: Gets the MAC address by looking for the registry.
This is the code to get the registry address:
Copy Code code as follows:

Txtmac1.text = Reg. ReadValue (YFReg.HKEY.HKEY_LOCAL_MACHINE, @ "Comm\dm9ce1\parms", "SoftwareMacAddress0");

The rest of the code is not listed here, in this way I did not get a MAC address. So in the online download a Registry View tool, find in the mobile terminal, looked, found that there is no comm\dm9ce1\parms path, and then find other paths, have not found softwaremacaddress node. Well, maybe this is a way to get a MAC address, but I can't do this version.

The third method: Gets the MAC address via Sendarp.
The code is as follows:
Copy Code code 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", "A K "," 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) | (() << 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>
Get MAC Address
</summary>
<returns></returns>
public string Getmac ()
{
UINT IP = 0;
String mac = String. Empty;
Take the native IP list
ipaddress[] ips = Dns.gethostentry (Dns.gethostname ()). AddressList;
Take native 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>
Get native IP
</summary>
<returns></returns>
public string getipaddress ()
{
String strhostname = Dns.gethostname (); Get the host name of this machine
Iphostentry ipentry = Dns.gethostbyname (strhostname); Get native IP
String straddr = Ipentry.addresslist[1]. ToString ();
return straddr;
}
}
}

Sending ARP requests via Sendarp in the IP Helper API can be used to obtain the MAC address of the specified IP address, which is simple and convenient, and the disadvantage is that it cannot cross the gateway.
As far as obtaining the IP address, this article has given two methods, all are obtains through the net under the DNS class method.

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.