I. Obtention de l'adresse IP locale▲
Exemple : Sur une form (Form1) mettre un Bouton (Button1) et 2 label (Label1 et Label2) Dans l'évènement OnClick de Button1 :
Sélectionnez
#include
<winsock2.h>
//.................
void
__fastcall TForm1::
Button1Click(TObject *
Sender)
{
WORD wVersionRequested;
WSADATA wsaData;
char
name[255
];
AnsiString ip;
PHOSTENT hostinfo;
wVersionRequested =
MAKEWORD( 2
, 0
);
if
( WSAStartup( wVersionRequested, &
wsaData ) ==
0
)
{
if
( gethostname ( name, sizeof
(name)) ==
0
)
{
Label1->
Caption =
name; //nom de l'ordinateur
if
((hostinfo =
gethostbyname(name)) !=
NULL
)
{
ip =
inet_ntoa (*
(in_addr *
)*
hostinfo->
h_addr_list);
Label2->
Caption =
ip; //adresse IP
}
}
WSACleanup( );
}
}