如何获取本机所有MAC地址

类别:VC语言 点击:0 评论:0 推荐:

// need include file: #include <Nb30.h>

// need libary: Netapi32.lib

int GetAllLocalAdapterMacAddr(std::list<std::vector<unsigned char> >& mac)

{

    NCB ncb;

    LANA_ENUM AdapterList;

 

    memset(&ncb, 0, sizeof(ncb));

    ncb.ncb_command = NCBENUM;

    ncb.ncb_buffer = (unsigned char *)&AdapterList;

    ncb.ncb_length = sizeof(AdapterList);

    Netbios(&ncb);

 

    mac.resize(0);

 

    for (int i = 0; i < AdapterList.length ; ++i )

    {

        struct ASTAT

        {

            ADAPTER_STATUS adapt;

            NAME_BUFFER    psz_name[30];

        } Adapter;

                  

        // Reset the LAN adapter so that we can begin querying it

        NCB Ncb;

        memset( &Ncb, 0, sizeof (Ncb));

        Ncb.ncb_command  = NCBRESET;

        Ncb.ncb_lana_num = AdapterList.lana[i];

                  

        if (Netbios(&Ncb) != NRC_GOODRET)

            continue;

                  

        // Prepare to get the adapter status block

        memset(&Ncb, 0, sizeof(Ncb)) ;

        Ncb.ncb_command = NCBASTAT;

        Ncb.ncb_lana_num = AdapterList.lana[ i ];

        strcpy((char *)Ncb.ncb_callname, "*" );

                 

        memset(&Adapter, 0, sizeof (Adapter));

        Ncb.ncb_buffer = (unsigned char *)&Adapter;

        Ncb.ncb_length = sizeof (Adapter);

                 

        // Get the adapter's info and, if this works, return it in standard,

        // colon-delimited form.

        if ( Netbios( &Ncb ) == 0 )

        {

            std::vector<unsigned char> v6;

            v6.resize(6);

            for (int i=0; i<6; i++)

                v6[i] = Adapter.adapt.adapter_address[i];

            if (v6[0] == 0)

            {

                std::list<std::vector<unsigned char> >::iterator i = mac.begin();

                for (; i!=mac.end(); i++) if (*i == v6)

                    break;

                if (i==mac.end())

                    mac.push_back(v6);

            }

        }

        else

            break;

    }

    return 0;

}

 

 

本文地址:http://com.8s8s.com/it/it1225.htm