DHCP Server Management API Functions with P/Invoke

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
[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
 
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 said:
[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
 
Back
Top