VC编程获取网卡物理地址MAC的几种方法

2012-03-04 17:52:57|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:C/C++, 操作系统, 网络通信|来源:唯设编程网

重要API回顾:

GetAdaptersAddresses

This function retrieves the addresses associated with the adapters on the local computer.

函数原型:


DWORD WINAPI GetAdaptersAddresses(
  ULONG Family,
  DWORD Flags,
  PVOID Reserved,
  PIP_ADAPTER_ADDRESSES pAdapterAddresses,
  PULONG pOutBufLen
);

参数:


Family
[in] The address family of the addresses to retrieve. The following table shows the possible values for this parameter.
Value Description
AF_INET Return only IPv4 addresses.
AF_INET6 Return only IPv6 addresses.
AF_UNSPEC Return addresses from all address families.
Flags
[in] The type of the addresses to retrieve. The following table shows the possible values. This parameter is zero or a combination of these values.
Value Description
GAA_FLAG_INCLUDE_PREFIX Return the IPv6 address prefix.
GAA_FLAG_SKIP_UNICAST Do not return unicast addresses.
GAA_FLAG_SKIP_ANYCAST Do not return anycast addresses.
GAA_FLAG_SKIP_FRIENDLY_NAME Do not return the adapter's friendly name.
GAA_FLAG_SKIP_MULTICAST Do not return multicast addresses.
GAA_FLAG_SKIP_DNS_SERVER Do not return addresses of DNS servers.
Reserved
[in] Reserved.
pAdapterAddresses
[out] Pointer to an IP_ADAPTER_ADDRESSES structure containing information about the adapter addresses.
pOutBufLen
[in, out] Pointer to the size, in bytes, of the buffer indicated by pAdapterAddresses.

补充说明:

The information returned by this function includes information for IPv6 interfaces.

返回值:

Returns NO_ERROR if successful. If the function fails, it returns an error code. For a complete list of error codes, see Error Values or the SDK header file Winerror.h.

基本环境

OS Versions: Windows CE .NET 4.1 and later.
Header: Iphlpapi.h.
Link Library: Iphlpapi.lib.

使用GetAdaptersAddresses获取网卡物理地址MAC虽然方便,但是Win2000不支持GetAdaptersAddress,只支持GetAdaptersInfo函数,同时,GetAdaptersInfo还可以获取网卡的其它相关信息(包括:Adapter Name,Mac,Ip,NetMask,NetGate),下面是使用GetAdaptersInfo获取网卡MAC地址的方法。
示例:

#include<winsock2.h>
#include<Iphlpapi.h>
#include<stdio.h>
#pragma comment(lib,"Iphlpapi.lib")
int main()
{
    PIP_ADAPTER_INFO pAdapterInfo;
    PIP_ADAPTER_INFO pAdapter = NULL;
    DWORD dwRetVal = 0;
    ULONG ulOutBufLen;
    pAdapterInfo=(PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
    ulOutBufLen = sizeof(IP_ADAPTER_INFO);

    // 第一次调用GetAdapterInfo获取ulOutBufLen大小
    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)
        {
            printf("Adapter Name: 	%s
", pAdapter->AdapterName);
            printf("Adapter Desc: 	%s
", pAdapter->Description);
            printf("MAC Addr: 	%02x-%02x-%02x-%02x-%02x-%02x
",
                pAdapter->Address[0],
                pAdapter->Address[1],
                pAdapter->Address[2],
                pAdapter->Address[3],
                pAdapter->Address[4],
                pAdapter->Address[5]);
            printf("IP Address: 	%s
", pAdapter->IpAddressList.IpAddress.String);
            printf("IP Mask: 	%s
", pAdapter->IpAddressList.IpMask.String);    
            printf("Gateway: 	%s
", pAdapter->GatewayList.IpAddress.String);
            pAdapter = pAdapter->Next;
        }
    }
    else
    {
        printf("Call to GetAdaptersInfo failed.
");
    }
}
发表评论0条 】
网友评论(共?条评论)..
VC编程获取网卡物理地址MAC的几种方法