关于获取本地所有IP地址的办法

lirg8405 2012-01-18 05:02:54
XP系统中,本地连接属性:ICP/IP属性:高级中,IP设置可以添加多个IP,我想问的是:

1、MFC中,如何得到这些IP?

2、在其他 Windows操作系统中,是否一样的处理?

谢谢
...全文
157 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly4free 2012-01-18
  • 打赏
  • 举报
回复
这个Mark了。回头试试。
Eleven 2012-01-18
  • 打赏
  • 举报
回复
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
/* Note: could also use malloc() and free() */

int __cdecl main()
{

/* Declare and initialize variables */

// It is possible for an adapter to have multiple
// IPv4 addresses, gateways, and secondary WINS servers
// assigned to the adapter.
//
// Note that this sample code only prints out the
// first entry for the IP address/mask, and gateway, and
// the primary and secondary WINS server for each adapter.

PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
UINT i;

/* variables used to print DHCP time info */
struct tm newtime;
char buffer[32];
errno_t error;

ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
}

if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
printf("\tComboIndex: \t5d\n", pAdapter->ComboIndex);
printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);
printf("\tAdapter Desc: \t%s\n", pAdapter->Description);
printf("\tAdapter Addr: \t");
for (i = 0; i < pAdapter->AddressLength; i++) {
if (i == (pAdapter->AddressLength - 1))
printf("%.2X\n", (int) pAdapter->Address[i]);
else
printf("%.2X-", (int) pAdapter->Address[i]);
}
printf("\tIndex: \t%d\n", pAdapter->Index);
printf("\tType: \t");
switch (pAdapter->Type) {
case MIB_IF_TYPE_OTHER:
printf("Other\n");
break;
case MIB_IF_TYPE_ETHERNET:
printf("Ethernet\n");
break;
case MIB_IF_TYPE_TOKENRING:
printf("Token Ring\n");
break;
case MIB_IF_TYPE_FDDI:
printf("FDDI\n");
break;
case MIB_IF_TYPE_PPP:
printf("PPP\n");
break;
case MIB_IF_TYPE_LOOPBACK:
printf("Lookback\n");
break;
case MIB_IF_TYPE_SLIP:
printf("Slip\n");
break;
default:
printf("Unknown type %ld\n", pAdapter->Type);
break;
}

printf("\tIP Address: \t%s\n",
pAdapter->IpAddressList.IpAddress.String);
printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);

printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
printf("\t***\n");

if (pAdapter->DhcpEnabled) {
printf("\tDHCP Enabled: Yes\n");
printf("\t DHCP Server: \t%s\n",
pAdapter->DhcpServer.IpAddress.String);

printf("\t Lease Obtained: ");
/* Display local time */
error = _localtime32_s(&newtime, &pAdapter->LeaseObtained);
if (error)
printf("Invalid Argument to _localtime32_s\n");
else {
// Convert to an ASCII representation
error = asctime_s(buffer, 32, &newtime);
if (error)
printf("Invalid Argument to asctime_s\n");
else
/* asctime_s returns the string terminated by \n\0 */
printf("%s", buffer);
}

printf("\t Lease Expires: ");
error = _localtime32_s(&newtime, &pAdapter->LeaseExpires);
if (error)
printf("Invalid Argument to _localtime32_s\n");
else {
// Convert to an ASCII representation
error = asctime_s(buffer, 32, &newtime);
if (error)
printf("Invalid Argument to asctime_s\n");
else
/* asctime_s returns the string terminated by \n\0 */
printf("%s", buffer);
}
} else
printf("\tDHCP Enabled: No\n");

if (pAdapter->HaveWins) {
printf("\tHave Wins: Yes\n");
printf("\t Primary Wins Server: %s\n",
pAdapter->PrimaryWinsServer.IpAddress.String);
printf("\t Secondary Wins Server: %s\n",
pAdapter->SecondaryWinsServer.IpAddress.String);
} else
printf("\tHave Wins: No\n");
pAdapter = pAdapter->Next;
printf("\n");
}
} else {
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);

}
if (pAdapterInfo)
FREE(pAdapterInfo);

return 0;
}

MSDN上的例子代码
zwfgdlc 2012-01-18
  • 打赏
  • 举报
回复
GetAdaptersInfo()函数.

int _tmain(int argc, _TCHAR* argv[])
{
PIP_ADAPTER_INFO pAdapterInfo = 0;
ULONG uSize = 0;
PIP_ADDR_STRING pAddress = 0;

GetAdaptersInfo(pAdapterInfo, &uSize);
pAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[uSize];

if (GetAdaptersInfo(pAdapterInfo, &uSize) == ERROR_SUCCESS)
{
while(pAdapterInfo)
{
printf_s("网卡名=%s\n", pAdapterInfo->Description);
pAddress = &pAdapterInfo->IpAddressList;

while(pAddress)
{
printf_s("IP=%s\n", pAddress->IpAddress.String);
pAddress = pAddress->Next;
}
pAdapterInfo = pAdapterInfo->Next;
}

}
delete [] pAdapterInfo;
}
chunyou128 2012-01-18
  • 打赏
  • 举报
回复
void CGetIPDlg::OnButok() 
{
// TODO: Add your control notification handler code here
CString str="",name;
m_name.GetWindowText(name);
struct hostent * pHost;
pHost = gethostbyname(name);
for(int i=0;i<4;i++)
{
CString addr;
if(i > 0)
{
str += ".";
}
addr.Format("%u",(unsigned int)((unsigned char*)pHost->h_addr_list[0])[i]);
str += addr;
}
m_ip.SetWindowText(str);
}

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