473,288 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

P/Invoke with DHCP management API

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: System.TypeLoadException. Does
anybody have experience working with this API from C# or know of some
documentation/examples? Thanks in advance. steve.

Dec 15 '05 #1
5 6459

"Steve" <st**************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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: System.TypeLoadException. Does
anybody have experience working with this API from C# or know of some
documentation/examples? Thanks in advance. steve.


A "TypeLoadException" is not directly related to this API, could you please
post your code?

Willy.


Dec 15 '05 #2
here's a link to the API I'm trying to use:
http://msdn.microsoft.com/library/de...clientinfo.asp

Here's the structures and static api methods from my c# class:

//The DHCP_BINARY_DATA structure defines an opaque blob of binary data.
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_BINARY_DATA
{
public UInt32 DataLength; //Specifies the size of Data, in bytes
public byte[] Data; //Pointer to an opaque blob of byte (binary)
data
}

//The DHCP_HOST_INFO structure defines information on a DHCP server
(host).
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_HOST_INFO
{
public UInt32 IpAddress; //DHCP_IP_ADDRESS value that contains the
IP address of the DHCP server.
public StringBuilder NetBiosName; //Unicode string that contains the
Netbios name of the DHCP server.
public StringBuilder HostName; //Unicode string that contains the
network name of the DHCP server.
}

// The DATE_TIME structure defines a 64-bit integer value that
contains a date/time, expressed
// as the number of ticks (100-nanosecond increments) since 12:00
midnight, January 1, 1 C.E. in
// the Gregorian calendar.
[StructLayout(LayoutKind.Sequential)]
public struct DATE_TIME
{
public UInt32 dwLowDateTime; //Specifies the lower 32 bits of the
time value.
public UInt32 dwHighDateTime; //Specifies the upper 32 bits of the
time value
}

//The DHCP_CLIENT_INFO structure defines a client information record
used by the DHCP server.
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_CLIENT_INFO
{
public UInt32 ClientIpAddress;
public UInt32 SubnetMask;
public DHCP_BINARY_DATA ClientHardwareAddress;
public StringBuilder ClientName;
public StringBuilder ClientComment;
public DATE_TIME ClientLeaseExpires;
public DHCP_HOST_INFO OwnerHost;
};

[DllImport("dhcpsapi.dll")]
static extern UInt32 CreateClientInfo(char[] ServerIpAddress,
DHCP_CLIENT_INFO ci);

[DllImport("dhcpsapi.dll")]
static extern UInt32 DeleteClientInfo(char[] ServerIpAddress,
DHCP_CLIENT_INFO ci);
#endregion

Dec 16 '05 #3
A bunch of API declarations is of little help here, what I wan't is the call
that throws and the callstack.

Willy.

"Steve" <st**************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
here's a link to the API I'm trying to use:
http://msdn.microsoft.com/library/de...clientinfo.asp

Here's the structures and static api methods from my c# class:

//The DHCP_BINARY_DATA structure defines an opaque blob of binary data.
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_BINARY_DATA
{
public UInt32 DataLength; //Specifies the size of Data, in bytes
public byte[] Data; //Pointer to an opaque blob of byte (binary)
data
}

//The DHCP_HOST_INFO structure defines information on a DHCP server
(host).
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_HOST_INFO
{
public UInt32 IpAddress; //DHCP_IP_ADDRESS value that contains the
IP address of the DHCP server.
public StringBuilder NetBiosName; //Unicode string that contains the
Netbios name of the DHCP server.
public StringBuilder HostName; //Unicode string that contains the
network name of the DHCP server.
}

// The DATE_TIME structure defines a 64-bit integer value that
contains a date/time, expressed
// as the number of ticks (100-nanosecond increments) since 12:00
midnight, January 1, 1 C.E. in
// the Gregorian calendar.
[StructLayout(LayoutKind.Sequential)]
public struct DATE_TIME
{
public UInt32 dwLowDateTime; //Specifies the lower 32 bits of the
time value.
public UInt32 dwHighDateTime; //Specifies the upper 32 bits of the
time value
}

//The DHCP_CLIENT_INFO structure defines a client information record
used by the DHCP server.
[StructLayout(LayoutKind.Sequential)]
public struct DHCP_CLIENT_INFO
{
public UInt32 ClientIpAddress;
public UInt32 SubnetMask;
public DHCP_BINARY_DATA ClientHardwareAddress;
public StringBuilder ClientName;
public StringBuilder ClientComment;
public DATE_TIME ClientLeaseExpires;
public DHCP_HOST_INFO OwnerHost;
};

[DllImport("dhcpsapi.dll")]
static extern UInt32 CreateClientInfo(char[] ServerIpAddress,
DHCP_CLIENT_INFO ci);

