如何能象ipconfig一样取得所有的信息,其中包括gateway?

watt 2004-07-23 02:21:49
或者是如何取得所有的gateway,包括多个网卡、ADSL等的。
...全文
196 11 点赞 打赏 收藏 举报
写回复
11 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
kugou123 2005-01-12
参考:
http://www.vckbase.com/document/viewdoc/?id=851
有示例
  • 打赏
  • 举报
回复
zyp2kyear 2005-01-12
/******************************************************************************\
* This is a part of the Microsoft Source Code Samples.
* Copyright 1996 - 1998 Microsoft Corporation.
* All rights reserved.
* This source code is only intended as a supplement to
* Microsoft Development Tools and/or WinHelp documentation.
* See these sources for detailed information regarding the
* Microsoft samples programs.
\******************************************************************************/

/*
Module Name:

Ipconfig.cpp

Abstract:

This module illustrates how to programmatically retrieve IP configuration
information similar to the IPCONFIG.EXE utility. It demonstrates how to use
the IP Helper APIs GetNetworkParams() and GetAdaptersInfo().

To execute this application, simply build the application using the Microsoft Visual C++
nmake.exe program generation utility to make an executable ipconfig.exe. After the
build is complete, simply execute the resulting ipconfig.exe program.


Author:

Jim Ohlund 21-Apr-98

Revision History:

*/


#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <time.h>

void main(void) {

DWORD Err;

PFIXED_INFO pFixedInfo;
DWORD FixedInfoSize = 0;

PIP_ADAPTER_INFO pAdapterInfo, pAdapt;
DWORD AdapterInfoSize;
PIP_ADDR_STRING pAddrStr;

//
// Get the main IP configuration information for this machine using a FIXED_INFO structure
//
if ((Err = GetNetworkParams(NULL, &FixedInfoSize)) != 0)
{
if (Err != ERROR_BUFFER_OVERFLOW)
{
printf("GetNetworkParams sizing failed with error %d\n", Err);
return;
}
}

// Allocate memory from sizing information
if ((pFixedInfo = (PFIXED_INFO) GlobalAlloc(GPTR, FixedInfoSize)) == NULL)
{
printf("Memory allocation error\n");
return;
}

if ((Err = GetNetworkParams(pFixedInfo, &FixedInfoSize)) == 0)
{
printf("\tHost Name . . . . . . . . . : %s\n", pFixedInfo->HostName);
printf("\tDNS Servers . . . . . . . . : %s\n", pFixedInfo->DnsServerList.IpAddress.String);
pAddrStr = pFixedInfo->DnsServerList.Next;
while(pAddrStr)
{
printf("%52s\n", pAddrStr->IpAddress.String);
pAddrStr = pAddrStr->Next;
}

printf("\tNode Type . . . . . . . . . : ");
switch (pFixedInfo->NodeType)
{
case 1:
printf("%s\n", "Broadcast");
break;
case 2:
printf("%s\n", "Peer to peer");
break;
case 4:
printf("%s\n", "Mixed");
break;
case 8:
printf("%s\n", "Hybrid");
break;
default:
printf("\n");
}

printf("\tNetBIOS Scope ID. . . . . . : %s\n", pFixedInfo->ScopeId);
printf("\tIP Routing Enabled. . . . . : %s\n", (pFixedInfo->EnableRouting ? "yes" : "no"));
printf("\tWINS Proxy Enabled. . . . . : %s\n", (pFixedInfo->EnableProxy ? "yes" : "no"));
printf("\tNetBIOS Resolution Uses DNS : %s\n", (pFixedInfo->EnableDns ? "yes" : "no"));
} else
{
printf("GetNetworkParams failed with error %d\n", Err);
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.
//
AdapterInfoSize = 0;
if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)
{
if (Err != ERROR_BUFFER_OVERFLOW)
{
printf("GetAdaptersInfo sizing failed with error %d\n", Err);
return;
}
}

// Allocate memory from sizing information
if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)
{
printf("Memory allocation error\n");
return;
}

