`
guowee
  • 浏览: 176726 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Windows Mobile 获得 MAC,IP,IMEI,IMSI

阅读更多

How to get the MAC address of your network adapter.

24 05 2008


MAC address stands for Media Access Control address which is 6 bytes( 48 bits ) long . MAC address is the unique address which is used to identify network interface hardware. So how to get the MAC address of your network adapter?


You can use the function - GetAdaptersInfo() . The network adapter information will be populated and filled back in IP_ADAPTER_INFO structure. In that structure, you can get the network adapter name, MAC address and a couple of other information. See the sample code snippet.

#include "Iphlpapi.h"
...
// Get the buffer length required for IP_ADAPTER_INFO.
ULONG BufferLength = 0;
BYTE* pBuffer = 0;
if( ERROR_BUFFER_OVERFLOW == GetAdaptersInfo( 0, &BufferLength ))
{
    // Now the BufferLength contain the required buffer length.
    // Allocate necessary buffer.
    pBuffer = new BYTE[ BufferLength ];
}
else
{
    // Error occurred. handle it accordingly.
}

// Get the Adapter Information.
PIP_ADAPTER_INFO pAdapterInfo =
      reinterpret_cast<PIP_ADAPTER_INFO>(pBuffer);
GetAdaptersInfo( pAdapterInfo, &BufferLength );

// Iterate the network adapters and print their MAC address.
while( pAdapterInfo )
{
    // Assuming pAdapterInfo->AddressLength is 6.
    CString csMacAddress;
    csMacAddress.Format(_T("%02x:%02x:%02x:%02x:%02x:%02x"),
                        pAdapterInfo->Address[0],
                        pAdapterInfo->Address[1],
                        pAdapterInfo->Address[2],
                        pAdapterInfo->Address[3],
                        pAdapterInfo->Address[4],
                        pAdapterInfo->Address[5]);

    cout << "Adapter Name :" << pAdapterInfo->AdapterName << " "
         << "MAC :" << (LPCTSTR) csMacAddress << endl;

    // Get next adapter info.
    pAdapterInfo = pAdapterInfo->Next;
}

// deallocate the buffer.
delete[] pBuffer;

using System;

using System.Collections;

using System.Runtime.InteropServices;

namespace AdapterInfoTest

{

/// <summary>

/// Summary description for AdapterInfo.

/// </summary>

public sealed class AdapterInfo

{

const int MAX_ADAPTER_NAME_LENGTH = 256;

const int MAX_ADAPTER_DESCRIPTION_LENGTH = 128;

const int MAX_ADAPTER_ADDRESS_LENGTH = 8;

const int ERROR_BUFFER_OVERFLOW = 111;

const int ERROR_SUCCESS = 0;

[ DllImport ( "iphlpapi.dll" , CharSet= CharSet .Ansi)]

private static extern int GetAdaptersInfo( IntPtr pAdapterInfo, ref Int64 pBufOutLen);

[ StructLayout ( LayoutKind .Sequential, CharSet= CharSet .Ansi)]

private struct IP_ADDRESS_STRING

{

[ MarshalAs ( UnmanagedType .ByValTStr, SizeConst=16)]

public string Address;

}

[ StructLayout ( LayoutKind .Sequential, CharSet= CharSet .Ansi)]

private struct IP_ADDR_STRING

{

public IntPtr Next;

public IP_ADDRESS_STRING IpAddress;

public IP_ADDRESS_STRING Mask;

public Int32 Context;

}

 

[ StructLayout ( LayoutKind .Sequential, CharSet= CharSet .Ansi)]

private struct IP_ADAPTER_INFO

{

public IntPtr Next;

public Int32 ComboIndex;

[ MarshalAs ( UnmanagedType .ByValTStr, SizeConst=MAX_ADAPTER_NAME_LENGTH + 4)]

public string AdapterName;

[ MarshalAs ( UnmanagedType .ByValTStr, SizeConst=MAX_ADAPTER_DESCRIPTION_LENGTH + 4)]

public string AdapterDescription;

public UInt32 AddressLength;

[ MarshalAs ( UnmanagedType .ByValArray, SizeConst=MAX_ADAPTER_ADDRESS_LENGTH)]

public byte [] Address;

public Int32 Index;

public UInt32 Type;

public UInt32 DhcpEnabled;

public IntPtr CurrentIpAddress;

public IP_ADDR_STRING IpAddressList;

public IP_ADDR_STRING GatewayList;

public IP_ADDR_STRING DhcpServer;

public bool HaveWins;

public IP_ADDR_STRING PrimaryWinsServer;

public IP_ADDR_STRING SecondaryWinsServer;

public Int32 LeaseObtained;

public Int32 LeaseExpires;

}

public AdapterInfo()

{

}

public static Adapter [] GetAdaptersInfo()

{

Adapter [] adaptersList;

ArrayList adapters = new ArrayList ();

long structSize = Marshal .SizeOf( typeof ( IP_ADAPTER_INFO ) );

IntPtr pArray = Marshal .AllocHGlobal(( int ) structSize );

int ret = GetAdaptersInfo(pArray, ref structSize );

if (ret == ERROR_BUFFER_OVERFLOW ) // ERROR_BUFFER_OVERFLOW == 111

{

// Buffer was too small, reallocate the correct size for the buffer.

pArray = Marshal .ReAllocHGlobal( pArray, new IntPtr (structSize) );

ret = GetAdaptersInfo( pArray, ref structSize );

} // if

if ( ret == 0 )

{

// Call Succeeded

IntPtr pEntry = pArray;

do

{

// Retrieve the adapter info from the memory address

IP_ADAPTER_INFO entry = ( IP_ADAPTER_INFO ) Marshal .PtrToStructure( pEntry, typeof ( IP_ADAPTER_INFO ));

Adapter adapter = new Adapter ();

adapter.index = entry.Index.ToString();

adapter.name = entry.AdapterName;

adapter.description = entry.AdapterDescription;

adapter.ip = entry.IpAddressList.IpAddress.Address;

// MAC Address (data is in a byte[])

string tmpString = string .Empty;

for ( int i = 0; i < entry.AddressLength ; i++)

{

tmpString += string .Format( "{0:X2}" , entry.AddressIdea );

}

adapter.macAddress = tmpString;

adapters.Add(adapter);

// Get next adapter (if any)

pEntry = entry.Next;

}

while ( pEntry != IntPtr .Zero );

Marshal .FreeHGlobal(pArray);

adaptersList = new Adapter [adapters.Count];

adapters.CopyTo(adaptersList);

}

else

{

adaptersList = new Adapter [0];

Marshal .FreeHGlobal(pArray);

throw new InvalidOperationException ( "GetAdaptersInfo failed with " + ret );

}

return adaptersList;

}

}

public class Adapter

{

public string index;

public string name;

public string description;

public string ip;

public string macAddress;

}

}

 

How to get mac address using win32 programming in windows?

get_Adapter getMacAddress(void)
{
get_Adapter getMac;
char getAdap[100];
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;

pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);

if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
}

if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
wsprintf(getAdap , "MacAddress: %02X:%02X:%02X:%02X:%02X:%02X",
pAdapterInfo->Address[0],pAdapterInfo->Address[1],pAdapterInfo->Address[2],
pAdapterInfo->Address[3],pAdapterInfo->Address[4],pAdapterInfo->Address[5]);
getMac.AdapterAddress = getAdap;

wsprintf(getAdap,"Adp Name:%s", pAdapter->AdapterName);
getMac.AdapterName = getAdap;

wsprintf(getAdap,"Adapter Desc: %s", pAdapter->Description);
getMac.AdapterDesc = getAdap;

wsprintf(getAdap,"IP Address: %s", pAdapter->IpAddressList.IpAddress.String);
getMac.IpAddress = getAdap;

wsprintf(getAdap,"IP Mask: %s", pAdapter->IpAddressList.IpMask.String);
getMac.IpMask = getAdap;

_Adapter_Set._Adapter.push_back(getMac);
pAdapter = pAdapter->Next;
}
}
else {
MessageBox(NULL,"Call to GetAdaptersInfo failed","Error",0);
}
return getMac;
}



Don’t forget to add Iphlpapi.lib to project settings.

分享到:
评论

相关推荐

    C# windows mobile 获得IMEI号和IMSI号

    总的来说,使用C#进行Windows Mobile开发时,获取IMEI号相对简单,而获取IMSI号则涉及到更复杂的网络操作。Visual Studio 2008作为强大的开发工具,提供了模拟器和设备调试功能,帮助开发者更好地测试和优化代码。在...

    Windows Mobile 取得 IMEI、IMSI

    在Windows Mobile操作系统中,IMEI(国际移动设备识别码)和IMSI(国际移动用户识别码)是两个非常重要的概念,它们与移动通信设备的身份标识紧密相关。IMEI是每台GSM(全球系统移动通信)和UMTS(通用分组无线业务...

    Windows Mobile和Android获得IMEI、MEID和IMSI demo

    Windows Mobile和Android获得IMEI、MEID和IMSI demo Windows Mobile是从别处拿来的, sim5.rar是IMEI、MEID和IMSI demo CallGetDeviceUniqueId是获取window mobile系统UID的另一个方式 Android很浅显,一起共享吧

    Android 查看手机 IMEI IMSI

    在Android系统中,IMEI(International Mobile Equipment Identity)和IMSI(International Mobile Subscriber Identity)是两个重要的标识符,它们在移动通信中起着至关重要的作用。IMEI是设备的唯一标识,而IMSI则...

    IMSI.rar_IMEI_IMSI_windows mobile

    在移动通信领域,IMSI(International Mobile Subscriber Identity)和IMEI(International Mobile Equipment Identity)是两个非常关键的概念,尤其在Windows Mobile开发中,理解和利用这两个标识符对于开发针对...

    c#获取mobile手机的IMEI和IMSI

    在移动通信领域,IMEI(International Mobile Equipment Identity)和IMSI(International Mobile Subscriber Identity)是两个重要的标识符。IMEI是手机设备的唯一识别码,而IMSI则是移动网络用户的身份标识。在C#...

    imsi_imei Windows Mobile平台获取

    在Windows Mobile平台上,获取IMSI(国际移动用户识别码)和IMEI(国际移动设备识别码)是移动设备开发中的重要任务,这通常涉及到通信模块的管理与设备识别。以下是对这些知识点的详细阐述: IMSI是每个移动网络...

    wince windows mobile 获取IMSI IMEI码

    IMSI国际移动用户识别码(IMSI) international mobile subscriber identity 国际上为唯一识别一个移动用户所分配的号码。从技术上讲,IMSI可以彻底解决国际漫游问题。但是由于北美目前仍有大量的AMPS系统使用MIN...

    WIndow mobile 6.0 以上 获取IMSI和IMEI

    在Windows Mobile 6.0及更高版本的操作系统中,获取手机设备的国际移动用户识别码(IMSI)和国际移动设备标识(IMEI)是开发者进行设备管理或网络通信功能开发时的重要步骤。这两个标识符在移动通信领域扮演着至关...

    VC++手机IMEI/IMSI 读取

    在移动通信领域,IMEI(国际移动设备识别码)和IMSI(国际移动用户标识)是两个关键的概念,用于唯一地标识每一台手机设备和移动网络中的用户身份。本文将详细探讨如何使用VC++编程语言来读取这些信息。 IMEI是每个...

    Tapi.zip_IMEI_IMSI_tapi

    在IT行业中,IMEI(International Mobile Equipment Identity)和IMSI(International Mobile Subscriber Identity)是两个非常重要的概念,尤其是在移动通信领域。IMEI是设备的唯一标识符,而IMSI则是用户的唯一...

    WinCE下100%获取IMEI和IMSI号

    在Windows CE(WinCE)操作系统环境下,IMEI(国际移动设备识别码)和IMSI(国际移动用户识别码)是两个关键的标识符,用于唯一地识别移动通信设备和其对应的用户。IMEI通常用于追踪和识别手机,而IMSI是SIM卡上的...

    android平台获取手机IMSI,IMEI ,序列号,和 手机号的方法

    Android 平台获取手机 IMSI、IMEI、序列号和手机号的方法 Android 操作系统提供了多种方法来获取手机的 IMSI、IMEI、序列号和手机号,这些信息对于移动应用程序的开发和维护非常重要。本文将详细介绍 Android 平台...

    android 显示手机号码,手机imei imsi 手机设备号

    在Android系统中,获取和显示手机号码、IMEI(国际移动设备识别码)以及IMSI(国际移动用户识别码)是常见的需求,这些信息对于设备管理和应用开发具有重要作用。下面将详细阐述这些概念以及如何在Android中获取它们...

    C# Windows Mobile 读取SIM卡信息

    本主题将深入探讨如何在Windows Mobile 5.0环境下,利用Visual Studio 2008和.NET Compact Framework 2.0(CF2.0)来读取SIM卡的相关信息,包括IMEI、IMSI和ICCID。 **IMEI(国际移动设备识别码)** 是每个移动设备...

    Android 读取IMSI 和IMEI号支持双卡双待

    在Android系统中,IMSI(International Mobile Subscriber Identity)和IMEI(International Mobile Equipment Identity)是两个重要的标识符,用于区分不同的移动设备和用户。本文将深入探讨如何在Android平台上...

    IMEI_IMSI.rar_IMEI_IMSI_SIM卡imsi_visual c_wince_wince IMEI

    WinCE 获取PDA的SIM卡号和设备序列号

    获取手机IMEI号,sim卡的IMSI号C#源码

    在移动通信领域,IMEI(International Mobile Equipment Identity)和IMSI(International Mobile Subscriber Identity)是两个关键的概念。IMEI是国际移动设备身份码,用于唯一标识一部移动设备,而IMSI则是国际...

    IMSI获取,IMEI获取

    IMSI获取 安卓手机IMSI 和IMEI获取,安装后打开即可看到

Global site tag (gtag.js) - Google Analytics