最近,搞了一个Windows工具,需要获取网卡mac, 这个太熟悉了,之前也经常玩,搞起:
- #include <stdio.h>
- #include <winsock2.h>
- #include <iphlpapi.h>
- #pragma comment(lib, "iphlpapi.lib")
-
- int main()
- {
- int i = 0;
- int ret = 0;
- unsigned long size = 0;
-
- PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
- ret = GetAdaptersInfo(pIpAdapterInfo,&size);
-
- if (ERROR_BUFFER_OVERFLOW == ret)
- {
- delete pIpAdapterInfo;
- pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[size];
- ret = GetAdaptersInfo(pIpAdapterInfo, &size);
- }
-
- if (ERROR_SUCCESS == ret)
- {
- while (pIpAdapterInfo) // 每个网卡对应一个链表结点
- {
- for (i = 0; i < pIpAdapterInfo->AddressLength; i++)
- {
- if (i < pIpAdapterInfo->AddressLength - 1)
- {
- printf("%02X-", pIpAdapterInfo->Address[i]);
- }
- else
- {
- printf("%02X\n", pIpAdapterInfo->Address[i]);
- }
- }
-
- pIpAdapterInfo = pIpAdapterInfo->Next;
- }
- }
-
- if (pIpAdapterInfo)
- {
- delete pIpAdapterInfo;
- pIpAdapterInfo = NULL;
- }
-
- return 0;
- }
测试了一下,OK.