方法一:
#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 );
}