VS6.0下使用 IP Help API(Iphlpapi.dll)系列函数之GetAdaptersAddresses(GetAdaptersInfo) 获取网卡信息简述

VS6.0下使用IP Help API(Iphlpapi.dll)系列函数之 GetAdaptersAddresses(GetAdaptersInfo)

获取网卡信息简述

第一:用到的微软动态库 Iphlpapi.dll

V5.1.2600.2912  2006-5-19 21:14:08

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

 

The GetAdaptersAddresses functions retrieves the addresses associated with the adapters on the local computer.

第二:应用实例

#include "iptypes.h"  //工程需要加入 IPTypes.h 实体文件 PIP_ADAPTER_INFO用到
typedef DWORD (__stdcall *pGetAdapterInfo)(PIP_ADAPTER_INFO, PULONG);

void GetMac()
{
 //加载DLL
 HMODULE hInst = LoadLibrary("iphlpapi.dll");
 if( NULL == hInst )
    {
  MessageBox(NULL, "无法加载iphlpapi.dll!", "错误", MB_OK|MB_ICONSTOP);
  return;
    }
 //得到函数的地址空间
 pGetAdapterInfo GetAdapterInfo = (pGetAdapterInfo)GetProcAddress(
  hInst,
  "GetAdaptersInfo");
    if( GetAdapterInfo == NULL )
    {
        MessageBox(NULL, "无法定位到函数 GetAdaptersInfo 地址!", "错误", MB_OK|MB_ICONSTOP);
        exit(0);
    }
 
 PIP_ADAPTER_INFO  pAdapter = NULL;
 //PIP_ADAPTER_INFO  AdapterList[20];
 int nIndex = 0;
 ULONG uOutBufLen = 0;//存放网卡信息的缓冲区大小
 
 IP_ADAPTER_INFO * pIPAdapterInfo
  = (PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
 
 //第一次调用 GetAdapterInfo 获取 ulOutBufLen 大小
 if( (GetAdapterInfo(pIPAdapterInfo, &uOutBufLen))
  == ERROR_BUFFER_OVERFLOW )
 {
  //获取需要的缓冲区大小
  free(pIPAdapterInfo);
  pIPAdapterInfo = NULL;
  pIPAdapterInfo = (PIP_ADAPTER_INFO)malloc(uOutBufLen); //分配所需要的内存
 }
 //第二次调用获取网卡信息
 if( (GetAdapterInfo(pIPAdapterInfo, &uOutBufLen)) == ERROR_SUCCESS )
 {
  //保存网卡信息
  //...
  pAdapter = pIPAdapterInfo;
  while( pAdapter )
  {
   TCHAR tchMac[18] = {0};
   sprintf(tchMac, "%02X:%02X:%02X:%02X:%02X:%02X",
    pAdapter->Address[0],
    pAdapter->Address[1],
    pAdapter->Address[2],
    pAdapter->Address[3],
    pAdapter->Address[4],
    pAdapter->Address[5]);
   AfxMessageBox(tchMac);
   pAdapter =  pAdapter->Next;
  }
 }
}


 

你可能感兴趣的:(api,null,buffer,微软,winapi)