MFC获取本机IP的方法

来源:互联网 发布:乐视电视软件 编辑:程序博客网 时间:2024/06/05 17:20

工作中遇到需要sip发送REGISTER时,参数需要程序自动获取本机ip 实现如下:这些只是核心代码///

      #include <winsock.h>
      WORD wVersionRequested;WSADATA wsaData;char name[255];CString ip;PHOSTENT hostinfo;wVersionRequested = MAKEWORD( 2, 0 );if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ){if( gethostname ( name, sizeof(name)) == 0){if((hostinfo = gethostbyname(name)) != NULL){ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);}}WSACleanup( );}
       AfxMessageBox(ip); //返回CString类型的,根据需要自己调整
                          
 


原创粉丝点击