首先,这篇文章实现了两种方法查询IP,实现截图如下:
第一种方法时调用系统命令,代码如下:
#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { system("ipconfig"); system("PAUSE"); return EXIT_SUCCESS; }
下面这种方法是对源码的解析:
#include <windows.h> #include <IPHlpApi.h> #include <stdio.h> #include <time.h> #include<Ipexport.h> #include<Windns.h> #pragma comment(lib,"Ws2_32") #pragma comment(lib,"Dnsapi") #define Max 10 ; //#include <winsock2.h> #pragma comment(lib,"IPHlpApi") void DoUsage(bool); void DoRelease(bool);// realized void DoRenew(bool);//realized void DoDisplayDns(bool); void DoFlushDns(bool); void DoAll(void); void GetInterfaceInfo(void); //LPCTSTR GetNodeTypeName(UINT nodeType); //LPCTSTR GetInterfaceTypeName(UINT InterfaceType); IP_ADAPTER_INDEX_MAP adapterInfo; PIP_INTERFACE_INFO pInfo; DWORD checkReturn; void main(int argc,char **argv) { bool doUsage=false; bool doAll=false; bool doRelease=false; bool doRenew=false; bool doDisplayDns=false; bool doFlushDns=false; char *argument; //store the command line code argument=&argv[1][1]; if((argc>1)&&(argv[1][0]=='/')) { if(!strcmp(argument,"?")) { doUsage=true; DoUsage(doUsage); } if(!strcmp(argument,"all")) { doUsage=true; DoAll(); } if(!strcmp(argument,"release")) { doRelease=true; DoRelease(doRelease); } if(!strcmp(argument,"renew")) { doRenew=true; DoRenew(doRenew); } if(!strcmp(argument,"displaydns")) { doDisplayDns=true; DoDisplayDns(doDisplayDns); } if(!strcmp(argument,"flushdns")) { doFlushDns=true; DoFlushDns(doFlushDns); } } else printf("error command code!"); printf("......................................"); } /* get the using adapter information*/ void GetInterfaceInfo() { ULONG outBufferLen=0; pInfo=(IP_INTERFACE_INFO *)malloc(sizeof(IP_INTERFACE_INFO)); /* Make the first call to buffer information*/ if((checkReturn=GetInterfaceInfo(pInfo,&outBufferLen))==ERROR_INSUFFICIENT_BUFFER) { free(pInfo); pInfo=(IP_INTERFACE_INFO *)malloc(outBufferLen); } /* Make the second call of GetInterfaceInfo() to get the actual data */ if((checkReturn=GetInterfaceInfo(pInfo,&outBufferLen))==NO_ERROR) { adapterInfo=pInfo->Adapter[0];//关键此处返回IP_ADAPTER_INDEX_MAP 用于IP更新 printf("the dapter name is: %d ",pInfo->Adapter[0].Name); } else { printf("Get the interface information occer error!"); } } /* release IP address */ void DoRelease(bool doRelease) { if(doRelease) { GetInterfaceInfo(); /**the specific action to config/release*/ if((checkReturn=IpReleaseAddress(&adapterInfo))==NO_ERROR) { printf("the action of release IP work!"); } else printf("An error occured while releasing interface of %s",pInfo->Adapter[0].Name); } else { printf("illegal action"); } } /** Do renew IPAddree */ void DoRenew(bool doRenew) { if(doRenew) { GetInterfaceInfo(); if((checkReturn=IpRenewAddress(&adapterInfo))==NO_ERROR) { printf("the action of release IP work!"); } else printf("An error occured while releasing interface of %s",pInfo->Adapter[0].Name); } else { printf("illegal action"); } } /* display all information ipconfig/all */ void DoAll(void) { DWORD returnCheckError; PFIXED_INFO pFixedInfo; DWORD FixedInfoSize = 0; PIP_ADDR_STRING pAddrStr; PIP_ADAPTER_INFO pAdapt,pAdapterInfo; if ((returnCheckError = GetNetworkParams(NULL, &FixedInfoSize)) != 0) { if (returnCheckError != ERROR_BUFFER_OVERFLOW) { printf("GetNetworkParams sizing failed with error %d ", returnCheckError); return; } } // Allocate memory from sizing information if ((pFixedInfo = (PFIXED_INFO) malloc( FixedInfoSize)) == NULL) { printf("Memory allocation error "); return; } if ((returnCheckError = GetNetworkParams(pFixedInfo, &FixedInfoSize)) == 0) { printf(" Host Name . . . . . . . . . : %s ", pFixedInfo->HostName); printf(" DNS Servers . . . . . . . . : %s ", pFixedInfo->DnsServerList.IpAddress.String); pAddrStr = pFixedInfo->DnsServerList.Next; while(pAddrStr) { printf("%52s ", pAddrStr->IpAddress.String); pAddrStr = pAddrStr->Next; } printf(" Node Type . . . . . . . . . : "); switch (pFixedInfo->NodeType) { case 1: printf("%s ", "Broadcast"); break; case 2: printf("%s ", "Peer to peer"); break; case 4: printf("%s ", "Mixed"); break; case 8: printf("%s ", "Hybrid"); break; default: printf(" "); } printf(" NetBIOS Scope ID. . . . . . : %s ", pFixedInfo->ScopeId); printf(" IP Routing Enabled. . . . . : %s ", (pFixedInfo->EnableRouting ? "yes" : "no")); printf(" WINS Proxy Enabled. . . . . : %s ", (pFixedInfo->EnableProxy ? "yes" : "no")); printf(" NetBIOS Resolution Uses DNS : %s ", (pFixedInfo->EnableDns ? "yes" : "no")); } else { printf("GetNetworkParams failed with error %d ", returnCheckError); return; } // // Enumerate all of the adapter specific information using the IP_ADAPTER_INFO structure. // Note: IP_ADAPTER_INFO contains a linked list of adapter entries. ULONG AdapterInfoSize = 0; if ((returnCheckError = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0) { if (returnCheckError != ERROR_BUFFER_OVERFLOW) { printf("GetAdaptersInfo sizing failed with error %d ", returnCheckError); return; } } // Allocate memory from sizing information if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL) { printf("Memory allocation error "); return; } // Get actual adapter information if ((returnCheckError = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0) { printf("GetAdaptersInfo failed with error %d ", returnCheckError); return; } pAdapt = pAdapterInfo; while (pAdapt) { switch (pAdapt->Type) { case MIB_IF_TYPE_ETHERNET: printf(" Ethernet adapter "); break; case MIB_IF_TYPE_TOKENRING: printf(" Token Ring adapter "); break; case MIB_IF_TYPE_FDDI: printf(" FDDI adapter "); break; case MIB_IF_TYPE_PPP: printf(" PPP adapter "); break; case MIB_IF_TYPE_LOOPBACK: printf(" Loopback adapter "); break; case MIB_IF_TYPE_SLIP: printf(" Slip adapter "); break; case MIB_IF_TYPE_OTHER: default: printf(" Other adapter "); } printf("%s: ", pAdapt->AdapterName); printf(" Description . . . . . . . . : %s ", pAdapt->Description); printf(" Physical Address. . . . . . : "); for (UINT i=0; i<pAdapt->AddressLength; i++) { if (i == (pAdapt->AddressLength - 1)) printf("%.2X ",(int)pAdapt->Address[i]); else printf("%.2X-",(int)pAdapt->Address[i]); } printf(" DHCP Enabled. . . . . . . . : %s ", (pAdapt->DhcpEnabled ? "yes" : "no")); pAddrStr = &(pAdapt->IpAddressList); while(pAddrStr) { printf(" IP Address. . . . . . . . . : %s ", pAddrStr->IpAddress.String); printf(" Subnet Mask . . . . . . . . : %s ", pAddrStr->IpMask.String); pAddrStr = pAddrStr->Next; } printf(" Default Gateway . . . . . . : %s ", pAdapt->GatewayList.IpAddress.String); pAddrStr = pAdapt->GatewayList.Next; while(pAddrStr) { printf("%52s ", pAddrStr->IpAddress.String); pAddrStr = pAddrStr->Next; } printf(" DHCP Server . . . . . . . . : %s ", pAdapt->DhcpServer.IpAddress.String); printf(" Primary WINS Server . . . . : %s ", pAdapt->PrimaryWinsServer.IpAddress.String); printf(" Secondary WINS Server . . . : %s ", pAdapt->SecondaryWinsServer.IpAddress.String); struct tm *newtime; // Display coordinated universal time - GMT newtime = gmtime(&pAdapt->LeaseObtained); printf( " Lease Obtained. . . . . . . : %s", asctime( newtime ) ); newtime = gmtime(&pAdapt->LeaseExpires); printf( " Lease Expires . . . . . . . : %s", asctime( newtime ) ); pAdapt = pAdapt->Next; } } void DoUsage(bool doUsage) { if(doUsage) { printf(" USAGE: " " ipconfig [/? | /all | /renew [adapter] | /release [adapter] | " " /flushdns | /displaydns | /registerdns | " " /showclassid adapter | " " /setclassid adapter [classid] ] " " " "where " " adapter Connection name " " (wildcard characters * and ? allowed, see examples) " " " " Options: " " /? Display this help message " " /all Display full configuration information. " " /release Release the IP address for the specified adapter. " " /renew Renew the IP address for the specified adapter. " " /flushdns Purges the DNS Resolver cache. " " /registerdns Refreshes all DHCP leases and re-registers DNS names. " " /displaydns Display the contents of the DNS Resolver Cache. " " /showclassid Displays all the dhcp class IDs allowed for adapter. " " /setclassid Modifies the dhcp class id. " " " "The default is to display only the IP address, subnet mask and " "default gateway for each adapter bound to TCP/IP. " " " "For Release and Renew, if no adapter name is specified, then the IP address " "leases for all adapters bound to TCP/IP will be released or renewed. " " " "For Setclassid, if no ClassId is specified, then the ClassId is removed. " " " "Examples: " " > ipconfig ... Show information. " " > ipconfig /all ... Show detailed information " " > ipconfig /renew ... renew all adapters " " > ipconfig /renew EL* ... renew any connection that has its " " name starting with EL " " > ipconfig /release *Con* ... release all matching connections, " " eg. "Local Area Connection 1" or " " "Local Area Connection 2" "); } else { printf("ilegal code!"); } } void DoDisplayDns(bool doDisplayDns) { if(doDisplayDns) { char hostname[256]; int iRet = 0; WSADATA wsaData; DNS_STATUS status; //Return value of DnsQuery_A() function. PDNS_RECORD pDnsRecord; int assignBuffLenght; assignBuffLenght=(sizeof(DNS_RECORD))*Max; pDnsRecord=(DNS_RECORD*)malloc(assignBuffLenght); memset(pDnsRecord,0,assignBuffLenght); if (WSAStartup(MAKEWORD(2,1),&wsaData)) //调用Windows Sockets DLL { printf("Winsock无法初始化! "); WSACleanup(); } memset(hostname, 0, 256); iRet = gethostname(hostname, sizeof(hostname)); if(iRet != 0 ) { printf( "get hostname error:%d ", iRet); } printf("%s ", hostname); LPSTR pOwnerName=hostname; // Calling function DnsQuery to query Host or PTR records status = DnsQuery(pOwnerName, //Pointer to OwnerName. DNS_TYPE_A, //Type of the record to be queried. DNS_QUERY_STANDARD, // Bypasses the resolver cache on the lookup. NULL, //Contains DNS server IP address. &pDnsRecord, //Resource record that contains the response. NULL); //Reserved for future use. // printf("The host name is %s ",(pDnsRecord->Data.PTR.pNameHost)); while(pDnsRecord){ printf("The host name is %s ",pDnsRecord->pName); printf("the type is %d ",pDnsRecord->wType); printf("the lenght of data is %d ", pDnsRecord->wDataLength); printf(" the time to live is %d ", pDnsRecord-> dwTtl); printf("the lenght of data id %d ", pDnsRecord-> dwReserved); pDnsRecord=pDnsRecord->pNext; } printf("dnsCache print over"); } else { printf("ilegal code"); } } void DoFlushDns(bool){ printf("relizing config/flushdns!"); }
转载自:http://blog.csdn.net/dlutbrucezhang/article/details/8577712