473,324 Members | 2,214 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,324 software developers and data experts.

DHCP Server Management API Functions with P/Invoke

I'm trying to get the number of clients on every subnet using
DhcpEnumSubnetClients on every subnet returned by DhcpEnumSubnets. The
problem is that I don't understand how to declare the .Net wrapper from the
win32 C++ functions.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace DHCPLeases
{
class Program
{
struct DHCP_IP_ARRAY
{
public uint NumElements;
public IntPtr IPAddresses;
}

[DllImport("dhcpsapi.dll")]
private static extern uint DhcpEnumSubnets(
string ServerIP,
ref uint resumeHandle, ulong PerferedMax,
ref DHCP_IP_ARRAY ipAddresses,
ref uint ElementsRead,
ref uint ElementsTotal);

static void Main(string[] args)
{
DHCP_IP_ARRAY ips = new DHCP_IP_ARRAY();
uint nr=0,total=0, resumeHandle =0;
DhcpEnumSubnets("10.100.1.6",ref resumeHandle , 1000, ref ips,
ref nr,ref total);
}
}
}

Throws:
PInvokeStackImbalance was detected
Message: A call to PInvoke function
'DHCPLeases!DHCPLeases.Program::DhcpEnumSubnets' has unbalanced the stack.
This is likely because the managed PInvoke signature does not match the
unmanaged target signature. Check that the calling convention and parameters
of the PInvoke signature match the target unmanaged signature.

Please help.
Apr 19 '06 #1
2 3846
> [DllImport("dhcpsapi.dll")]
private static extern uint DhcpEnumSubnets(
string ServerIP,
ref uint resumeHandle, ulong PerferedMax,
ref DHCP_IP_ARRAY ipAddresses,
ref uint ElementsRead,
ref uint ElementsTotal);


PreferredMax should be an uint.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 19 '06 #2
Hi, Mattias and thanks for your help!

changing PreferredMatx to an uint kind of solved the problem. The wrapper
declaration now seemes to be ok, but I'm not getting any subnets in response.

class Program
{
struct DHCP_IP_ARRAY
{
public uint NumElements;
public IntPtr IPAddresses;
}

[DllImport("dhcpsapi.dll")]
private static extern uint DhcpEnumSubnets(
string ServerIP,
ref uint resumeHandle, uint PerferedMax,
ref DHCP_IP_ARRAY ipAddresses,
ref uint ElementsRead,
ref uint ElementsTotal);

static void Main(string[] args)
{
DHCP_IP_ARRAY ips = new DHCP_IP_ARRAY();
uint nr=0,total=0, resumeHandle =0;

DhcpEnumSubnets("10.100.1.1", ref resumeHandle, 1000, ref ips,
ref nr, ref total);
Console.WriteLine("Elements read {0} of total {1}", nr, total);

}
}

"Mattias Sjögren" wrote:
[DllImport("dhcpsapi.dll")]
private static extern uint DhcpEnumSubnets(
string ServerIP,
ref uint resumeHandle, ulong PerferedMax,
ref DHCP_IP_ARRAY ipAddresses,
ref uint ElementsRead,
ref uint ElementsTotal);


PreferredMax should be an uint.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Apr 20 '06 #3

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...
3
by: Leszek Gruszka | last post by:
How to get static IP adresses from DHCP?
5
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:...
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,...
0
cyberking
by: cyberking | last post by:
Hi all, Firstly, I am very little knowledgible when it comes to Windows OS (Client OS or Server OS). But offlate my urge to get myself in terms with it increased and soon I was meddling with the...
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
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.