Hello Mat

 找回密码
 立即注册
查看: 2431|回复: 1

windows操作系统mac地址获取

[复制链接]

1298

主题

1524

帖子

114

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22653
发表于 2022-3-16 21:20:26 | 显示全部楼层 |阅读模式
windows操作系统mac地址获取
  1. // ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <iphlpapi.h>       // API GetAdaptersInfo 头文件
  6. #include <shlwapi.h>        // API StrCmpIA 头文件
  7. #pragma comment(lib, "iphlpapi.lib")
  8. #pragma comment(lib, "shlwapi.lib")
  9. #include <Strsafe.h>        // API StringCbPrintfA 头文件
  10. #include <shellapi.h>       // API lstrcpyA 头文件
  11. #include <iostream>
  12. #define MAX_SIZE 50
  13. #define BUF_SIZE 50

  14. //
  15. // 功能:获取适配器特性
  16. // 参数:
  17. //   adapter_name 适配器 ID
  18. // 返回值:成功则返回由参数指定的适配器的特性标志,是一个 DWORD 值,失败返回 0
  19. //
  20. UINT GetAdapterCharacteristics(char* adapter_name)
  21. {
  22.         if (adapter_name == NULL || adapter_name[0] == 0)
  23.                 return 0;

  24.         HKEY root = NULL;
  25.         // 打开存储适配器信息的注册表根键
  26.         if (ERROR_SUCCESS != RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ, &root))
  27.                 return 0;

  28.         DWORD subkeys = 0;
  29.         // 获取该键下的子键数
  30.         if (ERROR_SUCCESS != RegQueryInfoKeyA(root, NULL, NULL, NULL, &subkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL))
  31.                 subkeys = 100;

  32.         DWORD ret_value = 0;
  33.         for (DWORD i = 0; i < subkeys; i++)
  34.         {
  35.                 // 每个适配器用一个子键存储,子键名为从 0 开始的 4 位数
  36.                 char subkey[MAX_SIZE];
  37.                 memset(subkey, 0, MAX_SIZE);
  38.                 StringCbPrintfA(subkey, MAX_SIZE, "%04u", i);

  39.                 // 打开该子键
  40.                 HKEY hKey = NULL;
  41.                 if (ERROR_SUCCESS != RegOpenKeyExA(root, subkey, 0, KEY_READ, &hKey))
  42.                         continue;

  43.                 // 获取该子键对应的适配器 ID,存于 name 中
  44.                 char name[MAX_PATH];
  45.                 DWORD type = 0;
  46.                 DWORD size = MAX_PATH;
  47.                 if (ERROR_SUCCESS != RegQueryValueExA(hKey, "NetCfgInstanceId", NULL, &type, (LPBYTE)name, &size))
  48.                 {
  49.                         RegCloseKey(hKey);
  50.                         continue;
  51.                 }

  52.                 // 对比该适配器 ID 是不是要获取特性的适配器 ID
  53.                 if (StrCmpIA(name, adapter_name) != 0)
  54.                 {
  55.                         RegCloseKey(hKey);
  56.                         continue;
  57.                 }

  58.                 // 读取该适配器的特性标志,该标志存储于值 Characteristics 中
  59.                 DWORD val = 0;
  60.                 size = 4;
  61.                 LSTATUS ls = RegQueryValueExA(hKey, "Characteristics", NULL, &type, (LPBYTE)&val, &size);
  62.                 RegCloseKey(hKey);

  63.                 if (ERROR_SUCCESS == ls)
  64.                 {
  65.                         ret_value = val;
  66.                         break;
  67.                 }
  68.         }

  69.         RegCloseKey(root);
  70.         return ret_value;
  71. }

  72. //
  73. // 功能:获取 Mac 地址的二进制数据
  74. // 参数:
  75. //   mac 用于输出 Mac 地址的二进制数据的缓冲区指针
  76. // 返回值:成功返回 mac 地址的长度,失败返回 0,失败时 mac 中保存一些简单的错误信息,可适当修改,用于调试
  77. //
  78. int GetMAC(BYTE mac[BUF_SIZE])
  79. {
  80. #define NCF_PHYSICAL 0x4
  81.         DWORD AdapterInfoSize = 0;
  82.         if (ERROR_BUFFER_OVERFLOW != GetAdaptersInfo(NULL, &AdapterInfoSize))
  83.         {
  84.                 StringCbPrintfA((LPSTR)mac, BUF_SIZE, "GetMAC Failed! ErrorCode: %d", GetLastError());
  85.                 return 0;
  86.         }

  87.         void* buffer = malloc(AdapterInfoSize);
  88.         if (buffer == NULL)
  89.         {
  90.                 lstrcpyA((LPSTR)mac, "GetMAC Failed! Because malloc failed!");
  91.                 return 0;
  92.         }

  93.         PIP_ADAPTER_INFO pAdapt = (PIP_ADAPTER_INFO)buffer;
  94.         if (ERROR_SUCCESS != GetAdaptersInfo(pAdapt, &AdapterInfoSize))
  95.         {
  96.                 StringCbPrintfA((LPSTR)mac, BUF_SIZE, "GetMAC Failed! ErrorCode: %d", GetLastError());
  97.                 free(buffer);
  98.                 return 0;
  99.         }

  100.         int mac_length = 0;
  101.         while (pAdapt)
  102.         {
  103.                 if (pAdapt->AddressLength >= 6 && pAdapt->AddressLength <= 8)
  104.                 {
  105.                         memcpy(mac, pAdapt->Address, pAdapt->AddressLength);
  106.                         mac_length = pAdapt->AddressLength;

  107.                         UINT flag = GetAdapterCharacteristics(pAdapt->AdapterName);
  108.                         bool is_physical = ((flag & NCF_PHYSICAL) == NCF_PHYSICAL);
  109.                         if (is_physical)
  110.                                 break;
  111.                 }
  112.                 pAdapt = pAdapt->Next;
  113.         }
  114.         free(buffer);
  115.         return mac_length;
  116. }

  117. //
  118. // 功能:获取 Mac 地址,使用时直接调用此函数即可
  119. // 参数:
  120. //   mac 用于存储 Mac 地址的缓冲区指针
  121. // 返回值:无返回值,函数执行完后会把 Mac 地址以16进制的形式存于参数指定的缓冲区中,若有错误,缓冲区中保存的是错误信息
  122. //
  123. void GetMacAddress(char* mac)
  124. {
  125.         BYTE buf[BUF_SIZE];
  126.         memset(buf, 0, BUF_SIZE);

  127.         int len = GetMAC(buf);
  128.         if (len <= 0)
  129.         {
  130.                 lstrcpyA(mac, (LPCSTR)buf);
  131.                 return;
  132.         }

  133.         if (len == 6)
  134.                 StringCbPrintfA(mac, BUF_SIZE, "%02X-%02X-%02X-%02X-%02X-%02X", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
  135.         else
  136.                 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]);
  137. }

  138. int main() {
  139.         char str[50];
  140.         GetMacAddress(str);
  141.         std::cout << str << std::endl;
  142.         std::cin.get();
  143. }
