对书上的代码进行的改写,使用C++ 《Windows网络编程(第2版)》
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include<iphlpapi.h>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib,"iphlpapi.lib")
#include<iostream>
#include<sstream>
using namespace std;
void getInfo()
{
IP_ADAPTER_INFO* pAdapterInfo;
ULONG ulOutBufLen;
DWORD dwRetVal;
PIP_ADAPTER_INFO pAdapter;
pAdapterInfo = NULL;
ulOutBufLen = 0;
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
pAdapterInfo = new IP_ADAPTER_INFO[ulOutBufLen];
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) != ERROR_SUCCESS)
{
cout<<"GetAdaptersInfo Error! :"<< dwRetVal<<endl;
}
pAdapter = pAdapterInfo;
while (pAdapter)
{
cout << endl;
cout << "网络适配器名:\t" << pAdapter->AdapterName << endl;
cout << "\n 网络适配器描述:\t" << pAdapter->Description << endl;
printf(" MAC地址: \t\t");
for (unsigned int i = 0; i < pAdapter->AddressLength; i++)
{
if (i == (pAdapter->AddressLength - 1))
printf("%.2X\n", (int)pAdapter->Address[i]);
else
printf("%.2X-", (int)pAdapter->Address[i]);
}
printf(" IP地址: \t\t%s\n", pAdapter->IpAddressList.IpAddress.String);
printf(" 子网掩码: \t\t%s\n", pAdapter->IpAddressList.IpMask.String);
printf(" 网关: \t\t%s\n", pAdapter->GatewayList.IpAddress.String);
if (pAdapter->DhcpEnabled)
{
printf(" 启用DHCP: \t\t是\n");
printf(" DHCP服务器: \t\t%s\n", pAdapter->DhcpServer.IpAddress.String);
}
else
{
printf(" 启用DHCP: \t\t否\n");
}
pAdapter = pAdapter->Next;
}
if (pAdapterInfo)
delete pAdapter;
cout << endl;
}
int main()
{
getInfo();
return 0;
}
|