I would use the GetAdaptersAddresses()
function of windows to get some informations about the adapters of the computer. In this MSDN page there is a big example of how to use it. But I'm using C++ so I would stick with the new
keyword and not malloc
(C native). How can I rewrite the parts that uses malloc
with new
?
EDIT 1
std::vector<PIP_ADAPTER_ADDRESSES> buffer;
ULONG outBufferLen = 0;
if (GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &outBufferLen) == ERROR_BUFFER_OVERFLOW)
{
buffer.resize(outBufferLen);
PIP_ADAPTER_ADDRESSES addresses = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(buffer.data());
if (GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, addresses, &outBufferLen) == NO_ERROR)
{
for (; addresses != NULL; addresses = addresses->Next)
{
...
}
}
}
EDIT 2
ULONG outBufferLen = 0;
if (GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &outBufferLen) == ERROR_BUFFER_OVERFLOW)
{
PIP_ADAPTER_ADDRESSES p = new IP_ADAPTER_ADDRESSES[outBufferLen / sizeof(IP_ADAPTER_ADDRESSES)];
if (GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, p, &outBufferLen) == NO_ERROR)
{
....
}
}