473,385 Members | 1,282 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,385 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 6470

"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,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.