VC++ 获取本机MAC地址列表

方法一: 

#include <windows.h> 
#include <wincon.h> 
#include <stdio.h> 
#include <nb30.h> 
#define MACSESION 6
#pragma warning( disable : 4996 )
#pragma comment(lib,"Netapi32.lib")

#include <vector>
#include <string>
std::string getmac_one( int lana_num )
{
    typedef struct _ASTAT_ 
    { 
        ADAPTER_STATUS adapt; 
        NAME_BUFFER NameBuff [30]; 
    }ASTAT, * PASTAT; 
    ASTAT Adapter;
    NCB ncb; 
    UCHAR uRetCode; 

    memset( &ncb, 0, sizeof( ncb ) ); 
    ncb.ncb_command = NCBRESET; 
    ncb.ncb_lana_num = lana_num; 
    uRetCode = Netbios( &ncb ); 
    memset( &ncb, 0, sizeof( ncb ) ); 
    ncb.ncb_command = NCBASTAT; 
    ncb.ncb_lana_num = lana_num; 
    strcpy( (char *)ncb.ncb_callname, "* " ); 
    ncb.ncb_buffer = ( unsigned char * ) &Adapter; 
    ncb.ncb_length = sizeof( Adapter ); 
    uRetCode = Netbios( &ncb ); 
    std::string s;
    if ( uRetCode == 0 ) 
    {
        int bAddressInt [ MACSESION ];
        char CommarSeperatedAddress[ MACSESION * 3 ]={0};
        for( int i = 0; i < MACSESION; ++i )
        {
            bAddressInt[ i ] = Adapter.adapt.adapter_address[ i ];
            bAddressInt[ i ] &= 0x000000ff;
        }
        sprintf( CommarSeperatedAddress, "%02x:%02x:%02x:%02x:%02x:%02x",
            bAddressInt[ 0 ],
            bAddressInt[ 1 ],
            bAddressInt[ 2 ],
            bAddressInt[ 3 ],
            bAddressInt[ 4 ],
            bAddressInt[ 5 ]); // Should use scl::FormatString inside 
        s = CommarSeperatedAddress;
    }

    return s;
}

void GetMacAddresses(std::vector<std::string>& result)
{
    NCB ncb; 
    UCHAR uRetCode; 
    LANA_ENUM lana_enum; 
    memset( &ncb, 0, sizeof( ncb ) ); 
    ncb.ncb_command = NCBENUM; 
    ncb.ncb_buffer = (unsigned char *) &lana_enum; 
    ncb.ncb_length = sizeof( lana_enum ); 
    uRetCode = Netbios( &ncb ); 
    if ( uRetCode == 0 ) 
    { 
        for ( int i=0; i < lana_enum.length; ++i )
        {
            std::string s = getmac_one( lana_enum.lana[i] ); 
            if( ! s.empty() )
            {
				result.push_back( s );
            }
        }
    }
}

方法二:

#include <windows.h> 
#include <wincon.h> 
#include <stdio.h> 
#include <Winsock2.h>
#include <Iphlpapi.h>
#include <vector>
#include <string>

#pragma comment(lib,"Iphlpapi")
#pragma warning( disable : 4996 )

std::string& trim(std::string &s)
{
    if (s.empty()) 
        return s;

    s.erase(0,s.find_first_not_of(" "));
    s.erase(s.find_last_not_of(" ") + 1);
    return s;
}


void GetMacAddresses(std::vector<std::string>& result)
{
    PIP_ADAPTER_ADDRESSES pAddresses = NULL;
    ULONG family = AF_UNSPEC;
    ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
    ULONG outBufLen = sizeof( IP_ADAPTER_ADDRESSES );
    if ( GetAdaptersAddresses( family, flags, NULL, pAddresses, &outBufLen ) == ERROR_BUFFER_OVERFLOW )
        pAddresses = static_cast<PIP_ADAPTER_ADDRESSES>( HeapAlloc( GetProcessHeap(), 0, outBufLen ) );

    _ASSERT( pAddresses );
    DWORD dwRetVal = GetAdaptersAddresses( family, flags, NULL, pAddresses, &outBufLen );
    if ( dwRetVal != ERROR_SUCCESS )
        return ;

    PIP_ADAPTER_ADDRESSES pFirst = pAddresses;
    while( pAddresses )
    {
        if (pAddresses->PhysicalAddressLength != 0)
        {
            char CommarSeperatedAddress[ MACSESION * 3 ] = { 0 };
            sprintf( CommarSeperatedAddress, "%02x:%02x:%02x:%02x:%02x:%02x",
                pAddresses->PhysicalAddress[ 0 ],
                pAddresses->PhysicalAddress[ 1 ],
                pAddresses->PhysicalAddress[ 2 ],
                pAddresses->PhysicalAddress[ 3 ],
                pAddresses->PhysicalAddress[ 4 ],
                pAddresses->PhysicalAddress[ 5 ]);
            
			std::string sMac = std::string(CommarSeperatedAddress);
			trim(sMac);
			if ( sMac!="00:00:00:00:00:00" )
				result.push_back(sMac);
        }

		pAddresses = pAddresses->Next ? pAddresses->Next : NULL;
    }

    HeapFree( GetProcessHeap(), 0, pFirst );
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值