日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

获取mac地址方法之一 GetAdaptersInfo()

發布時間:2024/6/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取mac地址方法之一 GetAdaptersInfo() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

GetAdaptersInfo -20151116 防止返回的mac出現null

20151116

From:http://blog.csdn.net/weiyumingwww/article/details/17554461

轉載URL:http://www.cnblogs.com/ourran/p/4968502.html

?

測試環境win7 x64 vs2013?

?工程下載地址:鏈接:http://pan.baidu.com/s/1pJAbwgz 密碼:ifst

代碼改為:

mac.h 內容

#ifndef __MAC_MAC_H #define __MAC_MAC_H#include <windows.h> #include <iphlpapi.h> // API GetAdaptersInfo 頭文件 #include <shlwapi.h> // API StrCmpIA 頭文件 #pragma comment(lib, "iphlpapi.lib") #pragma comment(lib, "shlwapi.lib") #include <Strsafe.h> // API StringCbPrintfA 頭文件 #include <shellapi.h> // API lstrcpyA 頭文件#define BUF_SIZE 254 #define MAX_SIZE 254UINT GetAdapterCharacteristics(char* adapter_name); int GetMAC(BYTE mac[BUF_SIZE]); void GetMacAddress( char* mac );#endif

?

mac.c 內容

#include "stdafx.h"
#include "mac.h"

//
// 功能:獲取適配器特性
// 參數:
// adapter_name 適配器 ID
// 返回值:成功則返回由參數指定的適配器的特性標志,是一個 DWORD 值,失敗返回 0
//
UINT GetAdapterCharacteristics(char* adapter_name)
{
if(adapter_name == NULL || adapter_name[0] == 0)
return 0;

HKEY root = NULL;
// 打開存儲適配器信息的注冊表根鍵
if(ERROR_SUCCESS != RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ, &root))
return 0;

