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.Address
);
}
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.
分享到:
相关推荐
如標題同 Windows Mobile 取得 IMEI、IMSI
Windows Mobile和Android获得IMEI、MEID和IMSI demo Windows Mobile是从别处拿来的, sim5.rar是IMEI、MEID和IMSI demo CallGetDeviceUniqueId是获取window mobile系统UID的另一个方式 Android很浅显,一起共享吧
在模拟器5.0上通过6.0一上模拟器不支持,但在真机6.0以上也通过。 使用Visual studio 2008工具
简单的DEMO 用于查看Android手机的IMEI IMSI
c#获取mobile手机的IMEI和IMSI!
Windows Mobile 开发获取手机IMSI码和IMEI码
android 显示手机号码,手机imei imsi 手机设备号,android 显示手机号码,手机imei imsi 手机设备号
IMSI国际移动用户识别码(IMSI) international mobile subscriber identity 国际上为唯一识别一个移动用户所分配的号码。从技术上讲,IMSI可以彻底解决国际漫游问题。但是由于北美目前仍有大量的AMPS系统使用MIN...
imsi和imei Windows Mobile平台获取 通过TPAI获取手机的imsi 以及imei号
WIndow mobile 6.0 以上 获取IMSI和IMEI.rar
VC++编写的手机设备信息读取,包括IMEI IMSI 设备ID
获取手机硬件号(如 IMEI IMSI 号等)
WinCE 获取PDA的SIM卡号和设备序列号
IMSI获取 安卓手机IMSI 和IMEI获取,安装后打开即可看到
Android 读取IMSI 和IMEI号支持双卡双待,可以判断双卡双待通过那个卡上网,及读取双卡双待的电话卡的信息
绝对不是网络上搜索的那种获取IMEI的方式,100%原创 C++语言
读取的SIM卡信息包括:IMEI、IMSI、ICCID 开发环境:VS 2008,CF2.0,Windows Mobile 5.0 SDK ICCID 信息不是十分准确只供参考。
获取手机IMEI号,sim卡的IMSI号,手机型号等 C#源码
正则表达式 详解 基础 正则表达式之道(A Tao of Regular Expressions)