*.cpp подключаем #includeПолучение IP адресов всех сетевых карт
*.h public: // User declarations TStringList *GetIp(); *.cpp //--------------------------------------------------------------------------- TStringList *TForm1::GetIp() { TStringList *ip_list= new TStringList(); TStrings *key_name = new TStringList(); TRegistry *reg = new TRegistry(); int kl_key; reg->RootKey=HKEY_LOCAL_MACHINE; reg->OpenKeyReadOnly("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards"); reg->GetKeyNames(key_name); kl_key=key_name->Count; delete reg; delete key_name; WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(1, 0); int err = WSAStartup(wVersionRequested, &wsaData); if(err == 0) { char hn[1024]; struct hostent *adr; if(gethostname((char *)&hn, 1024)) { int err = WSAGetLastError(); Beep(); }; adr = gethostbyname(hn); if(adr) { for (int i = 0; i < kl_key; i++) { char *LocalIp = new char[15]; sprintf(LocalIp,"%d.%d.%d.%d", (unsigned char)adr->h_addr_list[i][0], (unsigned char)adr->h_addr_list[i][1], (unsigned char)adr->h_addr_list[i][2], (unsigned char)adr->h_addr_list[i][3]); ip_list->Add(LocalIp); delete LocalIp; } return ip_list; //Выводим IP адрес } } WSACleanup(); }Получение IP для одной сетевой
//--------------------------------------------------------------------------- String TForm1::GetIp() { String out = "WinSock ERR"; WSADATA wsaData; if (!WSAStartup(WINSOCK_VERSION, &wsaData)){char chInfo[64]; if (!gethostname(chInfo, sizeof(chInfo))) { hostent *sh; sh=gethostbyname((char*)&chInfo); if (sh!=NULL) { int nAdapter = 0; while (sh->h_addr_list[nAdapter]) { struct sockaddr_in adr; memcpy(&adr.sin_addr, sh->h_addr_list[nAdapter], sh->h_length); out = inet_ntoa(adr.sin_addr); nAdapter++; } } } } WSACleanup(); return out; } //---------------------------------------------------------------------------
Комментариев нет:
Отправить комментарий