VC++获取本机所有IP和掩码信息
2017-06-27 10:12
1216 查看
[cpp] view
plain copy
// GetIPs.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <atlstr.h>
#include <IPHlpApi.h>
//#include <WinSock2.h>
#include <iostream>
//#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "Iphlpapi.lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CString szMark;
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
pAdapterInfo = ( IP_ADAPTER_INFO * ) malloc( sizeof( IP_ADAPTER_INFO ) );
ULONG ulOutBufLen;
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
// 第一次调用GetAdapterInfo获取ulOutBufLen大小
if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
}
if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
{
pAdapter = pAdapterInfo;
while (pAdapter)
{
PIP_ADDR_STRING pIPAddr;
pIPAddr = &pAdapter->IpAddressList;
while (pIPAddr)
{
cout << "IP:" << pIPAddr->IpAddress.String << endl;
cout << "Mask:" << pIPAddr->IpMask.String << endl;
cout << endl;
pIPAddr = pIPAddr->Next;
}
pAdapter = pAdapter->Next;
}
}
getchar();
return 0;
}
[cpp] view
plain copy
#include "stdafx.h"
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<winsock2.h>
#include<windows.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define ERR_EXIT(m) \
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}while(0)
int getlocalip(char *ip)
{
char host[100] ={"sina.com.cn"};
if(gethostname(host,sizeof(host))<0)
return -1;
struct hostent *hp;
if((hp =gethostbyname(host))==NULL)
return -1;
strcpy(ip,inet_ntoa(*(struct in_addr*)hp->h_addr)); //h_addr代表主机IP
return 0;
}
int main()
{
WSADATA wsd;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
cout<<"WSAStartup failed!"<<endl;
ERR_EXIT("WSAStartup");
}
char host[100] ={0};
if(gethostname(host,sizeof(host)) ==SOCKET_ERROR) //获取主机名
ERR_EXIT("gethostname");
struct hostent *hp;
if((hp = gethostbyname(host)) ==NULL) //通过主机名获取更多信息
ERR_EXIT("gethostbyname");
int i =0;
while(hp->h_addr_list[i]!=NULL)
{
printf("%s\n",inet_ntoa(*(struct in_addr*)hp->h_addr_list[i]));
i++;
}
char ip[16] ={0};
getlocalip(ip);
printf("localip =%s\n",ip);
getchar();
return 0;
}
plain copy
// GetIPs.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <atlstr.h>
#include <IPHlpApi.h>
//#include <WinSock2.h>
#include <iostream>
//#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "Iphlpapi.lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CString szMark;
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
pAdapterInfo = ( IP_ADAPTER_INFO * ) malloc( sizeof( IP_ADAPTER_INFO ) );
ULONG ulOutBufLen;
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
// 第一次调用GetAdapterInfo获取ulOutBufLen大小
if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
}
if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
{
pAdapter = pAdapterInfo;
while (pAdapter)
{
PIP_ADDR_STRING pIPAddr;
pIPAddr = &pAdapter->IpAddressList;
while (pIPAddr)
{
cout << "IP:" << pIPAddr->IpAddress.String << endl;
cout << "Mask:" << pIPAddr->IpMask.String << endl;
cout << endl;
pIPAddr = pIPAddr->Next;
}
pAdapter = pAdapter->Next;
}
}
getchar();
return 0;
}
[cpp] view
plain copy
#include "stdafx.h"
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<winsock2.h>
#include<windows.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define ERR_EXIT(m) \
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}while(0)
int getlocalip(char *ip)
{
char host[100] ={"sina.com.cn"};
if(gethostname(host,sizeof(host))<0)
return -1;
struct hostent *hp;
if((hp =gethostbyname(host))==NULL)
return -1;
strcpy(ip,inet_ntoa(*(struct in_addr*)hp->h_addr)); //h_addr代表主机IP
return 0;
}
int main()
{
WSADATA wsd;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
cout<<"WSAStartup failed!"<<endl;
ERR_EXIT("WSAStartup");
}
char host[100] ={0};
if(gethostname(host,sizeof(host)) ==SOCKET_ERROR) //获取主机名
ERR_EXIT("gethostname");
struct hostent *hp;
if((hp = gethostbyname(host)) ==NULL) //通过主机名获取更多信息
ERR_EXIT("gethostbyname");
int i =0;
while(hp->h_addr_list[i]!=NULL)
{
printf("%s\n",inet_ntoa(*(struct in_addr*)hp->h_addr_list[i]));
i++;
}
char ip[16] ={0};
getlocalip(ip);
printf("localip =%s\n",ip);
getchar();
return 0;
}
相关文章推荐
- VC++获取本机所有IP和掩码信息
- VC++实现IP与ARP信息获取,可以同理实现APR攻击
- VC获取本机IP、子网掩码、网关、计算广播地址等信息代码(通过读注册表)
- 使用WMI控制网络适配器的类(获取所有启用的网络适配器及其信息。设置IP、子网掩码,网关,DNS)
- VC++实现IP与ARP信息获取,可以同理实现APR攻击
- 获取指定(访客)IP的所有信息,地址、邮政编码、国家、经纬度等的API
- vc 获取网络连接的ip,mac地址,dns,dhcp等详细信息
- VC++获取系统信息/获取OS/获取MAC/获取本地IP/判断是否为网吧
- php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法
- Python获取本机所有网卡ip,掩码和广播地址实例代码
- 获取指定(访客)IP的所有信息,地址、邮政编码、国家、经纬度等的API
- 获取vc下所有虚拟机的名称,目录等信息
- VC获取本机IP、子网掩码、网关、计算广播地址等信息
- c# 获取所有网卡的信息(IP。MAC)
- java获取局域网内所有ip信息
- VC++获取系统信息/获取OS/获取MAC/获取本地IP/判断是否为网吧
- php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法
- Python获取本机所有网卡ip,掩码和广播地址
- 获取外网IP和本机所有网卡地址信息
- 获取本机所有网卡的网卡名、网卡描述、网卡MAC地址、网卡IP、网卡类型等信息及网线是否插入状态