DhcpGetClientInfo (dhcpsapi)
Last changed: -137.246.199.200

.
Summary
The DhcpGetClientInfo function returns information about a specific DHCP client.

C# Signature:

/// <summary>
/// The DhcpGetClientInfo function returns information about a specific DHCP client.
/// </summary>
/// <param name="ServerIpAddress">[in] Unicode string that specifies the IP address of the DHCP server.</param>
/// <param name="SearchInfo">[in] DHCP_SEARCH_INFO structure that contains the parameters for the search. </param>
/// <param name="ClientInfo">[out] Pointer to a DHCP_CLIENT_INFO structure that contains information describing the DHCP client that most closely matches the provided search parameters. If no client is found, this parameter will be null.</param>
/// <returns>This function returns ERROR_SUCCESS upon a successful call. Otherwise, it returns one of the DHCP Server Management API Error Codes.</returns>

[DllImport("dhcpsapi.dll")]

        static extern UInt32 DhcpGetClientInfo(
            [MarshalAs(UnmanagedType.LPWStr)]
            String ServerIpAddress,
            ref DHCP_CLIENT_SEARCH_IP_ADDRESS si,
            out IntPtr ci);

VB Signature:

     ''' <summary>
    ''' The DhcpGetClientInfo function returns information about a specific DHCP client.
    ''' </summary>
    ''' <param name="ServerIpAddress">[in] Unicode string that specifies the IP address of the DHCP server.
    ''' </param>
    ''' <param name="SearchInfo">[in] DHCP_SEARCH_INFO structure that contains the parameters for the search.
    ''' </param>
    ''' <param name="ClientInfo">[out] Pointer to a DHCP_CLIENT_INFO structure that contains information describing the DHCP client that most closely matches the provided search parameters. If no client is found, this parameter will be null.
    ''' </param>
    ''' <returns>This function returns ERROR_SUCCESS upon a successful call. Otherwise, it returns one of the DHCP Server Management API Error Codes.
    ''' </returns>
    <DllImport("dhcpsapi.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Public Shared Function DhcpGetClientInfo( _
        ByVal ServerIpAddress As String, _
        ByRef SearchInfo As DHCP_SEARCH_INFO, _
        ByRef ClientInfo As IntPtr) As UInt32
    End Function

User-Defined Types:

DHCP_SEARCH_INFO

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

VB Signature and sample code added by BlUNt, adapted from C# Samples

Tips & Tricks:

Please add some!

Sample VB Code:

    Public Function GetClientInfo(ByVal serverIP As String, ByVal clientIP As String)
        Dim DHCPResult As UInt32 = 0
        Try
        Dim searchInfo As New DHCP_SEARCH_INFO
        Dim searchInfoType As DHCP_SEARCH_INFO_TYPE = DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress
        searchInfo.SearchType = searchInfoType
        searchInfo.ClientIpAddress = ConvertIPAddress(clientIP)
        Dim hClientInfo As IntPtr

        DHCPResult = DhcpGetClientInfo(serverIP, searchInfo, hClientInfo)

        If DHCPResult = ERROR_SUCCESS And Not hClientInfo = IntPtr.Zero Then
            Dim clientInfo As DHCP_CLIENT_INFO = Marshal.PtrToStructure(hClientInfo, GetType(DHCP_CLIENT_INFO))
            Return clientInfo
        End If
        Return Nothing
        Catch ex As Exception
        Console.WriteLine(ex.Message)
        Return Nothing
        End Try
    End Function

    Public Shared Function ConvertIPAddress(ByVal ipAddress As String) As UInt32
        Dim dot As Char() = ".".ToCharArray()
        Dim zero As Char() = "0".ToCharArray()
        Dim Octets As String() = ipAddress.Split(dot(0))
        Dim HexIP As String = ""

        For Each Octet As String In Octets
        HexIP += Convert.ToString(Convert.ToInt16(Octet), 16).PadLeft(2, zero(0))
        Next
        Return Convert.ToUInt32(HexIP, 16)
    End Function

Sample Code:

static void Main()
    {
        String ServerIpAddress = "192.168.0.250";

        UInt32 DHCPResult = 0;
        try
        {

        DHCP_SEARCH_INFO searchInfo = new DHCP_SEARCH_INFO();
        DHCP_SEARCH_INFO_TYPE searchInfoType = DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress;

        searchInfo.SearchType = searchInfoType;
        searchInfo.ClientIpAddress = ConvertIPAddress("192.168.0.10");

        IntPtr pClientInfo;
        DHCPResult = DhcpGetClientInfo(ServerIpAddress, ref searchInfo, out pClientInfo);
        if (DHCPResult == 0 && pClientInfo != IntPtr.Zero)
        {
            DHCP_CLIENT_INFO clientInfo = (DHCP_CLIENT_INFO)Marshal.PtrToStructure(pClientInfo, typeof(DHCP_CLIENT_INFO));
            Console.WriteLine(DHCPResult.ToString() + " Client Info: " + clientInfo.ClientName);
        }
        else
        {
            Console.WriteLine(DHCPResult.ToString() + " Failed");
        }
    }

    public static UInt32 ConvertIPAddress(string ipAddress)
    {
        char[] dot = ".".ToCharArray();
        char[] zero = "0".ToCharArray();
        string[] Octets = ipAddress.Split(dot[0]);
        string HexIP = "";

        foreach (string Octet in Octets)
        {
        HexIP += Convert.ToString(Convert.ToInt16(Octet), 16).PadLeft(2, zero[0]);
        }
        return Convert.ToUInt32(HexIP, 16);
    }

Documentation