有个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 ); }
我查了一下MSDN,这个函数GetAdaptersAddresses要求有点高: Client Requires Windows Vista or Windows XP. Server Requires Windows Server 2008 or Windows Server 2003. Header Declared in Iphlpapi.h.
不知道你要做什么,这个函数可以获取全部IP,但是会包含进虚拟设备的IP请留意。
有具体的实现吗?谢谢。帮个忙吧。
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 );
}
我尝试了这段代码,为什么我把winsock.h之类的文件include近来后,还是找不到一些变量和方法呢?比如PIP_ADAPTER_ADDRESSES就找不到啊?
#include <Ws2tcpip.h>
但还是报错。错误如下:
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
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就行了。