1#include"stdafx.h"2#include <Windows.h>3#include <IPHlpApi.h>4#include <iostream>5 #pragmaComment (lib, "IPHlpApi.lib")6 using namespacestd;7 8 BOOL getlocaladaptersinfo ()9 {Ten //Ip_adapter_info Structural Body OnePip_adapter_info Pipadapterinfo =NULL; APipadapterinfo =NewIp_adapter_info; - - //Structure Size theUnsignedLongUlsize =sizeof(ip_adapter_info); - - //Get Adapter Information - intNret = GetAdaptersInfo (pipadapterinfo,&ulsize); + - if(Error_buffer_overflow = =nret) + { A //Insufficient space to delete previously allocated space at delete []pipadapterinfo; - - //Reassign Size -Pipadapterinfo = (pip_adapter_info)NewByte[ulsize]; - - //Get Adapter Information inNret = GetAdaptersInfo (pipadapterinfo,&ulsize); - to //Get failed + if(Error_success! =nret) - { the if(Pipadapterinfo! =NULL) * { $ delete []pipadapterinfo;Panax Notoginseng } - returnFALSE; the } + } A the //MAC Address Information + Charszmacaddr[ -]; - //Assignment Pointers $Pip_adapter_info Piterater =Pipadapterinfo; $ while(Piterater) - { -cout<<"nic Name:"<<pIterater->AdapterName<<Endl; the -cout<<"Network card Description:"<<pIterater->Description<<Endl;Wuyi thesprintf_s (SZMACADDR, -,"%02x-%02x-%02x-%02x-%02x-%02x", -piterater->address[0], Wupiterater->address[1], -piterater->address[2], Aboutpiterater->address[3], $piterater->address[4], -piterater->address[5]); - -cout<<"MAC Address:"<<szMacAddr<<Endl; A +cout<<"IP Address list:"<<endl<<Endl; the - //point to IP Address list $Pip_addr_string pipaddr = &pIterater->ipaddresslist; the while(PIPADDR) the { thecout<<"IP Address:"<<pIpAddr->IpAddress.String<<Endl; thecout<<"Subnet Mask:"<<pIpAddr->IpMask.String<<Endl; - in //point to Gateway list thePip_addr_string pgateawaylist = &pIterater->gatewaylist; the while(pgateawaylist) About { thecout<<"Gateway:"<<pGateAwayList->IpAddress.String<<Endl; the thePgateawaylist = pgateawaylist->Next; + } - thePIPADDR = pipaddr->Next;Bayi } thecout<<endl<<"--------------------------------------------------"<<Endl; the -Piterater = piterater->Next; - } the the //Clean up the if(Pipadapterinfo) the { - delete []pipadapterinfo; the } the the returnTRUE;94 } the the int_tmain (intARGC, _tchar*argv[]) the {98 getlocaladaptersinfo (); About -Cin.Get();101 return 0;102}
Original address: http://blog.csdn.net/sonsie007/article/details/8837572
VC Get system NIC list and IP address information