您的位置:首页 > 编程语言 > C语言/C++

MFC获取本机IP地址

2015-09-17 12:40 344 查看
CString
CSockInfo::GetLocalHostIP()
{
CString strIP("");
WORD wVersionRequested;
    WSADATA wsaData;
    char name[255];
memset(name,'\0',255);
    PHOSTENT hostinfo;
    wVersionRequested = MAKEWORD( 2, 0 );

    if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
    {
if( gethostname ( name, sizeof(name)) == 0)
        {
if((hostinfo = gethostbyname(name)) != NULL)
            {
strIP= inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
            }
        }    
        WSACleanup( );
}
return strIP;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mfc c++