VC如何获取一个机器上的主IP和从IP

z1zhangyanan 2007-11-30 01:59:05
求助,Windows操作系统中,如果有多个IP,那么用VC将如何获取所有的IP呢?
...全文
301 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hljalong 2007-12-05
  • 打赏
  • 举报
回复
我查了一下MSDN,这个函数GetAdaptersAddresses要求有点高:
Client Requires Windows Vista or Windows XP.
Server Requires Windows Server 2008 or Windows Server 2003.
Header Declared in Iphlpapi.h.

Library Use Iphlpapi.lib.

DLL Requires Iphlpapi.dll.

我的VC6是安装到WIN98上的,没有要求的.h和.lib及相应的.dll文件。

我以前是用如下方法做的,当然也是在CSDN上学别人的,你不妨试一下。

char szHostName[128];
CString ipaddr="";

WSADATA WsaData;
WSAStartup(MAKEWORD(2,2), &WsaData);


int rslt = gethostname(szHostName, 128);
if( rslt == 0 )
{
struct hostent * pHost;
pHost = gethostbyname(szHostName);
hostent* mpHost = gethostbyaddr(*(pHost->h_addr_list), 4 ,AF_INET);
for( int i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
ipaddr = ipaddr + inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]) + "\r\n";
}
MessageBox(ipaddr);
}
else
{
int nError = WSAGetLastError();
ipaddr.Format("%d",nError);
MessageBox("出错了:" + ipaddr);
}

WSACleanup();


这个代码只要有winsock2.h和ws2_32.lib就行了。
z1zhangyanan 2007-12-04
  • 打赏
  • 举报
回复
嗯,我用的是VC6.0。这位GG,有没有办法帮忙呀?
hljalong 2007-12-04
  • 打赏
  • 举报
回复
这种情况下,如果你用的是.net环境,只要在代码中的这个类型上点右键,在快捷菜单中就可选择 转到定义 这项,接着你就应该怎么都知道了。
z1zhangyanan 2007-12-04
  • 打赏
  • 举报
回复
这个我已经添加了。而且新建了个工程,单纯添加了这个功能。
但还是报错。错误如下:
D:\vcproject\iptest\iptest.cpp(10) : error C2065: 'PIP_ADAPTER_ADDRESSES' : undeclared identifier
D:\vcproject\iptest\iptest.cpp(10) : error C2146: syntax error : missing ';' before identifier 'pAddresses'
D:\vcproject\iptest\iptest.cpp(10) : error C2065: 'pAddresses' : undeclared identifier
D:\vcproject\iptest\iptest.cpp(11) : error C2065: 'IP_ADAPTER_ADDRESSES' : undeclared identifier
D:\vcproject\iptest\iptest.cpp(11) : error C2059: syntax error : ')'
D:\vcproject\iptest\iptest.cpp(17) : error C2065: 'GetAdaptersAddresses' : undeclared identifier
D:\vcproject\iptest\iptest.cpp(23) : error C2059: syntax error : ')'
D:\vcproject\iptest\iptest.cpp(35) : error C2227: left of '->FriendlyName' must point to class/struct/union
D:\vcproject\iptest\iptest.cpp(36) : error C2227: left of '->Description' must point to class/struct/union
D:\vcproject\iptest\iptest.cpp(37) : error C2227: left of '->Next' must point to class/struct/union

xujianlane 2007-12-04
  • 打赏
  • 举报
回复
#include <winsock2.h>
#include <Ws2tcpip.h>
z1zhangyanan 2007-12-04
  • 打赏
  • 举报
回复
to:xujianlane
我尝试了这段代码,为什么我把winsock.h之类的文件include近来后,还是找不到一些变量和方法呢?比如PIP_ADAPTER_ADDRESSES就找不到啊?
xujianlane 2007-11-30
  • 打赏
  • 举报
回复
有个msn的例子
PIP_ADAPTER_ADDRESSES pAddresses;
pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(sizeof(IP_ADAPTER_ADDRESSES));
ULONG outBufLen = 0;
DWORD dwRetVal = 0;

// Make an initial call to GetAdaptersAddresses to get the
// size needed into the outBufLen variable
if (GetAdaptersAddresses(AF_INET,
0,
NULL,
pAddresses,
&outBufLen) == ERROR_BUFFER_OVERFLOW) {
GlobalFree(pAddresses);
pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(outBufLen);
}

// Make a second call to GetAdapters Addresses to get the
// actual data we want
if ((dwRetVal = GetAdaptersAddresses(AF_INET,
0,
NULL,
pAddresses,
&outBufLen)) == NO_ERROR) {
// If successful, output some information from the data we received
while (pAddresses) {
printf("\tFriendly name: %S\n", pAddresses->FriendlyName);
printf("\tDescription: %S\n", pAddresses->Description);
pAddresses = pAddresses->Next;
}
}
else {
printf("Call to GetAdaptersAddresses failed.\n");
LPVOID lpMsgBuf;
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwRetVal,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL )) {
printf("\tError: %s", lpMsgBuf);
}
LocalFree( lpMsgBuf );
}

z1zhangyanan 2007-11-30
  • 打赏
  • 举报
回复
GetAdaptersAddresses() 怎么用?
有具体的实现吗?谢谢。帮个忙吧。
z1zhangyanan 2007-11-30
  • 打赏
  • 举报
回复
我就是要得到所有的IP地址
xujianlane 2007-11-30
  • 打赏
  • 举报
回复
GetAdaptersAddresses()
不知道你要做什么,这个函数可以获取全部IP,但是会包含进虚拟设备的IP请留意。
飞哥 2007-11-30
  • 打赏
  • 举报
回复
查到的本身就是一个列表,通常取第一个,使用gethostname ,gethostbyname

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

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