In Windows C++, you can use the GetAdaptersInfo function to retrieve a list of all network adapters and their associated IP addresses, including both IPv4 and IPv6 addresses.
Here's an example code snippet that demonstrates how to do this:
cpp
#include <windows.h>
#include <winioctl.h>
#include <ipsmanset.h>
// Function to retrieve IP addresses for a given adapter
void GetAdapterIPAddresses(const char* adapterName, vector<string>& ipAddresses)
{
// Create a socket handle
SOCKET s = WSASocket(AF_INET, SOCK_DGRAM, 0, 0, 0, 0);
if (s == INVALID_SOCKET)
{
// Handle error
return;
}
// Get adapter information
IP_ADAPTER_INFO adapterInfo;
DWORD dwRetVal = GetAdaptersInfo(AF_UNSPEC, 0, 0, &adapterInfo, sizeof(IP_ADAPTER_INFO));
if (dwRetVal == ERROR_BUFFER_OVERFLOW)
{
// Buffer overflow, retry with larger buffer
char* buffer = new char[dwRetVal];
dwRetVal = GetAdaptersInfo(AF_UNSPEC, 0, 0, &adapterInfo, dwRetVal);
if (dwRetVal == NO_ERROR)
{
// Copy adapter information to vector
for (ULONG i = 0; i < adapterInfo.Length; i += sizeof(IP_ADAPTER_ADDRESSES))
{
IP_ADAPTER_ADDRESSES* pAddresses = (IP_ADAPTER_ADDRESSES*)&adapterInfo.FirstUnicastAddress;
string ipAddress = inet_ntoa(*pAddresses->FirstUnicastAddress.lpAddress);
ipAddresses.push_back(ipAddress);
}
}
delete[] buffer;
}
else if (dwRetVal == NO_ERROR)
{
// Copy adapter information to vector
for (ULONG i = 0; i < adapterInfo.Length; i += sizeof(IP_ADAPTER_ADDRESSES))
{
IP_ADAPTER_ADDRESSES* pAddresses = (IP_ADAPTER_ADDRESSES*)&adapterInfo.FirstUnicastAddress;
string ipAddress = inet_ntoa(*pAddresses->FirstUnicastAddress.lpAddress);
ipAddresses.push_back(ipAddress);
}
}
else
{
// Handle error
}
// Close socket handle
WSAClose(s, 0);
}
int main()
{
// Initialize vector to store IP addresses
vector<string> ipAddresses;
// Get a list of all adapters
IP_ADAPTER_INFO* pAdapterInfo = nullptr;
DWORD dwRetVal = GetAdaptersInfo(AF_UNSPEC, 0, 0, &pAdapterInfo, 0);
if (dwRetVal == NO_ERROR)
{
// Iterate over adapters and retrieve IP addresses
for (ULONG i = 0; i < pAdapterInfo->Length; i += sizeof(IP_ADAPTER_INFO))
{
IP_ADAPTER_INFO* pCurrentAdapter = (IP_ADAPTER_INFO*)&pAdapterInfo[i];
string adapterName = pCurrentAdapter->AdapterName;
vector<string> tempIpAddresses;
GetAdapterIPAddresses(adapterName.c_str(), tempIpAddresses);
ipAddresses.insert(ipAddresses.end(), tempIpAddresses.begin(), tempIpAddresses.end());
}
}
else
{
// Handle error
}
// Print IP addresses
for (const string& ipAddress : ipAddresses)
{
cout << ipAddress << endl;
}
return 0;
}
In this example, we first define a function called GetAdapterIPAddresses that takes a string representing the adapter name as input, and a vector of strings to store the IP addresses. The function retrieves the IP addresses for the given adapter using the GetAdaptersInfo function, and then copies the addresses to the vector.
In the main function, we first allocate memory for an adapter information structure using GetAdaptersInfo with a buffer size of 0. We then iterate over the adapters, retrieve their IP addresses using GetAdapterIPAddresses, and store the addresses in a vector. Finally, we print the IP addresses to the console.
Note that this code uses the AF_UNSPEC flag to retrieve both IPv4 and IPv6 addresses. If you only want to retrieve IPv4 addresses, you can use AF_INET instead. Similarly, if you only want to retrieve IPv6 addresses, you can use AF_INET6.