locked
WinRT: DhcpRequestParams: Getting adapter name

    Question

  • How to retrieve the adapter name to be used when calling DhcpRequestParams.

    There is NetworkAdapter class but it does not have any property for its name.

    Thanks,

    Rafi

    Sunday, September 01, 2013 11:16 AM

Answers

All replies

  • Hi Rafi ,

    Welcome to MSDN forum!

    The following example illustrates how to retrieve the host name using the DhcpRequestParams function call. The name of the adapter can be retrieved using the GetInterfaceInfo structure, which is part of the Internet Protocol Helper API:

    #include <windows.h>
    #include <dhcpcsdk.h>
    #pragma comment( lib, "dhcpcsvc.lib" )
    BOOL RetrieveHostName(
        IN LPWSTR     pszAdapterName,
        IN OUT CHAR   pszHostNameBuf[], // must be large enough buffer
        IN DWORD      dwHostNameBufSize
    )
    /*++
    Routine returns TRUE on success and FALSE on failure.
    --*/
    {
        DWORD dwError, dwSize;
        CHAR TmpBuffer[1000]; // host name won't be larger than this
        DHCPCAPI_PARAMS DhcpApiHostNameParams = {
                0,                // Flags
                OPTION_HOST_NAME, // OptionId
                FALSE,            // vendor specific?
                NULL,             // data filled in on return
                0                 // nBytes
            }; 
        DHCPCAPI_PARAMS_ARRAY RequestParams = {
                1,  // only one option to request 
                &DhcpApiHostNameParams
            };
        DHCPCAPI_PARAMS_ARRAY SendParams = {
                0,   
                NULL
            };
        dwSize = sizeof(TmpBuffer);
        dwError = DhcpRequestParams(
                DHCPCAPI_REQUEST_SYNCHRONOUS, // Flags
                NULL,                         // Reserved
                pszAdapterName,               // Adapter Name
                NULL,                         // not using class id
                   SendParams,                         // sent parameters
                RequestParams,                // requesting params
                (PBYTE) TmpBuffer,            // buffer
                &dwSize,                      // buffer size
                NULL                          // Request ID
            );
        if( ERROR_MORE_DATA == dwError ) 
        {
                //
                // dwSize is not large enough.
                //
        }
        if( NO_ERROR == dwError ) 
        {
                // Check if the requested option was obtained.
                if( DhcpApiHostNameParams.nBytesData ) 
                {
                    // Check size with dwHostNameBufSize.
                    CopyMemory(
                         pszHostNameBuf, DhcpApiHostNameParams.Data,
                         DhcpApiHostNameParams.nBytesData
                         );
                    pszHostNameBuf[DhcpApiHostNameParams.nBytesData] = '\0';
                    return TRUE;
                }
        }
        return FALSE;
    }
    Best Regards!

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 02, 2013 8:49 AM
    Moderator
  • The question was for WinRT environment. Does the GetInterfaceInfo API works for WinRT (Windows 8, Windows Store Apps) ?

    Regards,

    Rafi

    Monday, September 02, 2013 9:21 AM
  • Hi Rafi,

    Welcome back!

    I think GetAdaptersInfo function is the best for you, please just have a try.

    http://msdn.microsoft.com/en-us/library/windows/desktop/aa365915(v=vs.85).aspx

    Best Regards!


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 02, 2013 10:04 AM
    Moderator