DWORD subkeys = 0;
// 獲取該鍵下的子鍵數
if(ERROR_SUCCESS != RegQueryInfoKeyA(root, NULL, NULL, NULL, &subkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
subkeys = 100;

DWORD ret_value = 0;
for(DWORD i = 0; i < subkeys; i++)
{
// 每個適配器用一個子鍵存儲,子鍵名為從 0 開始的 4 位數
char subkey[MAX_SIZE];
memset(subkey, 0, MAX_SIZE);
StringCbPrintfA(subkey, MAX_SIZE, "%04u", i);

// 打開該子鍵
HKEY hKey = NULL;
if(ERROR_SUCCESS != RegOpenKeyExA(root, subkey, 0, KEY_READ, &hKey))
continue;

// 獲取該子鍵對應的適配器 ID,存于 name 中
char name[MAX_PATH];
DWORD type = 0;
DWORD size = MAX_PATH;
if(ERROR_SUCCESS != RegQueryValueExA(hKey, "NetCfgInstanceId", NULL, &type, (LPBYTE)name, &size))
{
RegCloseKey(hKey);
continue;
}

// 對比該適配器 ID 是不是要獲取特性的適配器 ID
if(StrCmpIA(name, adapter_name) != 0)
{
RegCloseKey(hKey);
continue;
}

// 讀取該適配器的特性標志,該標志存儲于值 Characteristics 中
DWORD val = 0;
size = 4;
LSTATUS ls = RegQueryValueExA(hKey, "Characteristics", NULL, &type, (LPBYTE)&val, &size);
RegCloseKey(hKey);

if(ERROR_SUCCESS == ls)
{
ret_value = val;
break;
}
}

RegCloseKey(root);
return ret_value;
}

//
// 功能:獲取 Mac 地址的二進制數據
// 參數:
// mac 用于輸出 Mac 地址的二進制數據的緩沖區指針
// 返回值:成功返回 mac 地址的長度,失敗返回 0,失敗時 mac 中保存一些簡單的錯誤信息,可適當修改,用于調試
//
int GetMAC(BYTE mac[BUF_SIZE])
{
#define NCF_PHYSICAL 0x4
DWORD AdapterInfoSize = 0;
if(ERROR_BUFFER_OVERFLOW != GetAdaptersInfo(NULL, &AdapterInfoSize))
{
StringCbPrintfA((LPSTR)mac, BUF_SIZE, "GetMAC Failed! ErrorCode: %d", GetLastError());
return 0;
}

void* buffer = malloc(AdapterInfoSize);
if(buffer == NULL)
{
lstrcpyA((LPSTR)mac, "GetMAC Failed! Because malloc failed!");
return 0;
}

PIP_ADAPTER_INFO pAdapt = (PIP_ADAPTER_INFO)buffer;
if(ERROR_SUCCESS != GetAdaptersInfo(pAdapt, &AdapterInfoSize))
{
StringCbPrintfA((LPSTR)mac, BUF_SIZE, "GetMAC Failed! ErrorCode: %d", GetLastError());
free(buffer);
return 0;
}

int mac_length = 0;
while(pAdapt)
{
if(pAdapt->AddressLength >= 6 && pAdapt->AddressLength <= 8)
{
memcpy(mac, pAdapt->Address, pAdapt->AddressLength);
mac_length = pAdapt->AddressLength;

UINT flag = GetAdapterCharacteristics(pAdapt->AdapterName);
BOOL is_physical = ((flag & NCF_PHYSICAL) == NCF_PHYSICAL);
if(is_physical)
break;
}
pAdapt = pAdapt->Next;
}
free(buffer);
return mac_length;
}

//
// 功能:獲取 Mac 地址,使用時直接調用此函數即可
// 參數:
// mac 用于存儲 Mac 地址的緩沖區指針
// 返回值:無返回值,函數執行完后會把 Mac 地址以16進制的形式存于參數指定的緩沖區中,若有錯誤,緩沖區中保存的是錯誤信息
//
void GetMacAddress( char* mac )
{
BYTE buf[BUF_SIZE];
memset(buf, 0, BUF_SIZE);

int len = GetMAC(buf);
if(len <= 0)
{
lstrcpyA(mac, (LPCSTR)buf);
return;
}

if(len == 6)
StringCbPrintfA(mac, BUF_SIZE, "%02X-%02X-%02X-%02X-%02X-%02X", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
else
StringCbPrintfA(mac, BUF_SIZE, "%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
}

?

?

?

用方法獲取到的mac在部分機器上會出現獲取到的mac為null;

?

20150923

From:https://msdn.microsoft.com/en-us/library/aa365917

控制臺工程下載地址:?鏈接:http://pan.baidu.com/s/1o6xjJmy 密碼:gyxo

mfc工程下載地址: 鏈接:http://pan.baidu.com/s/1pJORZw3 密碼:djau

GetAdaptersInfo function

The?GetAdaptersInfo?function retrieves adapter information for the local computer.

On Windows?XP and later:??Use the?GetAdaptersAddresses?function instead of?GetAdaptersInfo.

Syntax

?

C++ DWORD GetAdaptersInfo(_Out_???PIP_ADAPTER_INFO pAdapterInfo,_Inout_?PULONG ??????????pOutBufLen );


Examples

This example retrieves the adapter information and prints various properties of each adapter.

C++ #include <winsock2.h> #include <iphlpapi.h> #include <stdio.h> #include <stdlib.h> #pragma comment(lib, "IPHLPAPI.lib")#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))/* Note: could also use malloc() and free() */int __cdecl main() {/* Declare and initialize variables */// It is possible for an adapter to have multiple // IPv4 addresses, gateways, and secondary WINS servers // assigned to the adapter. // // Note that this sample code only prints out the // first entry for the IP address/mask, and gateway, and // the primary and secondary WINS server for each adapter. PIP_ADAPTER_INFO pAdapterInfo;PIP_ADAPTER_INFO pAdapter = NULL;DWORD dwRetVal = 0;UINT i;/* variables used to print DHCP time info */struct tm newtime;char buffer[32];errno_t error;ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO));if (pAdapterInfo == NULL) {printf("Error allocating memory needed to call GetAdaptersinfo\n");return 1;} // Make an initial call to GetAdaptersInfo to get // the necessary size into the ulOutBufLen variableif (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {FREE(pAdapterInfo);pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen);if (pAdapterInfo == NULL) {printf("Error allocating memory needed to call GetAdaptersinfo\n");return 1;}}if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {pAdapter = pAdapterInfo;while (pAdapter) {printf("\tComboIndex: \t%d\n", pAdapter->ComboIndex);printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);printf("\tAdapter Desc: \t%s\n", pAdapter->Description);printf("\tAdapter Addr: \t");for (i = 0; i < pAdapter->AddressLength; i++) {if (i == (pAdapter->AddressLength - 1))printf("%.2X\n", (int) pAdapter->Address[i]);elseprintf("%.2X-", (int) pAdapter->Address[i]);}printf("\tIndex: \t%d\n", pAdapter->Index);printf("\tType: \t");switch (pAdapter->Type) {case MIB_IF_TYPE_OTHER:printf("Other\n");break;case MIB_IF_TYPE_ETHERNET:printf("Ethernet\n");break;case MIB_IF_TYPE_TOKENRING:printf("Token Ring\n");break;case MIB_IF_TYPE_FDDI:printf("FDDI\n");break;case MIB_IF_TYPE_PPP:printf("PPP\n");break;case MIB_IF_TYPE_LOOPBACK:printf("Lookback\n");break;case MIB_IF_TYPE_SLIP:printf("Slip\n");break;default:printf("Unknown type %ld\n", pAdapter->Type);break;}printf("\tIP Address: \t%s\n",pAdapter->IpAddressList.IpAddress.String);printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);printf("\t***\n");if (pAdapter->DhcpEnabled) {printf("\tDHCP Enabled: Yes\n");printf("\t DHCP Server: \t%s\n",pAdapter->DhcpServer.IpAddress.String);printf("\t Lease Obtained: ");/* Display local time */error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseObtained);if (error)printf("Invalid Argument to _localtime32_s\n");else {// Convert to an ASCII representation error = asctime_s(buffer, 32, &newtime);if (error)printf("Invalid Argument to asctime_s\n");else/* asctime_s returns the string terminated by \n\0 */printf("%s", buffer);}printf("\t Lease Expires: ");error = _localtime32_s(&newtime, (__time32_t*) &pAdapter->LeaseExpires);if (error)printf("Invalid Argument to _localtime32_s\n");else {// Convert to an ASCII representation error = asctime_s(buffer, 32, &newtime);if (error)printf("Invalid Argument to asctime_s\n");else/* asctime_s returns the string terminated by \n\0 */printf("%s", buffer);}} elseprintf("\tDHCP Enabled: No\n");if (pAdapter->HaveWins) {printf("\tHave Wins: Yes\n");printf("\t Primary Wins Server: %s\n",pAdapter->PrimaryWinsServer.IpAddress.String);printf("\t Secondary Wins Server: %s\n",pAdapter->SecondaryWinsServer.IpAddress.String);} elseprintf("\tHave Wins: No\n");pAdapter = pAdapter->Next;printf("\n");}} else {printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);}if (pAdapterInfo)FREE(pAdapterInfo);return 0; }

  

?





轉載于:https://www.cnblogs.com/ourran/p/4831571.html

總結

以上是生活随笔為你收集整理的获取mac地址方法之一 GetAdaptersInfo()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。