复制代码
E0-D5-5E-A4-12-F5

参考:
【1】https://blog.csdn.net/m0_37811192/article/details/82216825


算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

1298

主题

1524

帖子

114

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22653
 楼主| 发表于 2022-3-16 21:37:43 | 显示全部楼层
C#获取MAC地址
  1.         public string getMacAddress()
  2.         {
  3.             string macAddress = null;
  4.             try
  5.             {
  6.                 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  7.                 foreach (NetworkInterface adapter in nics)
  8.                 {
  9.                     if (adapter.NetworkInterfaceType.ToString().Equals("Ethernet")) //是以太网卡
  10.                     {
  11.                         string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\" + adapter.Id + "\\Connection";
  12.                         RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
  13.                         if (rk != null)
  14.                         {
  15.                             // 区分 PnpInstanceID     
  16.                             // 如果前面有 PCI 就是本机的真实网卡   
  17.                             // MediaSubType 为 01 则是常见网卡,02为无线网卡。   
  18.                             string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
  19.                             int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
  20.                             if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI") //是物理网卡
  21.                             {
  22.                                 macAddress = adapter.GetPhysicalAddress().ToString();
  23.                                 break;
  24.                             }
  25.                             else if (fMediaSubType == 1) //虚拟网卡
  26.                                 continue;
  27.                             else if (fMediaSubType == 2) //无线网卡(上面判断Ethernet时已经排除了)
  28.                                 continue;
  29.                         }
  30.                     }
  31.                 }
  32.             }
  33.             catch
  34.             {
  35.                 macAddress = null;
  36.             }
  37.             return macAddress;
  38.         }
复制代码

算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-5-20 10:34 , Processed in 0.217956 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表