473,488 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

P/Invoke DHCP API

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/DhcpGetClientInfo.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:
Expand|Select|Wrap|Line Numbers
  1. <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
  2. Public Structure DHCP_BINARY_DATA
  3.     Public DataLength As UInteger
  4.     Public Data As IntPtr 'byte array
  5. End Structure
  6.  
  7. <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
  8. Public Structure DHCP_SEARCH_INFO
  9.     Public SearchType As UInteger
  10.     Public ClientIpAddress As UInt32
  11.     Public ClientMacAddress As DHCP_BINARY_DATA
  12.     <MarshalAs(UnmanagedType.LPWStr)> _
  13.     Public ClientName As String
  14. End Structure
  15.  
  16. <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
  17. Public Structure DHCP_CLIENT_INFO
  18.     Public ClientIpAddress As UInteger
  19.     Public SubnetMask As UInteger
  20.     Public ClientHardwareAddress As DHCP_BINARY_DATA
  21.     <MarshalAs(UnmanagedType.LPWStr)> _
  22.     Public ClientName As String
  23.     <MarshalAs(UnmanagedType.LPWStr)> _
  24.     Public ClientComment As String
  25.     Public ClientLeaseExpires As DHCP_DATE_TIME
  26.     Public OwnerHost As DHCP_HOST_INFO
  27. End Structure
  28.  
  29. <StructLayout(LayoutKind.Sequential)> _
  30. Public Structure DHCP_DATE_TIME
  31.     Public dwLowDateTime As UInteger
  32.     Public dwHighDateTime As UInteger
  33. End Structure
  34.  
  35. <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
  36. Structure DHCP_HOST_INFO
  37.     Dim IpAddress As UInteger
  38.     Dim NetBiosName As String
  39.     Dim HostName As String
  40. End Structure
  41.  
  42. Public Enum DHCP_SEARCH_INFO_TYPE
  43.     DhcpClientIpAddress
  44.     DhcpClientHardwareAddress
  45.     DhcpClientName
  46. End Enum
  47.  
  48. ...
  49.  
  50. Dim clientInfo As DHCP_CLIENT_INFO = Marshal.PtrToStructure(hClientInfo, GetType(DHCP_CLIENT_INFO))
  51.  
  52.   clientInfo = GetClientInfo(ServerIP, ClientIP)
  53.   strHostName = clientInfo.ClientName
  54.  
  55. ...
  56.  
  57. <DllImport("C:\Windows\System32\dhcpsapi.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
  58. Public Shared Function DhcpGetClientInfo( _
  59.     ByVal ServerIpAddress As String, _
  60.     ByRef SearchInfo As DHCP_SEARCH_INFO, _
  61.     ByRef ClientInfo As IntPtr) As UInt32
  62. End Function
  63.  
  64. Public Function GetClientInfo(ByVal serverIP As String, ByVal clientIP As String)
  65.     Dim DHCPResult As UInt32 = 0
  66.     Dim ERROR_SUCCESS As UInteger = 0
  67.     Try
  68.         Dim searchInfo As New DHCP_SEARCH_INFO
  69.         Dim searchInfoType As DHCP_SEARCH_INFO_TYPE = DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress
  70.         searchInfo.SearchType = searchInfoType
  71.         searchInfo.ClientIpAddress = ConvertIPAddress(clientIP) 'Converts std IP string into UInt32
  72.         Dim hClientInfo As IntPtr
  73.  
  74.         DHCPResult = DhcpGetClientInfo(serverIP, searchInfo, hClientInfo)
  75.  
  76.         If DHCPResult = ERROR_SUCCESS And Not hClientInfo = IntPtr.Zero Then
  77.             Dim clientInfo As DHCP_CLIENT_INFO = Marshal.PtrToStructure(hClientInfo, GetType(DHCP_CLIENT_INFO))
  78.             Return clientInfo
  79.         End If
  80.         Return Nothing
  81.     Catch ex As Exception
  82.         Console.WriteLine(ex.Message)
  83.         Return Nothing
  84.     End Try
  85. End Function
  86.  
  87.  
  88.  
Nov 8 '10 #1
0 1381

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1280
by: Leszek Gruszka | last post by:
How to get static IP adresses from DHCP?
5
6475
by: Steve | last post by:
Hi Folks, I'm trying to create a c# class to manage a DHCP subnet using the win api (dhcpsapi.dll). Currently I can't even instanciate my class, instead I receive the error:...
3
3282
by: jmd.msdn | last post by:
Hello. Is it possible to obtain programmatically, with .net 2.0 + C# + Active Directory or P/Invoke, a list of the authorized current DHCP servers in a forest/domain ? And, if the above...
0
2814
by: Iziah | last post by:
Hey Guys, I've got a problem in trying to get the DhcpDeleteClientInfo method in the DHCP Server Management API working. I *really* think the issue revolves around my DHCP_SEARCH_INFO_TYPE enum....
2
3022
by: WhatHappend | last post by:
I have converted a .Net 1.0 application to .Net 2.0 and the web service invocations have delay of around 10seconds on each intial access. After the first access subsequent access are fast (After a...
5
7618
by: Richard | last post by:
Hello, I'm working on an application to allow our network team to use a small application to make DHCP reservations on our Microsoft DHCP Server. The problem is you have to use P/Invoke to do...
0
2906
by: GHUM | last post by:
Hello, I need to get a list of active leases on a windows dhcp server. Experts from Microsoft statet: """" A: Go to the Address leases of each scope in the DHCP snap-in and dump the leases...
1
11094
by: Screenbert | last post by:
After finding nothing anywhere in google I am posting this so everyone can benefit by it. The formating is not pretty since I copied it from my word document, but you should benefit by it. ...
0
12313
by: screenbert | last post by:
Managing DHCP Servers using C# They said it was impossible. It couldn't be done. But you can in fact manage DHCP servers using C#. This includes creating and deleting Scopes, SuperScopes,...
7
3624
by: ixeye | last post by:
Anybody else ever had a problem like this? Windows 2003 Domain with 180 machines, 21 of them with XP SP2 Laptop Wireless access to the domain, on three different WIFI AP. Randomly (and luckily...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.