设为首页 加入收藏

TOP

ipconfig的C语言实现(一)
2014-11-24 08:24:44 来源: 作者: 【 】 浏览:9
Tags:ipconfig 语言 实现

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



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


#include
#include
using namespace std;
int main(int argc, char *argv[])
{
system("ipconfig");
system("PAUSE");
return EXIT_SUCCESS;
}


下面这种方法是对源码的解析:


#include
#include
#include
#include
#include
#include
#pragma comment(lib,"Ws2_32")
#pragma comment(lib,"Dnsapi")
#define Max 10 ;
//#include
#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 != ER

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言中函数传指针学习小结 下一篇Android中SQLite构造函数参数Cont..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何从内核协议栈到 (2025-12-27 03:19:09)
·什么是网络协议?有哪 (2025-12-27 03:19:06)
·TCP/ IP协议有哪些 (2025-12-27 03:19:03)
·怎样用 Python 写一 (2025-12-27 02:49:19)
·如何学习python数据 (2025-12-27 02:49:16)