發新話題

[分享] 得到計算機的主機名和IP地址

得到計算機的主機名和IP地址

環境:Visual C++ 5 SP1/2 WINDOW NT4或WINDOWS 95.

需求:#include<winsock2.h>

鏈接庫:Wsock32.lib

詳細代碼:
複製內容到剪貼板
代碼:
{
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( );
}
}

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。