获取远程网卡MAC地址(VC++)
View Code
1 ×××××××××××××××××××××××××× 2 获取远程网卡MAC地址。 3 ×××××××××××××××××××××××××× 4 5 6 首先在头文件定义中加入#include "nb30.h" 7 #pragma comment(lib,"netapi32.lib") 8 typedef struct _ASTAT_ 9 { 10 ADAPTER_STATUS adapt; 11 NAME_BUFFER NameBuff[30]; 12 } ASTAT, * PASTAT; 13 14 就可以这样调用来获取远程网卡MAC地址了: 15 CString GetMacAddress(CString sNetBiosName) 16 { 17 ASTAT Adapter; 18 19 NCB ncb; 20 UCHAR uRetCode; 21 22 memset(&ncb, 0, sizeof(ncb)); 23 ncb.ncb_command = NCBRESET; 24 ncb.ncb_lana_num = 0; 25 26 uRetCode = Netbios(&ncb); 27 28 memset(&ncb, 0, sizeof(ncb)); 29 ncb.ncb_command = NCBASTAT; 30 ncb.ncb_lana_num = 0; 31 32 sNetBiosName.MakeUpper(); 33 34 FillMemory(ncb.ncb_callname, NCBNAMSZ - 1, 0x20); 35 36 strcpy((char *)ncb.ncb_callname, (LPCTSTR) sNetBiosName); 37 38 ncb.ncb_callname[sNetBiosName.GetLength()] = 0x20; 39 ncb.ncb_callname[NCBNAMSZ] = 0x0; 40 41 ncb.ncb_buffer = (unsigned char *) &Adapter; 42 ncb.ncb_length = sizeof(Adapter); 43 44 uRetCode = Netbios(&ncb); 45 46 CString sMacAddress; 47 48 if (uRetCode == 0) 49 { 50 sMacAddress.Format(_T("%02x%02x%02x%02x%02x%02x"), 51 Adapter.adapt.adapter_address[0], 52 Adapter.adapt.adapter_address[1], 53 Adapter.adapt.adapter_address[2], 54 Adapter.adapt.adapter_address[3], 55 Adapter.adapt.adapter_address[4], 56 Adapter.adapt.adapter_address[5]); 57 } 58 return sMacAddress; 59 } 60 61 62 63 -------------------------------------------------------------------------------- 64 65 66 ×××××××××××××××××××××××××××××××××××××××× 67 用NetBIOS的API获得网卡MAC地址 68 ×××××××××××××××××××××××××××××××××××××××× 69 70 #include "Nb30.h" 71 #pragma comment (lib,"netapi32.lib") 72 73 74 typedef struct tagMAC_ADDRESS 75 { 76 BYTE b1,b2,b3,b4,b5,b6; 77 }MAC_ADDRESS,*LPMAC_ADDRESS; 78 79 typedef struct tagASTAT 80 { 81 ADAPTER_STATUS adapt; 82 NAME_BUFFER NameBuff [30]; 83 }ASTAT,*LPASTAT; 84 85 UCHAR GetAddressByIndex(int lana_num, ASTAT &Adapter) 86 { 87 NCB ncb; 88 UCHAR uRetCode; 89 memset(&ncb, 0, sizeof(ncb) ); 90 ncb.ncb_command = NCBRESET; 91 ncb.ncb_lana_num = lana_num; 92 //指定网卡号,首先对选定的网卡发送一个NCBRESET命令,以便进行初始化 93 uRetCode = Netbios(&ncb ); 94 memset(&ncb, 0, sizeof(ncb) ); 95 ncb.ncb_command = NCBASTAT; 96 ncb.ncb_lana_num = lana_num; //指定网卡号 97 strcpy((char *)ncb.ncb_callname,"* " ); 98 ncb.ncb_buffer = (unsigned char *)&Adapter; 99 //指定返回的信息存放的变量 100 ncb.ncb_length = sizeof(Adapter); 101 //接着,可以发送NCBASTAT命令以获取网卡的信息 102 uRetCode = Netbios(&ncb ); 103 return uRetCode; 104 } 105 106 int GetMAC(LPMAC_ADDRESS pMacAddr) 107 { 108 NCB ncb; 109 UCHAR uRetCode; 110 int num = 0; 111 LANA_ENUM lana_enum; 112 memset(&ncb, 0, sizeof(ncb) ); 113 ncb.ncb_command = NCBENUM; 114 ncb.ncb_buffer = (unsigned char *)&lana_enum; 115 ncb.ncb_length = sizeof(lana_enum); 116 ////向网卡发送NCBENUM命令,以获取当前机器的网卡信息,如有多少个网卡 117 ////每张网卡的编号等 118 uRetCode = Netbios(&ncb); 119 if (uRetCode == 0) 120 { 121 num = lana_enum.length; 122 //对每一张网卡,以其网卡编号为输入编号,获取其MAC地址 123 for (int i = 0; i < num; i++) 124 { 125 ASTAT Adapter; 126 if(GetAddressByIndex(lana_enum.lana[i],Adapter) == 0) 127 { 128 pMacAddr[i].b1 = Adapter.adapt.adapter_address[0]; 129 pMacAddr[i].b2 = Adapter.adapt.adapter_address[1]; 130 pMacAddr[i].b3 = Adapter.adapt.adapter_address[2]; 131 pMacAddr[i].b4 = Adapter.adapt.adapter_address[3]; 132 pMacAddr[i].b5 = Adapter.adapt.adapter_address[4]; 133 pMacAddr[i].b6 = Adapter.adapt.adapter_address[5]; 134 } 135 } 136 } 137 return num; 138 } 139 140 ======= 调用: 141 142 MAC_ADDRESS m_MacAddr[10]; // 比如最多10个网卡 143 int n = GetMAC(m_MacAddr); // 获得网卡数量 144 145 146 TCHAR szAddr[128]; 147 wsprintf(szAddr,_T("%02x-%02x-%02x-%02x-%02x-%02x"), 148 m_MacAddr[0].b1,m_MacAddr[0].b2, 149 m_MacAddr[0].b3,m_MacAddr[0].b4, 150 m_MacAddr[0].b5,m_MacAddr[0].b6); 151 _tcsupr(szAddr); 152 153 // 这样就能获得诸如 00-E0-aa-aa-aa-aa 这样的MAC地址字符串 154 155 156 157 -------------------------------------------------------------------------------- 158 159 160 ×××××××××××××××××××××××××××××××××××××××× 161 用IP Helper API来获得网卡地址 162 ×××××××××××××××××××××××××××××××××××××××× 163 164 呵呵,最常用的方法放在了最后 165 166 用 GetAdaptersInfo函数 167 168 这里获得了一个网卡的大部分信息,朋友们可酌情选用^_^ 169 170 #include <Iphlpapi.h> 171 #pragma comment(lib, "Iphlpapi.lib") 172 173 typedef struct tagAdapterInfo 174 { 175 char szDeviceName[128]; // 名字 176 char szIPAddrStr[16]; // IP 177 char szHWAddrStr[18]; // MAC 178 DWORD dwIndex; // 编号 179 }INFO_ADAPTER, *PINFO_ADAPTER; 180 181 INFO_ADAPTER AdapterList[ 10]; // 网卡列表,比如十个 182 /**************************************************************************** 183 * Name & Params:: 184 * formatMACToStr 185 * ( 186 * LPSTR lpHWAddrStr : 显示出来的加 "-"的 mac字符串 187 * unsigned char *HWAddr : 传入的MAC字符串 188 * ) 189 * Purpose: 190 * 将用户输入的MAC地址字符转成相应格式 191 ****************************************************************************/ 192 void formatMACToStr(LPSTR lpHWAddrStr,const unsigned char *HWAddr) 193 { 194 int i; 195 short temp; 196 char szStr[3]; 197 198 strcpy(lpHWAddrStr, ""); 199 for (i=0; i<6; ++i) 200 { 201 temp = (short)(*(HWAddr + i)); 202 _itoa(temp, szStr, 16); 203 if (strlen(szStr) == 1) strcat(lpHWAddrStr, "0"); 204 strcat(lpHWAddrStr, szStr); 205 if (i<5) strcat(lpHWAddrStr, "-"); // 加上 - 206 } 207 } 208 209 // 填充结构 210 void GetAdapterInfo() 211 { 212 char tempChar; 213 ULONG uListSize=1; 214 PIP_ADAPTER_INFO pAdapter; // 定义PIP_ADAPTER_INFO结构存储网卡信息 215 int nAdapterIndex = 0; 216 217 DWORD dwRet = GetAdaptersInfo((PIP_ADAPTER_INFO)&tempChar, 218 &uListSize); // 关键函数 219 220 if (dwRet == ERROR_BUFFER_OVERFLOW) 221 { 222 PIP_ADAPTER_INFO pAdapterListBuffer = 223 (PIP_ADAPTER_INFO)new(char[uListSize]); 224 dwRet = GetAdaptersInfo(pAdapterListBuffer, &uListSize); 225 if (dwRet == ERROR_SUCCESS) 226 { 227 pAdapter = pAdapterListBuffer; 228 while (pAdapter) // 枚举网卡 229 { 230 CString strTemp = pAdapter->AdapterName; // 网卡名字 231 strTemp = "//Device//NPF_" + strTemp; // 加上前缀 232 strcpy(AdapterList[nAdapterIndex].szDeviceName,strTemp); 233 234 strcpy(AdapterList[nAdapterIndex].szIPAddrStr, 235 pAdapter->IpAddressList.IpAddress.String );// IP 236 237 formatMACToStr( AdapterList[nAdapterIndex].szHWAddrStr, 238 pAdapter->Address ); // MAC!!!!!!!!!!!!!!!!! 239 240 AdapterList[nAdapterIndex].dwIndex = pAdapter->Index; // 编号 241 242 pAdapter = pAdapter->Next; 243 244 nAdapterIndex ++; 245 } 246 delete pAdapterListBuffer; 247 } 248 } 249 }