ipconfig源码2

#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include <time.h>
#include <iptypes.h>
#include <ipexport.h>
#include <iphlpapi.h>
#pragma comment(lib,"iphlpapi")
/* imported from iphlpapi.dll
GetAdapterOrderMap
GetInterfaceInfo
GetIpStatsFromStack
NhGetInterfaceNameFromGuid
NhpAllocateAndGetInterfaceInfoFromStack
*/
static TCHAR* GetNodeTypeName(int nNodeType)
{
	switch (nNodeType) {
	case 0:  return _T("zero");
	case 1:  return _T("one");
	case 2:  return _T("two");
	case 3:  return _T("three");
	case 4:  return _T("mixed");
	default: return _T("unknown");
	}
}
static void ShowNetworkFixedInfo()
{
	FIXED_INFO* pFixedInfo = NULL;
	ULONG OutBufLen = 0;
	DWORD result;
	//获取网卡适配器长度OutBufLen
	result = GetNetworkParams(NULL, &OutBufLen);
	if (result == ERROR_BUFFER_OVERFLOW) {
		//分配内存
		pFixedInfo = (FIXED_INFO*)malloc(OutBufLen);
		if (!pFixedInfo) {
			_tprintf(_T("ERROR: failed to allocate 0x%08X bytes of memory\n"), OutBufLen);
			return;
		}
	} else {
		_tprintf(_T("ERROR: GetNetworkParams() failed to report required buffer size.\n"));
		return;
	}
	//真正获取网卡的网络参数信息
	result = GetNetworkParams(pFixedInfo, &OutBufLen);
	if (result == ERROR_SUCCESS) {
		IP_ADDR_STRING* pIPAddr;
		//主机名
		printf("\tHostName. . . . . . . . . . . : %s\n",  pFixedInfo->HostName);
		//域名
		printf("\tDomainName. . . . . . . . . . : %s\n",  pFixedInfo->DomainName);
		//dns服务器
		printf("\tDNS Servers . . . . . . . . . : %s\n",  pFixedInfo->DnsServerList.IpAddress.String);
		//本机DnsServerList ip地址
		pIPAddr = pFixedInfo->DnsServerList.Next;
		while (pIPAddr) {
			printf("\t\t\t\t      : %s\n",  pIPAddr->IpAddress.String);
			pIPAddr = pIPAddr->Next;
		}
		_tprintf(_T("\tNodeType. . . . . . . . . . . : %d (%s)\n"), pFixedInfo->NodeType, 
			GetNodeTypeName(pFixedInfo->NodeType));
		_tprintf(_T("\tScopeId . . . . . . . . . . . : %s\n"),  pFixedInfo->ScopeId);
		_tprintf(_T("\tEnableRouting . . . . . . . . : %s\n"), pFixedInfo->EnableRouting ? _T("yes") : _T("no"));
		_tprintf(_T("\tEnableProxy . . . . . . . . . : %s\n"), pFixedInfo->EnableProxy ? _T("yes") : _T("no"));
		_tprintf(_T("\tEnableDns . . . . . . . . . . : %s\n"), pFixedInfo->EnableDns ? _T("yes") : _T("no"));
		_tprintf(_T("\n"));
	} else {
		//获取不成功则提示错误
		switch (result) {
		case ERROR_BUFFER_OVERFLOW:
			_tprintf(_T("The buffer size indicated by the pOutBufLen parameter is too small to hold the adapter information. The pOutBufLen parameter points to the required size\n"));
			break;
		case ERROR_INVALID_PARAMETER:
			_tprintf(_T("The pOutBufLen parameter is NULL, or the calling process does not have read/write access to the memory pointed to by pOutBufLen, or the calling process does not have write access to the memory pointed to by the pAdapterInfo parameter\n"));
			break;
		case ERROR_NO_DATA:
			_tprintf(_T("No adapter information exists for the local computer\n"));
			break;
		case ERROR_NOT_SUPPORTED:
			_tprintf(_T("This function is not supported on the operating system in use on the local system\n"));
			break;
		default:
			_tprintf(_T("0x%08X - Use FormatMessage to obtain the message string for the returned error\n"), result);
			break;
		}
	}
}
//显示网卡接口信息
static void ShowNetworkInterfaces()
{
	IP_INTERFACE_INFO* pIfTable = NULL;
	DWORD result;
	DWORD dwNumIf;
	DWORD dwOutBufLen = 0;
	//获取网卡接口数量
	if ((result = GetNumberOfInterfaces(&dwNumIf)) != NO_ERROR) {
		_tprintf(_T("GetNumberOfInterfaces() failed with code 0x%08X - Use FormatMessage to obtain the message string for the returned error\n"), result);
		return;
	} else {
		_tprintf(_T("GetNumberOfInterfaces() returned %d\n"), dwNumIf);
	}
	//获取网卡接口信息
	result = GetInterfaceInfo(pIfTable, &dwOutBufLen);
	//分配内存
	pIfTable = (IP_INTERFACE_INFO*)malloc(dwOutBufLen);
	if (!pIfTable) {
		_tprintf(_T("ERROR: failed to allocate 0x%08X bytes of memory\n"), dwOutBufLen);
		return;
	}
	//获取网卡接口表信息,即网卡适配器
	result = GetInterfaceInfo(pIfTable, &dwOutBufLen);
	if (result == NO_ERROR) {
		int i;
		_tprintf(_T("GetInterfaceInfo() returned with %d adaptor entries\n"), pIfTable->NumAdapters);
		for (i = 0; i < pIfTable->NumAdapters; i++) {
			wprintf(L"[%d] %s \n", i + 1, pIfTable->Adapter[i].Name);
		}
	} else {
		//获取不成功,则提示出来
		switch (result) {
		case ERROR_INVALID_PARAMETER:
			_tprintf(_T("The dwOutBufLen parameter is NULL, or GetInterfaceInterface is unable to write to the memory pointed to by the dwOutBufLen parameter\n"));
			break;
		case ERROR_INSUFFICIENT_BUFFER:
			_tprintf(_T("The buffer pointed to by the pIfTable parameter is not large enough. The required size is returned in the DWORD variable pointed to by the dwOutBufLen parameter\n"));
			_tprintf(_T("\tdwOutBufLen: %d\n"), dwOutBufLen);
			break;
		case ERROR_NOT_SUPPORTED:
			_tprintf(_T("This function is not supported on the operating system in use on the local system\n"));
			break;
		default:
			_tprintf(_T("0x%08X - Use FormatMessage to obtain the message string for the returned error\n"), result);
			break;
		}
	}
	free(pIfTable);
}
//显示网卡适配器信息
static void ShowAdapterInfo()
{
	IP_ADAPTER_INFO* pAdaptorInfo;
	ULONG ulOutBufLen;
	DWORD dwRetVal;

	_tprintf(_T("\nAdaptor Information\t\n"));
	//申请内存
	pAdaptorInfo = (IP_ADAPTER_INFO*)GlobalAlloc(GPTR, sizeof(IP_ADAPTER_INFO));
	ulOutBufLen = sizeof(IP_ADAPTER_INFO);
	//获取适配器信息
	if (ERROR_BUFFER_OVERFLOW == GetAdaptersInfo(pAdaptorInfo, &ulOutBufLen)) {
		//如果获取失败,释放内存
		GlobalFree(pAdaptorInfo);
		//重新申请内存
		pAdaptorInfo = (IP_ADAPTER_INFO*)GlobalAlloc(GPTR, ulOutBufLen);
	}
	//再次获取适配器信息
	if (dwRetVal = GetAdaptersInfo(pAdaptorInfo, &ulOutBufLen)) {
		//获取失败
		_tprintf(_T("Call to GetAdaptersInfo failed. Return Value: %08x\n"), dwRetVal);
	} else {
		//获取成功
		//_IP_ADDR_STRING* pIPAddr;		
		while (pAdaptorInfo) {
			printf("  AdapterName: %s\n", pAdaptorInfo->AdapterName);
			printf("  Description: %s\n", pAdaptorInfo->Description);
			printf("  IPv4:%s\n",pAdaptorInfo->IpAddressList.IpAddress.String);
			printf("  Mask:%s\n",pAdaptorInfo->IpAddressList.IpMask.String);

			printf("  GatewayList:%s\n",pAdaptorInfo->GatewayList.IpAddress.String);
			printf("  Mask:%s\n",pAdaptorInfo->GatewayList.IpMask.String);
			printf("  DhcpServer:%s\n",pAdaptorInfo->DhcpServer.IpAddress.String);
			printf("  Mask:%s\n",pAdaptorInfo->DhcpServer.IpMask.String);

			printf("  PrimaryWinsServer:%s\n",pAdaptorInfo->PrimaryWinsServer.IpAddress.String);
			printf("  SecondaryWinsServer:%s\n",pAdaptorInfo->SecondaryWinsServer.IpAddress.String);
			printf("  Address:%s\n",pAdaptorInfo->Address);		
			printf("  DhcpEnabled:%s \n\n",pAdaptorInfo->DhcpEnabled ? ("yes") : ("no"));
			/**
			IP_ADDR_STRING* myCurrentIpAddress=(pAdaptorInfo->CurrentIpAddress);
			printf("  CurrentIpAddress:%s\n",myCurrentIpAddress->IpAddress.String);
			printf("  Mask:%s\n",myCurrentIpAddress->IpMask.String);*/
			//继续下一个网卡适配器
			pAdaptorInfo = pAdaptorInfo->Next;
		}
	}
}

const char szUsage[] = { "USAGE:\n ipconfig.exe "};
static void usage(void)
{
	fputs(szUsage, stderr);
}
int main(int argc, char *argv[])
{
	if (argc > 1) {
		usage();
		return -1;
	}
	_tprintf(_T("ReactOS IP Configuration\n"));
	ShowNetworkFixedInfo();
	ShowNetworkInterfaces();
	ShowAdapterInfo();
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值