Chris
Chris

Reputation: 1283

Decode an IP_ADAPTER_ADDRESSES structure returned into memory by GetAdaptersAddresses Windows API function?

I have an AHK script that I need to get various info about a particular network interface. To do that I do a dll call to IPHlpAPI library for the GetAdaptersAddresses function, which returns a IP_ADAPTER_ADDRESSES structure at the address I provide. Then I start to read memory at that address and decode each member of the structure.

For instance, for a IP_ADAPTER_INFO structure, if I read the first 4 bytes I get the address at which the next structure starts. Then if I read the next 4 bytes I get the ComboIndex value. Then if I read the next 260 bytes and convert that as a ANSI character string, I get the name of the adapter. And so on. This worked so far.

But for the IP_ADAPTER_ADDRESSES structure it doesn't seem to work. I tried this: I skipped the first 8 bytes, being the Alignment, then I skipped the next 4 bytes being the Length of the structure, then I read the next 4 bytes that should've been the Index of the Adapter. But this number is random large number and it doesn't correspond to any of the adapters. Neither does the rest of the data, except if I convert each byte of the memory as an ANSI character, you can then see the AdapterName, Description and FriendlyName.

Actually all I want is the FriendlyName, because the rest of the info I already have it from the IP_ADAPTER_INFO structure. So at what address I can find FriendlyName in the IP_ADAPTER_ADDRESSES structure?

I also posted on pastebin the memory content I get:
IP_ADAPTER_ADDRESSES structure - HEX
IP_ADAPTER_ADDRESSES structure - ANSI

Upvotes: 0

Views: 2069

Answers (2)

Edward Clements
Edward Clements

Reputation: 5132

Maybe I don't understand your problem fully, I just use the structure itself to get the friendly name -- following is an abbreviated code snippet from a non-Unicode program:

PIP_ADAPTER_ADDRESSES pCurrAddresses;
ULONG outBufLen = sizeof(IP_ADAPTER_ADDRESSES);
GetAdaptersAddresses(family, flags, NULL, NULL, &outBufLen);
pCurrAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
dwRetVal = GetAdaptersAddresses(family, flags, NULL, pCurrAddresses, &outBufLen);
if (dwRetVal == NO_ERROR)
{    while (pCurrAddresses != NULL)
    {   printf("Description: %wS\n", pCurrAddresses->Description);
        printf("Friendly Name: %wS\n", pCurrAddresses->FriendlyName);
        pCurrAddresses = pCurrAddresses->Next;
    }
}
FREE(pAddresses);

Would something like this do the trick?

Upvotes: 1

Graeme Perrow
Graeme Perrow

Reputation: 57248

The first 8 bytes is a union, not a structure. This means that the Alignment field and the Length/IfIndex fields are stored in the same location in memory. The first 4 bytes of the structure will be the length and the next 4 will be the IfIndex.

Is there a reason you can't use the structure defined in IPTypes.h? i.e. get the name using myAddrPtr->FriendlyName where myAddrPtr is an IP_ADAPTER_ADDRESSES *.

Upvotes: 3

Related Questions