I am attempting to write an app that uses dhcpsapi.dll interface to get client computer name from our DHCP server cased on client IP address. I am basing the code on http://www.pinvoke.net/default.aspx/dhcpsapi/DhcpGetClientIn fo.html. There is no termination error when I run it, but when I trace the program, I show that dhcpsapi is returning a code of 20013 (DHCP Jet Database error). I am unable to find any reference to the error either on the DHCP server's event logs or the client machine event logs.
Hoping somebody has some insight into what is happening. Related code below:
Hoping somebody has some insight into what is happening. Related code below:
Code:
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Structure DHCP_BINARY_DATA
Public DataLength As UInteger
Public Data As IntPtr 'byte array
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Structure DHCP_SEARCH_INFO
Public SearchType As UInteger
Public ClientIpAddress As UInt32
Public ClientMacAddress As DHCP_BINARY_DATA
<MarshalAs(UnmanagedType.LPWStr)> _
Public ClientName As String
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Structure DHCP_CLIENT_INFO
Public ClientIpAddress As UInteger
Public SubnetMask As UInteger
Public ClientHardwareAddress As DHCP_BINARY_DATA
<MarshalAs(UnmanagedType.LPWStr)> _
Public ClientName As String
<MarshalAs(UnmanagedType.LPWStr)> _
Public ClientComment As String
Public ClientLeaseExpires As DHCP_DATE_TIME
Public OwnerHost As DHCP_HOST_INFO
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure DHCP_DATE_TIME
Public dwLowDateTime As UInteger
Public dwHighDateTime As UInteger
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Structure DHCP_HOST_INFO
Dim IpAddress As UInteger
Dim NetBiosName As String
Dim HostName As String
End Structure
Public Enum DHCP_SEARCH_INFO_TYPE
DhcpClientIpAddress
DhcpClientHardwareAddress
DhcpClientName
End Enum
...
Dim clientInfo As DHCP_CLIENT_INFO = Marshal.PtrToStructure(hClientInfo, GetType(DHCP_CLIENT_INFO))
clientInfo = GetClientInfo(ServerIP, ClientIP)
strHostName = clientInfo.ClientName
...
<DllImport("C:\Windows\System32\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
Public Function GetClientInfo(ByVal serverIP As String, ByVal clientIP As String)
Dim DHCPResult As UInt32 = 0
Dim ERROR_SUCCESS As UInteger = 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) 'Converts std IP string into UInt32
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