// Get actual adapter information
if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)
{
printf("GetAdaptersInfo failed with error %d\n", Err);
return;
}

pAdapt = pAdapterInfo;

while (pAdapt)
{
switch (pAdapt->Type)
{
case MIB_IF_TYPE_ETHERNET:
printf("\nEthernet adapter ");
break;
case MIB_IF_TYPE_TOKENRING:
printf("\nToken Ring adapter ");
break;
case MIB_IF_TYPE_FDDI:
printf("\nFDDI adapter ");
break;
case MIB_IF_TYPE_PPP:
printf("\nPPP adapter ");
break;
case MIB_IF_TYPE_LOOPBACK:
printf("\nLoopback adapter ");
break;
case MIB_IF_TYPE_SLIP:
printf("\nSlip adapter ");
break;
case MIB_IF_TYPE_OTHER:
default:
printf("\nOther adapter ");
}
printf("%s:\n\n", pAdapt->AdapterName);

printf("\tDescription . . . . . . . . : %s\n", pAdapt->Description);

printf("\tPhysical Address. . . . . . : ");
for (UINT i=0; i<pAdapt->AddressLength; i++)
{
if (i == (pAdapt->AddressLength - 1))
printf("%.2X\n",(int)pAdapt->Address[i]);
else
printf("%.2X-",(int)pAdapt->Address[i]);
}

printf("\tDHCP Enabled. . . . . . . . : %s\n", (pAdapt->DhcpEnabled ? "yes" : "no"));

pAddrStr = &(pAdapt->IpAddressList);
while(pAddrStr)
{
printf("\tIP Address. . . . . . . . . : %s\n", pAddrStr->IpAddress.String);
printf("\tSubnet Mask . . . . . . . . : %s\n", pAddrStr->IpMask.String);
pAddrStr = pAddrStr->Next;
}

printf("\tDefault Gateway . . . . . . : %s\n", pAdapt->GatewayList.IpAddress.String);
pAddrStr = pAdapt->GatewayList.Next;
while(pAddrStr)
{
printf("%52s\n", pAddrStr->IpAddress.String);
pAddrStr = pAddrStr->Next;
}

printf("\tDHCP Server . . . . . . . . : %s\n", pAdapt->DhcpServer.IpAddress.String);
printf("\tPrimary WINS Server . . . . : %s\n", pAdapt->PrimaryWinsServer.IpAddress.String);
printf("\tSecondary WINS Server . . . : %s\n", pAdapt->SecondaryWinsServer.IpAddress.String);

struct tm *newtime;

// Display coordinated universal time - GMT
newtime = gmtime(&pAdapt->LeaseObtained);
printf( "\tLease Obtained. . . . . . . : %s", asctime( newtime ) );

newtime = gmtime(&pAdapt->LeaseExpires);
printf( "\tLease Expires . . . . . . . : %s", asctime( newtime ) );

pAdapt = pAdapt->Next;
}
}

  • 打赏
  • 举报
回复
EnochShen 2005-01-12
http://www.chinaunix.net/forum/viewtopic.php?t=85716&highlight=MAC

子网掩码+MAC
  • 打赏
  • 举报
回复
alongsoft1129 2005-01-12
mark
  • 打赏
  • 举报
回复
dlscdn 2004-07-23
mark
  • 打赏
  • 举报
回复
lixiaosan 2004-07-23
http://www.vchelp.net/vchelp/archive.asp?type_id=44&class_id=1&cata_id=5&article_id=676&search_term=
  • 打赏
  • 举报
回复
lixiaosan 2004-07-23
http://www.vchelp.net/vchelp/zsrc/tcpc.asp?type_id=44&class_id=1&cata_id=5&article_id=302&search_term=
  • 打赏
  • 举报
