ipconfig的C语言实现

首先,这篇文章实现了两种方法查询IP,实现截图如下:

ipconfig的C语言实现

第一种方法时调用系统命令,代码如下:

#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;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/57fda19319241a422019eec4f90ad986.html