IP_ADAPTER_INFO : AdapterName is not the name!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: IP_ADAPTER_INFO : AdapterName is not the name!

  1. #1
    Join Date
    May 2010
    Posts
    4

    IP_ADAPTER_INFO : AdapterName is not the name!

    Hey all,

    Coding in C++ using VS 2008.

    So I noticed today that IP_ADAPTER_INFO:AdapterName is not the name as the documentation indicates, but rather, it's a GUID. I need the name (not the Description, which I'm also retrieving).

    Ethernet adapter Local Area Connection:

    Description . . . . . . . . . . . : Intel(R) 82562V-2 10/100 Network Connection

    Is there a way to get the adapter name?

    Cheers!

  2. #2
    Join Date
    Nov 2003
    Posts
    1,787

    Re: IP_ADAPTER_INFO : AdapterName is not the name!

    In the past, I've pulled it form registry:

    SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\<adapter GUID>\Connection\Name

    http://support.microsoft.com/kb/314053

    gg

  3. #3
    Join Date
    May 2010
    Posts
    4

    Re: IP_ADAPTER_INFO : AdapterName is not the name!

    Is there any other way besides combing the registry? I tried the WMI solution (gross) and using Win32_NetworkAdapter.Name just gives me the Description and Win32_NetworkAdapterConfiguration.ServiceName just gives me, well, the service name. Neither is the adapter's friendly name.

    Any thoughts on other WMI classes to use or am I pretty much pooched as far as this approach goes?

  4. #4
    Join Date
    Nov 2003
    Posts
    1,787

    Re: IP_ADAPTER_INFO : AdapterName is not the name!

    It's Win32_NetworkAdapter.NetConnectionID
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    gg

  5. #5
    Join Date
    May 2010
    Posts
    4

    Re: IP_ADAPTER_INFO : AdapterName is not the name!

    Quote Originally Posted by Codeplug View Post
    It's Win32_NetworkAdapter.NetConnectionID
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    gg
    I will give that another shot, thank you.

    In the meanwhile, I have been trying another approach and it's giving me the friendly name, however, I'm now stuck trying to get the ip address and mac address.

    I'm using GetAdaptersAddresses which seems to pull all the required info, however, when I tried to view the IP address (p_adapter->FirstUnicastAddress->Address.lpSockaddr->sa_data), I see a blank string. OTOH, if I follow the example in MSDN help, I am informed there are two addresses in the FirstUnicastAddress list. More or less the same happens when I view p_adapter->PhysicalAddress, I just get a blank string. I've tried googling for other examples of using GetAdaptersAddresses but am not turning up anything useful that will let me extract the IP and MAC addresses. Can someone please point me to some sample code that better illustrates extracting IP addresses from the results?

  6. #6
    Join Date
    Nov 2003
    Posts
    1,787

    Re: IP_ADAPTER_INFO : AdapterName is not the name!

    >> p_adapter->FirstUnicastAddress->Address.lpSockaddr
    You need to cast that pointer to a "(struct sockaddr_in*)" for IPV4. The "sin_addr" member will have the IP address (but not as a string).

    >> More or less the same happens when I view p_adapter->PhysicalAddress, I just get a blank string.
    It's not a string. It's an array of bytes. The MSDN example code shows how to display it.

    gg

  7. #7
    Join Date
    May 2010
    Posts
    4

    Re: IP_ADAPTER_INFO : AdapterName is not the name!

    Quote Originally Posted by Codeplug View Post
    >> p_adapter->FirstUnicastAddress->Address.lpSockaddr
    You need to cast that pointer to a "(struct sockaddr_in*)" for IPV4. The "sin_addr" member will have the IP address (but not as a string).

    >> More or less the same happens when I view p_adapter->PhysicalAddress, I just get a blank string.
    It's not a string. It's an array of bytes. The MSDN example code shows how to display it.

    gg
    Yeah, I'd missed that in the MSDN example (the MAC address). They really don't make it easy to get at the data, do they? Thank you for the reply.

    I also found another page that finally allowed me to the get the IP address. Here's my final code which is a combination of the two:

    // Get the IP address.
    PIP_ADAPTER_UNICAST_ADDRESS pUnicastAddress = p_adapter->FirstUnicastAddress;
    while (pUnicastAddress)
    {
    dwAddressLen = 128;
    memset(szAddress, 0, sizeof(szAddress));

    // Handle IPV4 addresses only at this time.
    if (pUnicastAddress->Address.lpSockaddr->sa_family == AF_INET)
    {
    WSAAddressToStringA(
    pUnicastAddress->Address.lpSockaddr,
    pUnicastAddress->Address.iSockaddrLength,
    NULL,
    szAddress,
    &dwAddressLen);

    pAi[iii].m_sIP = szAddress;
    pAi[iii].m_bIP = inet_addr(szAddress);
    // Stop searching after the first address is found.
    break;
    }
    pUnicastAddress = pUnicastAddress->Next;
    }

    // Get the MAC address.
    memset(szAddress, 0, sizeof(szAddress));
    for (int i = 0; i < (int) p_adapter->PhysicalAddressLength; i++)
    {
    if (i == (p_adapter->PhysicalAddressLength - 1))
    {
    sprintf(szAddress, "&#37;s%.2X\n", szAddress, (int)p_adapter->PhysicalAddress[i]);
    }
    else
    {
    sprintf(szAddress, "%s%.2X:", szAddress, (int)p_adapter->PhysicalAddress[i]);
    }
    }
    pAi[iii].m_Mac = szAddress;

    The page I was referring to:

    http://www.winsocketdotnetworkprogra...nction13e.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center