如何找到一个mac地址? [英] how to find a mac address?

查看:26
本文介绍了如何找到一个mac地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 C++ 中找到当前运行应用程序的计算机的 mac 地址,然后将该 mac 地址与某个 mac 地址进行比较?

how can i find in c++, the mac address of the computer that an application is currently running on and then compare that mac address with a certain mac address?

所以假设我们要比较的某个 mac 地址是 AB-12-CD-34-EF-56,那么如何找到应用程序运行的 mac 地址上,然后将该 mac 地址与 AB-12-CD-34-EF-56 mac 地址进行比较?做这样的事情最好和最简单的方法是什么?

so lets say that certain mac address that we want to compare with is AB-12-CD-34-EF-56, how is it possible to find the mac address that the application is running on, then compare that mac address with the AB-12-CD-34-EF-56 mac address? what is the best and simplest way of doing something like this?

推荐答案

在 Windows 上,您可以使用该功能 GetAdaptersAddresses() 以获取 IP_ADAPTER_ADDRESSES 结构,其中包含 PhysicalAddress[MAX_ADAPTER_ADDRESS_LENGTH];(这是mac地址).

On Windows you can use the function GetAdaptersAddresses() to get an IP_ADAPTER_ADDRESSES structure which contains PhysicalAddress[MAX_ADAPTER_ADDRESS_LENGTH]; (that's the mac adress).

此函数将引入库Iphlpapi.lib 和头文件 作为依赖项.一个简单的例子,它将打印来自可用适配器的所有 mac 地址:

This function will introduce the library Iphlpapi.lib and the header file <iphlpapi.h> as dependency. A simple example which will print all mac addresses from available adapters:

#include <Winsock2.h>
#include <iphlpapi.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <vector>
#pragma comment(lib, "IPHLPAPI.lib")

int main(int argc, char* argv[]){
  ULONG outBufLen = sizeof(IP_ADAPTER_ADDRESSES);
  GetAdaptersAddresses(0, 0, NULL, NULL, &outBufLen);
  std::vector<uint8_t> bytes(outBufLen, 0);
  PIP_ADAPTER_ADDRESSES pCurrAddresses = (IP_ADAPTER_ADDRESSES *)bytes.data();
  DWORD dwRetVal = GetAdaptersAddresses(0, 0, NULL, pCurrAddresses, &outBufLen);
  if (dwRetVal == NO_ERROR) {
    while (pCurrAddresses != NULL){ 
      for (size_t i = 0; i < (int) pCurrAddresses->PhysicalAddressLength; i++) {
        if (i == (pCurrAddresses->PhysicalAddressLength - 1))
          std::printf("%.2X\n", (int) pCurrAddresses->PhysicalAddress[i]);
        else
          std::printf("%.2X-",(int) pCurrAddresses->PhysicalAddress[i]);
      }
      pCurrAddresses = pCurrAddresses->Next;
    }
  }
  std::system("pause");
  return 0;
}

这篇关于如何找到一个mac地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