GetAdaptersInfo获取MAC地址

转自:http://wingjang.blog.163.com/blog/static/47913442201082823833637/

源代码:
#include
#include
#include"iphlpapi.h"

#pragma comment(lib,"Iphlpapi.lib")

int main(int argc,char*argv[])
{
    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_SUCCESS)
    {
        GlobalFree(pAdapterInfo);
        pAdapterInfo=(IP_ADAPTER_INFO*)malloc(ulOutBufLen);
            
    }
    
    if((dwRetVal=GetAdaptersInfo(pAdapterInfo,&ulOutBufLen))==NO_ERROR)
    {
        pAdapter=pAdapterInfo;
        while(pAdapter)
        {
            if(strstr(pAdapter->Description,"PCI")>=0//pAdapter->Description中包含"PCI"为:物理网卡
              || pAdapter->Type==71          //pAdapter->Type是71为:无线网卡
              )
          {
              printf("--------\n");
              printf("AdapterName:%s\n",pAdapter->AdapterName);
              printf("AdapterDesc:%s\n",pAdapter->Description);
              printf("AdapterAddr:");
              for(UINT i=0;iAddressLength;i++)
              {
                  printf("%02X%c",pAdapter->Address[i],i==pAdapter->AddressLength-1?'\n':'-');
              }
              printf("AdapterType:%d\n",pAdapter->Type);
              printf("IPAddress:%s\n",pAdapter->IpAddressList.IpAddress.String);
              printf("IPMask:%s\n",pAdapter->IpAddressList.IpMask.String);
          }
          pAdapter=pAdapter->Next;
      }
    }
    else
    {
        printf("CalltoGetAdaptersInfofailed\n");
    }

    return 0;
}
备注: "iphlpapi.h","Iphlpapi.lib"可能找不到,这个库在VC安装目录可能有,也可能没有,如果没有,可以安装winddk,在winddk相应目录能找到该头文件和库文件,找到之后,将头文件所在目录加入到工程的additional include directories,将库文件所在目录加入到工程的additional library path。

你可能感兴趣的:(c++Builder,工作日志,vc++,网络工程)