回复
gracezhu 2004-07-23
用SDK中提供的IP助手清除出函数GetAdaptersInfo(),GetNetworkParams, GetIfTable()等等, <windows网络编程>附录中有讲IPconfig的实现
  • 打赏
  • 举报
回复
lixiaosan 2004-07-23
GetAdaptersInfo()然后
GetNetworkParams()


获取适配器名称

这里指的适配器名称要区别于适配器描述,比如我的一块网卡,适配器描述是:Realtek RTL8139(A) PCI Fast Ethernet Adapter,适配器名称为:{66156DC3-44A4-434C-B8A9-0E5DB4B3EEAD}。获取适配器名称的方法有多种:

1.1 调用IP helper API取得适配器名称

ULONG ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO);
IP_ADAPTER_INFO *pAdapterInfoBkp, *pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // 缓冲区不够大
{
delete pAdapterInfo;
pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
pAdapterInfoBkp = pAdapterInfo;
}
if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS )
{
do{ // 遍历所有适配器
if(pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) // 判断是否为以太网接口
{
// pAdapterInfo->Description 是适配器描述
// pAdapterInfo->AdapterName 是适配器名称
}
pAdapterInfo = pAdapterInfo->Next;
}while(pAdapterInfo);
}
delete pAdapterInfoBkp;
1.2 读取注册表取得适配器名称

在Windows2000中可以通过遍历 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\000n\ (n是从0开始编号的数字)所有接口, 在Windows NT中可以读取HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards中的信息,下面以Windows2000为例: HKEY hKey, hSubKey, hNdiIntKey;

if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"System\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}",
0,
KEY_READ,
&hKey) != ERROR_SUCCESS)
return FALSE;

DWORD dwIndex = 0;
DWORD dwBufSize = 256;
DWORD dwDataType;
char szSubKey[256];
unsigned char szData[256];

while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
{
if(RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS)
{
dwBufSize = 256;
if(RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
if(strcmp((char*)szData, "ethernet") == 0) // 判断是不是以太网卡
{
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
// szData 中便是适配器详细描述
dwBufSize = 256;
if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
// szData 中便是适配器名称
}
}
}
}
RegCloseKey(hNdiIntKey);
}
RegCloseKey(hSubKey);
}

dwBufSize = 256;
} /* end of while */

RegCloseKey(hKey);
  • 打赏
  • 举报
回复
watt 2004-07-23
gateway如何取?
  • 打赏
  • 举报
回复
lixiaosan 2004-07-23
取ip地址
int GetIpAddress(const CString &sHostName, CStringArray &sIpAddress)
{
struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
sIpAddress.RemoveAll();
if(lpHostEnt==NULL)
{
//产生错误
return GetLastError();
}
//获取IP
int i=0;
LPSTR lpAddr=lpHostEnt->h_addr_list[i];
CString temp;
while(lpAddr)
{
i++;
struct in_addr inAddr;
memmove(&inAddr,lpAddr,4);
//转换为标准格式
temp=inet_ntoa(inAddr);
if(temp.IsEmpty())
{
break;
}
sIpAddress.Add(temp);
lpAddr=lpHostEnt->h_addr_list[i];
}
return 0;
}


调用:

if(AfxSocketInit(NULL)==FALSE)//初始化socket
{
AfxMessageBox("Socket Error");
}

CStringArray ip;
CString m_sHostName;
char hosttemp[256];
gethostname(hosttemp,256);
m_sHostName=hosttemp;//如果这里,m_sHostName="www.163.com",将得到网易的IP

GetIpAddress(dlg.m_sHostName,ip);//获得本地IP
CString temp;
int i=0;
while(i<ip.GetSize())
{

temp+=ip[i];
temp+=";\n";
i++;
}
MessageBox(dlg.m_sHostName+"的IP是:\n"+temp);
}
  • 打赏
  • 举报
回复
相关推荐
发帖
VC/MFC
加入

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2004-07-23 02:21
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……