VC++获取本机所有IP和掩码信息

来源:互联网 发布:万方数据库下载器 编辑:程序博客网 时间:2024/06/07 02:58
[cpp] view plain copy
  1. // GetIPs.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <atlstr.h>  
  6. #include <IPHlpApi.h>  
  7. //#include <WinSock2.h>  
  8. #include <iostream>  
  9.   
  10. //#pragma comment(lib, "ws2_32.lib")  
  11. #pragma comment(lib, "Iphlpapi.lib")  
  12.   
  13. using namespace std;  
  14.   
  15. int _tmain(int argc, _TCHAR* argv[])  
  16. {  
  17.   
  18.     CString szMark;  
  19.   
  20.   
  21.     PIP_ADAPTER_INFO pAdapterInfo;  
  22.     PIP_ADAPTER_INFO pAdapter = NULL;   
  23.     DWORD dwRetVal = 0;   
  24.   
  25.     pAdapterInfo = ( IP_ADAPTER_INFO * ) malloc( sizeof( IP_ADAPTER_INFO ) );  
  26.     ULONG ulOutBufLen;   
  27.     ulOutBufLen = sizeof(IP_ADAPTER_INFO);   
  28.   
  29.   
  30.   
  31.     // 第一次调用GetAdapterInfo获取ulOutBufLen大小   
  32.     if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)   
  33.     {   
  34.         free(pAdapterInfo);  
  35.         pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);   
  36.     }   
  37.   
  38.     if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR)   
  39.     {   
  40.         pAdapter = pAdapterInfo;   
  41.         while (pAdapter)   
  42.         {  
  43.             PIP_ADDR_STRING pIPAddr;  
  44.             pIPAddr = &pAdapter->IpAddressList;  
  45.             while (pIPAddr)  
  46.             {  
  47.                 cout << "IP:" << pIPAddr->IpAddress.String << endl;  
  48.                 cout << "Mask:" << pIPAddr->IpMask.String << endl;  
  49.                 cout << endl;  
  50.                 pIPAddr = pIPAddr->Next;  
  51.             }  
  52.             pAdapter = pAdapter->Next;   
  53.         }   
  54.     }   
  55.   
  56.       
  57.     getchar();  
  58.     return 0;  
  59. }  

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include<stdio.h>  
  3. #include<iostream>  
  4. #include<string.h>  
  5. #include<winsock2.h>  
  6. #include<windows.h>  
  7. #pragma comment(lib, "ws2_32.lib")  
  8. using namespace std;  
  9.   
  10. #define ERR_EXIT(m) \  
  11.     do\  
  12. {\  
  13.     perror(m);\  
  14.     exit(EXIT_FAILURE);\  
  15. }while(0)  
  16.   
  17. int getlocalip(char *ip)  
  18. {  
  19.     char host[100] ={"sina.com.cn"};  
  20.     if(gethostname(host,sizeof(host))<0)  
  21.         return -1;  
  22.     struct hostent *hp;  
  23.     if((hp =gethostbyname(host))==NULL)  
  24.         return -1;  
  25.     strcpy(ip,inet_ntoa(*(struct in_addr*)hp->h_addr));  //h_addr代表主机IP  
  26.     return 0;  
  27. }  
  28. int main()  
  29. {  
  30.     WSADATA wsd;  
  31.   
  32.     if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)  
  33.     {  
  34.         cout<<"WSAStartup failed!"<<endl;  
  35.         ERR_EXIT("WSAStartup");  
  36.     }  
  37.   
  38.     char host[100] ={0};  
  39.     if(gethostname(host,sizeof(host)) ==SOCKET_ERROR)  //获取主机名  
  40.         ERR_EXIT("gethostname");  
  41.   
  42.     struct hostent *hp;  
  43.     if((hp = gethostbyname(host)) ==NULL)  //通过主机名获取更多信息  
  44.         ERR_EXIT("gethostbyname");  
  45.   
  46.     int i =0;  
  47.     while(hp->h_addr_list[i]!=NULL)  
  48.     {  
  49.         printf("%s\n",inet_ntoa(*(struct in_addr*)hp->h_addr_list[i]));  
  50.         i++;  
  51.     }  
  52.   
  53.     char ip[16] ={0};  
  54.     getlocalip(ip);  
  55.     printf("localip =%s\n",ip);  
  56.   
  57.     getchar();  
  58.     return 0;     
  59. }  


原创粉丝点击