[DllImport("dhcpsapi.dll")]
static extern UInt32 DeleteClientInfo(char[] ServerIpAddress,
DHCP_CLIENT_INFO ci);
#endregion

Dec 16 '05 #4
I've been fighting with this a little more in my free time. I've made
some progress such that I can create a lease now. I'm currently
struggling with two issues that I know of. I'll mention the first
issue for now.

Getting a structure back from a win api call. i've tried different
variations of the following code but nothing works. The function call
does not return an error result or throw an exception so I'm stuck.
The DHCP_CLIENT_INFO structure passed back from GetClientInfo always
contains only null values. I'm having the same issue with other
functions to I'm assuming that I'm not managing output parameters
properly.

Here's are the c++ functions:

typedef struct _DHCP_CLIENT_SEARCH_INFO {
DHCP_SEARCH_INFO_TYPE SearchType;
union {
DHCP_IP_ADDRESS ClientIpAddress;
DHCP_CLIENT_UID ClientHardwareAddress;
LPWSTR ClientName;
} SearchInfo;
} DHCP_SEARCH_INFO,
*LPDHCP_SEARCH_INFO;

typedef struct _DHCP_CLIENT_INFO {
DHCP_IP_ADDRESS ClientIpAddress;
DHCP_IP_MASK SubnetMask;
DHCP_CLIENT_UID ClientHardwareAddress;
LPWSTR ClientName;
LPWSTR ClientComment;
DATE_TIME ClientLeaseExpires;
DHCP_HOST_INFO OwnerHost;
} DHCP_CLIENT_INFO,
*LPDHCP_CLIENT_INFO;

DWORD DHCP_API_FUNCTION DhcpGetClientInfo(
DHCP_CONST WCHAR* ServerIpAddress,
DHCP_CONST DHCP_SEARCH_INFO* SearchInfo,
LPDHCP_CLIENT_INFO* ClientInfo
);

and now for the c#:

[DllImport("dhcpsapi.dll")]
static extern UInt32
DhcpGetClientInfo([MarshalAs(UnmanagedType.LPWStr)]String
ServerIpAddress, ref DHCP_CLIENT_SEARCH_IP_ADDRESS si, ref
DHCP_CLIENT_INFO ci);

public enum DHCP_SEARCH_INFO_TYPE: int
{
DhcpClientIpAddress=0,
DhcpClientHardwareAddress=1,
DhcpClientName=2
}

[StructLayout(LayoutKind.Sequential)]
public struct DHCP_CLIENT_SEARCH_IP_ADDRESS
{
public int SearchType;
public UInt32 ClientIpAddress;
};

[StructLayout(LayoutKind.Sequential,CharSet=CharSet .Unicode)]
public struct DHCP_CLIENT_INFO
{
public UInt32 ClientIpAddress;
public UInt32 SubnetMask;
public DHCP_BINARY_DATA ClientHardwareAddress;
[MarshalAs(UnmanagedType.LPWStr)]
public String ClientName;
[MarshalAs(UnmanagedType.LPWStr)]
public String ClientComment;
public DATE_TIME ClientLeaseExpires;
public DHCP_HOST_INFO OwnerHost;
};

public UInt32 GetClientInfoByIpAddress(string ServerIpAddress,string
ClientIpAddress)
{
DHCP_CLIENT_SEARCH_IP_ADDRESS SearchInfo = new
DHCP_CLIENT_SEARCH_IP_ADDRESS();

SearchInfo.SearchType = (int)DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress;
SearchInfo.ClientIpAddress = ConvertIPStringToNumeric(ClientIpAddress);

DHCP_CLIENT_INFO ClientInfo = new DHCP_CLIENT_INFO();

UInt32 DHCPResult=0;
try
{
DHCPResult = DhcpGetClientInfo(new
String(ServerIpAddress.ToCharArray()),
ref SearchInfo, ref ClientInfo);
}
catch (Exception ex)
{
throw new Exception("Error searching for client lease:
"+ex.Message+"\nError code: "+DHCPResult.ToString(),ex);
}

// ClientInfo values are all null or empty here!!
return DHCPResult;
}

Dec 31 '05 #5
I figured it out. I needed to use an IntPtr for out parameters and
then marshal the structure. On to the next function... boy it takes a
long time to write code to call a few win32 functions! Somebody should
write a .net assembly with the common win32 functions and sell it.

steve.

Jan 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marco | last post by:
Hi to all I've a simple question...It's possible to configure and manage DHCP and DNS serve via Visual Basic .NET I need to create a simple application in Visual Basic that allow to manage DHCP...
5
by: KarlM | last post by:
Is there any sample code for making a DHCP reservation via C#? Thanks in advance
0
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
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
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
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
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
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,...
4
by: kvickers | last post by:
I work with a school system, and we have a web filter box where we can monitor students' internet usage. When we find abuse, we look up the IP address of the abuser in the DHCP management console,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.