Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

DhcpGetClientInfo (dhcpsapi)
 
.

&@msdn=http://search.microsoft.com/search/results.aspx?qu=$$$

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="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, DHCP_CLIENT_SEARCH_IP_ADDRESS

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!

Completed library DHCP functions.

http://www.rupj.net/portfolio/dhcp-web-services.html

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);
    }

C# Sample for getting a MAC-Address to a specified IP

public string GetClientInfoByIp(string server, string client)
{
    DHCP_CLIENT_SEARCH_IP_ADDRESS si = new DHCP_CLIENT_SEARCH_IP_ADDRESS();
    si.SearchType = (int)DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress ;
    si.ClientIpAddress = StringIPAddressToUint32(client);

    UInt32 res = 0;
    try
    {
        IntPtr oInfo;
        res = DhcpGetClientInfo(new String(server.ToCharArray()), ref si, out oInfo);

        if (res == 0 && oInfo != IntPtr.Zero)
        {
            DHCP_CLIENT_INFO info = (DHCP_CLIENT_INFO)Marshal.PtrToStructure(oInfo, typeof(DHCP_CLIENT_INFO));
            String mac = String.Format("{0:x2}-{1:x2}-{2:x2}-{3:x2}-{4:x2}-{5:x2}",
                Marshal.ReadByte(info.ClientHardwareAddress.Data,0),
                Marshal.ReadByte(info.ClientHardwareAddress.Data,1),
                Marshal.ReadByte(info.ClientHardwareAddress.Data,2),
                Marshal.ReadByte(info.ClientHardwareAddress.Data,3),
                Marshal.ReadByte(info.ClientHardwareAddress.Data,4),
                Marshal.ReadByte(info.ClientHardwareAddress.Data,5));

            return mac;
        }
        else
        {
            Console.WriteLine("Failed!");
            return "00-00-00-00-00-00";
        }
    }
    catch(Exception e)
    {
        Console.WriteLine(e);
        return "00-00-00-00-00-00";
    }
}

public uint StringIPAddressToUint32(string ip_string)
{
    IPAddress ipa = IPAddress.Parse(ip_string);
    byte[] ip_bytes = ipa.GetAddressBytes();

    uint ip_uint = (uint)ip_bytes[0] << 24;
    ip_uint+= (uint)ip_bytes[1] << 16;
    ip_uint+= (uint)ip_bytes[2] << 8;
    ip_uint+= (uint)ip_bytes[3];

    return ip_uint;
}

static void Main()
{
    String dhcpServerIP = "192.168.1.1";
    String clientIP = "182.168.1.120";

    Console.WriteLine("MAC-Adress for " + clientIp + " is " + GetClientInfoByIp(dhcpServerIP, clientIp));
}

Documentation
[DhcpGetClientInfo] on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions