获取网络适配器信息

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#include <iphlpapi.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"iphlpapi")
#pragma comment(lib,"ws2_32")
using namespace std;
int main(int argc, char* argv[])
{
	//init socket
	WSADATA stData = { 0 };
	WORD dVer = MAKEWORD(2, 2);
	WSAStartup(dVer, &stData);

	//net apater info
	PIP_ADAPTER_INFO pAdapter = nullptr;
	PIP_ADAPTER_INFO pCurrent = nullptr;
	ULONG ulLen = 0L;
	DWORD dwIndex = 1;
	GetAdaptersInfo(pAdapter, &ulLen);
	pAdapter = (PIP_ADAPTER_INFO)
		VirtualAlloc(NULL, ulLen, MEM_COMMIT, PAGE_READWRITE);

	if (GetAdaptersInfo(pAdapter, &ulLen) != ERROR_SUCCESS)
	{
		VirtualFree(pAdapter, NULL, MEM_RELEASE);
		cout << "Get adapter info fail" << endl;
		return -1;
	}
	else
	{
		pCurrent = pAdapter;
	}

	in_addr stAddr = { 0 };
	while (pCurrent)
	{
		cout << "************************************************************" << endl;
		cout << "current adapter " << dwIndex << endl;
		cout << "adapter name " << pCurrent->AdapterName << endl;
		cout << "adapter description " << pCurrent->Description << endl;

		//使用C++的cout不懂得如何描述了直接用printf了。。。。
		printf("max address %2x-%2x-%2x-%2x-%2x-%2x\n",
			pCurrent->Address[0],
			pCurrent->Address[1],
			pCurrent->Address[2],
			pCurrent->Address[3],
			pCurrent->Address[4],
			pCurrent->Address[5]);
		//cout << "mac address "
		//	<< hex
		//	<< pCurrent->Address[0]
		//	<< "-"
		//	<< hex
		//	<< pCurrent->Address[1]
		//	<< "-"
		//	<< hex
		//	<< pCurrent->Address[2]
		//	<< "-"
		//	<< hex
		//	<< pCurrent->Address[3]
		//	<< "-"
		//	<< hex
		//	<< pCurrent->Address[4]
		//	<< "-"
		//	<< hex
		//	<< pCurrent->Address[5]
		//	<< endl;
		cout << "adapter type " << pCurrent->Type << endl;

		stAddr.S_un.S_addr = inet_addr(pCurrent->IpAddressList.IpAddress.String);
		cout << "local ip " << inet_ntoa(stAddr) << endl;

		stAddr.S_un.S_addr = inet_addr(pCurrent->GatewayList.IpAddress.String);
		cout << "gateway ip " << inet_ntoa(stAddr) << endl;

		stAddr.S_un.S_addr = inet_addr(pCurrent->IpAddressList.IpMask.String);
		cout << "mask ip " << inet_ntoa(stAddr) << endl;

		cout << "\n*********************************************************" << endl;
		cout << endl;
		dwIndex++;
		pCurrent = pCurrent->Next;
	}

	VirtualFree(pAdapter, NULL, MEM_RELEASE);
	//clear socket
	system("pause");
	WSACleanup();
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值