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 it, and from what I've
found on the web and in this newsgroup is that it's not easy, however I
believe it can/has been done.
At the moment I'm just trying to find out information about an existing
reservation but I'm getting stuck. I read the newsgroup article
'P/Invoke with DHCP management API' which got me started but the last
comment made on 2006-01-01 didn't make sense to me and I can't get the
damn thing to work, in my test console app.
Here is the code I am using, could someone point me in the right
direction please?:
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Runtime. InteropServices ;
namespace DHCP
{
class Program
{
static void Main(string[] args)
{
String ServerIpAddress = "192.168.0.250" ;
UInt32 DHCPResult = 0;
try
{
DhcpApi.DHCP_SE ARCH_INFO searchInfo = new
DhcpApi.DHCP_SE ARCH_INFO();
DhcpApi.DHCP_SE ARCH_INFO_TYPE searchInfoType =
DhcpApi.DHCP_SE ARCH_INFO_TYPE. DhcpClientIpAdd ress;
searchInfo.Sear chType = searchInfoType;
searchInfo.Clie ntIpAddress =
DhcpApi.Convert IPAddress("192. 168.0.5");
DhcpApi.DHCP_CL IENT_INFO clientInfo = new
DhcpApi.DHCP_CL IENT_INFO();
DHCPResult = DhcpApi.DhcpGet ClientInfo(Serv erIpAddress,
ref searchInfo, ref clientInfo);
Console.WriteLi ne(DHCPResult.T oString() + " Client
Info: " + clientInfo.Clie ntName);
}
catch (Exception ex)
{
Console.WriteLi ne("*********** *************** *************** *************** *************** *****");
Console.WriteLi ne("ERROR: \t" + DHCPResult.ToSt ring());
Console.WriteLi ne("MESSAGE: \t" + ex.Message);
Console.WriteLi ne();
Console.WriteLi ne("FULL DETAILS:");
Console.WriteLi ne(ex.ToString( ));
Console.WriteLi ne("*********** *************** *************** *************** *************** *****");
}
Console.ReadKey (true);
}
}
public class DhcpApi
{
/// <summary>
/// The DhcpGetClientIn fo function returns information about a
specific DHCP client.
/// </summary>
/// <param name="ServerIpA ddress">[in] Unicode string that
specifies the IP address of the DHCP server.</param>
/// <param name="SearchInf o">[in] DHCP_SEARCH_INF O structure
that contains the parameters for the search. </param>
/// <param name="ClientInf o">[out] Pointer to a
DHCP_CLIENT_INF O 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("dhcp sapi.dll", SetLastError = true,CharSet =
CharSet.Unicode )]
public static extern UInt32
DhcpGetClientIn fo([MarshalAs(Unman agedType.LPWStr )]String
ServerIpAddress , ref DHCP_SEARCH_INF O SearchInfo, ref DHCP_CLIENT_INF O
ClientInfo);
/// <summary>
/// The DHCP_SEARCH_INF O_TYPE enumeration defines the set of
possible
/// attributes used to search DHCP client information records.
/// </summary>
public enum DHCP_SEARCH_INF O_TYPE : int
{
/// <summary>
/// The search will be performed against the assigned DHCP
client IP address, represented as a 32-bit unsigned integer value.
/// </summary>
DhcpClientIpAdd ress = 0,
/// <summary>
/// The search will be performed against the MAC address of
the DHCP client network interface device, represented as a
DHCP_BINARY_DAT A structure.
/// </summary>
DhcpClientHardw areAddress = 1,
/// <summary>
/// The search will be performed again the DHCP client's
network name, represented as a Unicode string.
/// </summary>
DhcpClientName = 2
}
/// <summary>
/// The DHCP_SEARCH_INF O structure defines the DHCP client record
/// data used to search against for particular server operations.
/// </summary>
[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Unicode )]
public struct DHCP_SEARCH_INF O
{
// typedef struct _DHCP_CLIENT_SE ARCH_INFO {
// DHCP_SEARCH_INF O_TYPE SearchType;
// union {
// DHCP_IP_ADDRESS ClientIpAddress ;
// DHCP_CLIENT_UID ClientHardwareA ddress;
// LPWSTR ClientName;
// } SearchInfo;
//} DHCP_SEARCH_INF O, *LPDHCP_SEARCH_ INFO;
public DHCP_SEARCH_INF O_TYPE SearchType;
public UInt32 ClientIpAddress ;
}
/// <summary>
/// The DHCP_CLIENT_INF O structure defines a client information
record
/// used by the DHCP server.
/// </summary>
[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Unicode )]
public struct DHCP_CLIENT_INF O
{
//
*************** *************** *************** *************** *************** *
// DHCP_IP_ADDRESS Specifies an IP address, with the first
byte containing
// the first number in the address, the second byte
containing the second
// number, the third byte containing the third number, and
the last byte
// containing the last number in the address. For example,
the address
// 192.1.1.10 is represented as 11000000 00000001 00000001
00001010 (binary),
// or 3221291274 (decimal).
//
*************** *************** *************** *************** *************** *
public UInt32 ClientIpAddress ;
public UInt32 SubnetMask;
public DHCP_BINARY_DAT A ClientHardwareA ddress;
[MarshalAs(Unman agedType.LPWStr )]
public String ClientName;
[MarshalAs(Unman agedType.LPWStr )]
public String ClientComment;
public DATE_TIME ClientLeaseExpi res;
public DHCP_HOST_INFO OwnerHost;
}
}
}
I'm also hoping to implement the following API functions in the future,
if anyone has any pointers on these as well! ;o):
DhcpGetClientIn fo
DhcpDeleteClien tInfo
DhcpCreateClien tInfo
DhcpSetClientIn fo
DhcpEnumSubnetC lients
Cheers,
Richard W.
--
Google First. Ask Later.
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 it, and from what I've
found on the web and in this newsgroup is that it's not easy, however I
believe it can/has been done.
At the moment I'm just trying to find out information about an existing
reservation but I'm getting stuck. I read the newsgroup article
'P/Invoke with DHCP management API' which got me started but the last
comment made on 2006-01-01 didn't make sense to me and I can't get the
damn thing to work, in my test console app.
Here is the code I am using, could someone point me in the right
direction please?:
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Runtime. InteropServices ;
namespace DHCP
{
class Program
{
static void Main(string[] args)
{
String ServerIpAddress = "192.168.0.250" ;
UInt32 DHCPResult = 0;
try
{
DhcpApi.DHCP_SE ARCH_INFO searchInfo = new
DhcpApi.DHCP_SE ARCH_INFO();
DhcpApi.DHCP_SE ARCH_INFO_TYPE searchInfoType =
DhcpApi.DHCP_SE ARCH_INFO_TYPE. DhcpClientIpAdd ress;
searchInfo.Sear chType = searchInfoType;
searchInfo.Clie ntIpAddress =
DhcpApi.Convert IPAddress("192. 168.0.5");
DhcpApi.DHCP_CL IENT_INFO clientInfo = new
DhcpApi.DHCP_CL IENT_INFO();
DHCPResult = DhcpApi.DhcpGet ClientInfo(Serv erIpAddress,
ref searchInfo, ref clientInfo);
Console.WriteLi ne(DHCPResult.T oString() + " Client
Info: " + clientInfo.Clie ntName);
}
catch (Exception ex)
{
Console.WriteLi ne("*********** *************** *************** *************** *************** *****");
Console.WriteLi ne("ERROR: \t" + DHCPResult.ToSt ring());
Console.WriteLi ne("MESSAGE: \t" + ex.Message);
Console.WriteLi ne();
Console.WriteLi ne("FULL DETAILS:");
Console.WriteLi ne(ex.ToString( ));
Console.WriteLi ne("*********** *************** *************** *************** *************** *****");
}
Console.ReadKey (true);
}
}
public class DhcpApi
{
/// <summary>
/// The DhcpGetClientIn fo function returns information about a
specific DHCP client.
/// </summary>
/// <param name="ServerIpA ddress">[in] Unicode string that
specifies the IP address of the DHCP server.</param>
/// <param name="SearchInf o">[in] DHCP_SEARCH_INF O structure
that contains the parameters for the search. </param>
/// <param name="ClientInf o">[out] Pointer to a
DHCP_CLIENT_INF O 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("dhcp sapi.dll", SetLastError = true,CharSet =
CharSet.Unicode )]
public static extern UInt32
DhcpGetClientIn fo([MarshalAs(Unman agedType.LPWStr )]String
ServerIpAddress , ref DHCP_SEARCH_INF O SearchInfo, ref DHCP_CLIENT_INF O
ClientInfo);
/// <summary>
/// The DHCP_SEARCH_INF O_TYPE enumeration defines the set of
possible
/// attributes used to search DHCP client information records.
/// </summary>
public enum DHCP_SEARCH_INF O_TYPE : int
{
/// <summary>
/// The search will be performed against the assigned DHCP
client IP address, represented as a 32-bit unsigned integer value.
/// </summary>
DhcpClientIpAdd ress = 0,
/// <summary>
/// The search will be performed against the MAC address of
the DHCP client network interface device, represented as a
DHCP_BINARY_DAT A structure.
/// </summary>
DhcpClientHardw areAddress = 1,
/// <summary>
/// The search will be performed again the DHCP client's
network name, represented as a Unicode string.
/// </summary>
DhcpClientName = 2
}
/// <summary>
/// The DHCP_SEARCH_INF O structure defines the DHCP client record
/// data used to search against for particular server operations.
/// </summary>
[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Unicode )]
public struct DHCP_SEARCH_INF O
{
// typedef struct _DHCP_CLIENT_SE ARCH_INFO {
// DHCP_SEARCH_INF O_TYPE SearchType;
// union {
// DHCP_IP_ADDRESS ClientIpAddress ;
// DHCP_CLIENT_UID ClientHardwareA ddress;
// LPWSTR ClientName;
// } SearchInfo;
//} DHCP_SEARCH_INF O, *LPDHCP_SEARCH_ INFO;
public DHCP_SEARCH_INF O_TYPE SearchType;
public UInt32 ClientIpAddress ;
}
/// <summary>
/// The DHCP_CLIENT_INF O structure defines a client information
record
/// used by the DHCP server.
/// </summary>
[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Unicode )]
public struct DHCP_CLIENT_INF O
{
//
*************** *************** *************** *************** *************** *
// DHCP_IP_ADDRESS Specifies an IP address, with the first
byte containing
// the first number in the address, the second byte
containing the second
// number, the third byte containing the third number, and
the last byte
// containing the last number in the address. For example,
the address
// 192.1.1.10 is represented as 11000000 00000001 00000001
00001010 (binary),
// or 3221291274 (decimal).
//
*************** *************** *************** *************** *************** *
public UInt32 ClientIpAddress ;
public UInt32 SubnetMask;
public DHCP_BINARY_DAT A ClientHardwareA ddress;
[MarshalAs(Unman agedType.LPWStr )]
public String ClientName;
[MarshalAs(Unman agedType.LPWStr )]
public String ClientComment;
public DATE_TIME ClientLeaseExpi res;
public DHCP_HOST_INFO OwnerHost;
}
}
}
I'm also hoping to implement the following API functions in the future,
if anyone has any pointers on these as well! ;o):
DhcpGetClientIn fo
DhcpDeleteClien tInfo
DhcpCreateClien tInfo
DhcpSetClientIn fo
DhcpEnumSubnetC lients
Cheers,
Richard W.
--
Google First. Ask Later.
Comment