473,797 Members | 3,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.TypeLoad Exception. 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 6504

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


A "TypeLoadExcept ion" 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_DAT A structure defines an opaque blob of binary data.
[StructLayout(La youtKind.Sequen tial)]
public struct DHCP_BINARY_DAT A
{
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(La youtKind.Sequen tial)]
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(La youtKind.Sequen tial)]
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_INF O structure defines a client information record
used by the DHCP server.
[StructLayout(La youtKind.Sequen tial)]
public struct DHCP_CLIENT_INF O
{
public UInt32 ClientIpAddress ;
public UInt32 SubnetMask;
public DHCP_BINARY_DAT A ClientHardwareA ddress;
public StringBuilder ClientName;
public StringBuilder ClientComment;
public DATE_TIME ClientLeaseExpi res;
public DHCP_HOST_INFO OwnerHost;
};

[DllImport("dhcp sapi.dll")]
static extern UInt32 CreateClientInf o(char[] ServerIpAddress ,
DHCP_CLIENT_INF O ci);

[DllImport("dhcp sapi.dll")]
static extern UInt32 DeleteClientInf o(char[] ServerIpAddress ,
DHCP_CLIENT_INF O 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.goo glegroups.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_DAT A structure defines an opaque blob of binary data.
[StructLayout(La youtKind.Sequen tial)]
public struct DHCP_BINARY_DAT A
{
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(La youtKind.Sequen tial)]
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(La youtKind.Sequen tial)]
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_INF O structure defines a client information record
used by the DHCP server.
[StructLayout(La youtKind.Sequen tial)]
public struct DHCP_CLIENT_INF O
{
public UInt32 ClientIpAddress ;
public UInt32 SubnetMask;
public DHCP_BINARY_DAT A ClientHardwareA ddress;
public StringBuilder ClientName;
public StringBuilder ClientComment;
public DATE_TIME ClientLeaseExpi res;
public DHCP_HOST_INFO OwnerHost;
};

[DllImport("dhcp sapi.dll")]
static extern UInt32 CreateClientInf o(char[] ServerIpAddress ,
DHCP_CLIENT_INF O ci);

[DllImport("dhcp sapi.dll")]
static extern UInt32 DeleteClientInf o(char[] ServerIpAddress ,
DHCP_CLIENT_INF O 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_INF O 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_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;

typedef struct _DHCP_CLIENT_IN FO {
DHCP_IP_ADDRESS ClientIpAddress ;
DHCP_IP_MASK SubnetMask;
DHCP_CLIENT_UID ClientHardwareA ddress;
LPWSTR ClientName;
LPWSTR ClientComment;
DATE_TIME ClientLeaseExpi res;
DHCP_HOST_INFO OwnerHost;
} DHCP_CLIENT_INF O,
*LPDHCP_CLIENT_ INFO;

DWORD DHCP_API_FUNCTI ON DhcpGetClientIn fo(
DHCP_CONST WCHAR* ServerIpAddress ,
DHCP_CONST DHCP_SEARCH_INF O* SearchInfo,
LPDHCP_CLIENT_I NFO* ClientInfo
);

and now for the c#:

[DllImport("dhcp sapi.dll")]
static extern UInt32
DhcpGetClientIn fo([MarshalAs(Unman agedType.LPWStr )]String
ServerIpAddress , ref DHCP_CLIENT_SEA RCH_IP_ADDRESS si, ref
DHCP_CLIENT_INF O ci);

public enum DHCP_SEARCH_INF O_TYPE: int
{
DhcpClientIpAdd ress=0,
DhcpClientHardw areAddress=1,
DhcpClientName= 2
}

[StructLayout(La youtKind.Sequen tial)]
public struct DHCP_CLIENT_SEA RCH_IP_ADDRESS
{
public int SearchType;
public UInt32 ClientIpAddress ;
};

[StructLayout(La youtKind.Sequen tial,CharSet=Ch arSet.Unicode)]
public struct DHCP_CLIENT_INF O
{
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;
};

public UInt32 GetClientInfoBy IpAddress(strin g ServerIpAddress ,string
ClientIpAddress )
{
DHCP_CLIENT_SEA RCH_IP_ADDRESS SearchInfo = new
DHCP_CLIENT_SEA RCH_IP_ADDRESS( );

SearchInfo.Sear chType = (int)DHCP_SEARC H_INFO_TYPE.Dhc pClientIpAddres s;
SearchInfo.Clie ntIpAddress = ConvertIPString ToNumeric(Clien tIpAddress);

DHCP_CLIENT_INF O ClientInfo = new DHCP_CLIENT_INF O();

UInt32 DHCPResult=0;
try
{
DHCPResult = DhcpGetClientIn fo(new
String(ServerIp Address.ToCharA rray()),
ref SearchInfo, ref ClientInfo);
}
catch (Exception ex)
{
throw new Exception("Erro r searching for client lease:
"+ex.Message+"\ nError code: "+DHCPResult.To String(),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
2148
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 and DN server Has someone some ideas Thanks in advance, Marco
5
4813
by: KarlM | last post by:
Is there any sample code for making a DHCP reservation via C#? Thanks in advance
0
2826
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. Eventually what I want to get done is a delete from the DHCP server by MAC Address. The code below works for IPAddress, but not Name or MACAddress. both of those attempts yield the same error: "87: the parameter is incorrect." Have a look and...
2
3054
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 whild of inactivity the next access will be slow agian). Changing the client to static IP address instead of DHCP makes the initial access about 1 second which is fine. Monitoring the network of the client computer the cleint is sending out 2...
5
7655
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 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
0
2971
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 to a text file from the DHCP server snap-in. The text file gives you all the information for every active lease. You might also
1
11150
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. 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, Reservations, Exclusions, Options and so forth. Since the dll...
0
12361
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, Reservations, Exclusions, Options and so forth. Since the dll that is used was written several years ago by Microsoft, you cannot manage things on the DNS tab when looking at a reservation and few things such as that however it gives you main things you...
4
4034
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, get the hostname of the computer, and thus locate the offender (the computers are given a hostname that includes the classroom number). However, often when we look up an IP address, the hostname does not appear, and we suspect some of the students have...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10468
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9063
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7559
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5458
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.