ipconfig的C语言实现

首先,这篇文章实现了两种方法查询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->
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值