1 #include <windows.h>
 2 #include <stdio.h>
 3 #include "Iphlpapi.h" //包含对IP帮助函数的定义
 4 #pragma comment(lib,"Iphlpapi.lib")
 5 #pragma comment(lib,"WS2_32.lib")//全局数据
 6 u_char g_ucLocalMac[6];
 7 DWORD g_dwGatewayIP;
 8 DWORD g_dwLocalIP;
 9 DWORD g_dwMask;
10 BOOL GetGlobalData()
11 {
12     PIP_ADAPTER_INFO pAdapterInfo = NULL;
13     ULONG ulLen = 0;
14     //为适配器结构申请内存
15     ::GetAdaptersInfo(pAdapterInfo,&ulLen);
16     pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR,ulLen);
17     //取得本地适配器结构信息
18     if(::GetAdaptersInfo(pAdapterInfo,&ulLen) == ERROR_SUCCESS)
19     {
20         if(pAdapterInfo != NULL)
21         {
22             memcpy(g_ucLocalMac,pAdapterInfo->Address,6);
23             g_dwGatewayIP = ::inet_addr(pAdapterInfo->GatewayList.IpAddress.String);
24             g_dwLocalIP = ::inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);
25             g_dwMask = ::inet_addr(pAdapterInfo->IpAddressList.IpMask.String);
26         }
27     }
28
29     printf("\n ---------------------本地主机信息---------------------\n\n");
30     in_addr in;
31     in.S_un.S_addr = g_dwLocalIP;
32     printf("    IP Address : %s\n",::inet_ntoa(in));
33
34     in.S_un.S_addr = g_dwMask;
35     printf("    Subnet Mask : %s\n",::inet_ntoa(in));
36
37     in.S_un.S_addr = g_dwGatewayIP;
38     printf("    Default Gateway:%s\n",::inet_ntoa(in));
39
40     u_char *p = g_ucLocalMac;
41     printf("    MAC Address:%02X-%02X-%02X-%02X-%02X-%02X\n",p[0],p[1],p[2],p[3],p[4],p[5]);
42
43     printf("\n\n");
44
45     return TRUE;
46 }

主要使用帮助函数GetAdapterInfo即可。

VS成功实例:

 1 // getMac.cpp : 定义控制台应用程序的入口点。
 2 //
 3
 4 #include "stdafx.h"
 5 #include <windows.h>
 6  #include <stdio.h>
 7  #include "Iphlpapi.h" //包含对IP帮助函数的定义
 8  #pragma comment(lib,"Iphlpapi.lib")
 9  #pragma comment(lib,"WS2_32.lib")//全局数据
10  u_char g_ucLocalMac[6];
11  DWORD g_dwGatewayIP;
12  DWORD g_dwLocalIP;
13  DWORD g_dwMask;
14  BOOL GetGlobalData()
15  {
16      PIP_ADAPTER_INFO pAdapterInfo = NULL;
17      ULONG ulLen = 0;
18      //为适配器结构申请内存
19      ::GetAdaptersInfo(pAdapterInfo,&ulLen);
20      pAdapterInfo = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR,ulLen);
21      //取得本地适配器结构信息
22      if(::GetAdaptersInfo(pAdapterInfo,&ulLen) == ERROR_SUCCESS)
23      {
24          if(pAdapterInfo != NULL)
25          {
26              memcpy(g_ucLocalMac,pAdapterInfo->Address,6);
27              g_dwGatewayIP = ::inet_addr(pAdapterInfo->GatewayList.IpAddress.String);
28              g_dwLocalIP = ::inet_addr(pAdapterInfo->IpAddressList.IpAddress.String);
29              g_dwMask = ::inet_addr(pAdapterInfo->IpAddressList.IpMask.String);
30          }
31      }
32
33      printf("\n ---------------------本地主机信息---------------------\n\n");
34      in_addr in;
35      in.S_un.S_addr = g_dwLocalIP;
36      printf("    IP Address : %s\n",::inet_ntoa(in));
37
38      in.S_un.S_addr = g_dwMask;
39      printf("    Subnet Mask : %s\n",::inet_ntoa(in));
40
41      in.S_un.S_addr = g_dwGatewayIP;
42      printf("    Default Gateway:%s\n",::inet_ntoa(in));
43
44      u_char *p = g_ucLocalMac;
45      printf("    MAC Address:%02X-%02X-%02X-%02X-%02X-%02X\n",p[0],p[1],p[2],p[3],p[4],p[5]);
46
47      printf("\n\n");
48
49      return TRUE;
50  }
51
52 int _tmain(int argc, _TCHAR* argv[])
53 {
54     GetGlobalData();
55     return 0;
56 }

结果如下:

获取本机IP MAC地址-LMLPHP

04-07 15:31