C++ Получить имена сетевых устройств
Есть функция для получения IP адресов сетевых устройств.
Как получить их имена
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include<fstream>
#include <regex>
// Для корректной работы freeaddrinfo в MinGW
#define _WIN32_WINNT 0x501
#include <WinSock2.h>
#include <WS2tcpip.h>
//#pragma comment(lib, "Ws2_32.lib")
std::string PcName; //Название компютера в сети
struct sIP{
std::string ip;
std::string name;
};
std::vector<sIP>IpList; //Достные сетевіе интерфейсы
//Получить один из ІР первый попавшыйся обяычно нужный Остальные записать в масив
std::string GetIP()
{
PcName = ""; //Название компютера в сети
IpList.clear(); //Достные сетевіе интерфейсы
const int winsock_version = 2;
std::string out = "WinSock ERR";
WSADATA wsaData;
if (!WSAStartup(winsock_version/*WINSOCK_VERSION*/, &wsaData))
{
char chInfo[64];
if (!gethostname(chInfo, sizeof(chInfo)))
{
hostent *sh;
sh = gethostbyname((char*)&chInfo);
PcName = chInfo; //Имя пк
//std::cout<<chInfo<<"<<\n";
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);
//sh->h_name
out = inet_ntoa(adr.sin_addr);
sIP temp1;
temp1.ip = out;
temp1.name = "no ralisation";
IpList.push_back(temp1);
nAdapter++;
}
}
}
}
WSACleanup();
return out;
}
int main()
{
GetIP();
std::cout<<"PC name: "<< PcName<< std::endl;
for(int i = 0;i < IpList.size();i++)
{
std::cout<<"PC name: " << IpList[i].name<< " IP:"<< IpList[i].ip<<std::endl;
}
}