C#调用调用DhcpEnumSubnets,DhcpEnumSubnetClients系统函数写法 各位大哥,在系统中需要调用API函数DhcpEnumSubnets,DhcpEnumSubnetClients,现需要用C#代码实现,高分求此代码? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Sample Code:static void Main() { String ServerIpAddress = "192.168.0.250"; UInt32 DHCPResult = 0; IntPtr ips; uint nr = 0; uint total = 0; uint resumeHandle = 0; DHCPResult = DhcpEnumSubnets(ServerIpAddress, ref resumeHandle, 1000, out ips, ref nr, ref total); if (DHCPResult == 0) { DHCP_IP_ARRAY ipArray = (DHCP_IP_ARRAY)Marshal.PtrToStructure(ips, typeof(DHCP_IP_ARRAY)); int size = (int)ipArray.NumElements; IntPtr outArray = ipArray.IPAddresses; DHCP_IP_ADDRESS[] ipAddressArray = new DHCP_IP_ADDRESS[size]; IntPtr current = outArray; for (int i = 0; i < size; i++) { ipAddressArray[i] = new DHCP_IP_ADDRESS(); Marshal.PtrToStructure(current, ipAddressArray[i]); Marshal.DestroyStructure(current, typeof(DHCP_IP_ADDRESS)); current = (IntPtr)((int)current + Marshal.SizeOf(ipAddressArray[i])); Console.WriteLine("{0}", UInt32IPAddressToString(ipAddressArray[i].IPAddress)); } Marshal.FreeCoTaskMem(outArray); Console.WriteLine("Elements read {0} of total {1}", nr, total); } else { int code = 0; unchecked { code = (int)DHCPResult; } Win32Exception winex = new Win32Exception(code); Console.WriteLine(winex.NativeErrorCode + " : " + winex.Message); } } [DllImport("dhcpsapi.dll", SetLastError = true, CharSet = CharSet.Unicode)] public static extern uint DhcpEnumSubnets( string ServerIpAddress, ref uint ResumeHandle, uint PreferredMaximum, out IntPtr EnumInfo, ref uint ElementsRead, ref uint ElementsTotal ); [StructLayout(LayoutKind.Sequential)] public struct DHCP_IP_ARRAY { public uint NumElements; public IntPtr IPAddresses; } /// This is a custom type/class [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public class DHCP_IP_ADDRESS { public UInt32 IPAddress; } public static string UInt32IPAddressToString(UInt32 ipAddress) { IPAddress ipA = new IPAddress(ipAddress); string[] sIp = ipA.ToString().Split('.'); return sIp[3] + "." + sIp[2] + "." + sIp[1] + "." + sIp[0]; } 感谢二楼提供代码,请问一下,这样就已经取出了DHCP服务器分配IP地址,可是相应IP对应的机器名,以及租用到期,机器MAC地址并没有取到,请求再帮忙提供相关代码? 二楼大哥,根据你的代码可以取出相应的子网IP,但如何根据此子网IP取到该子网下所有客户端的IP,如何调用DhcpEnumSubnetClients此方法? 谢谢 如何向datatable中增加一列 如何在一个外部程序的窗口上添加控件 与网银连接的接口程序是什么类型的程序,如何设计? 如何让窗体中的工具栏组件(下拉列表框)自动调整大小,类似于IE的地址栏文本框 请教,repeater中嵌套checkbox 帮帮忙!急在线等 未处理nullreferenceException的错误! B/S模式和C/S模式进行通信 c# 关于 TcpClient有关的,高手请进(超急!!!!!!) 继承窗体有什么用? 谁有C#中R树的代码 C#执行无参数存储过程代码
static void Main()
{
String ServerIpAddress = "192.168.0.250";
UInt32 DHCPResult = 0; IntPtr ips;
uint nr = 0;
uint total = 0;
uint resumeHandle = 0; DHCPResult = DhcpEnumSubnets(ServerIpAddress, ref resumeHandle, 1000, out ips, ref nr, ref total);
if (DHCPResult == 0)
{ DHCP_IP_ARRAY ipArray = (DHCP_IP_ARRAY)Marshal.PtrToStructure(ips, typeof(DHCP_IP_ARRAY)); int size = (int)ipArray.NumElements;
IntPtr outArray = ipArray.IPAddresses;
DHCP_IP_ADDRESS[] ipAddressArray = new DHCP_IP_ADDRESS[size];
IntPtr current = outArray;
for (int i = 0; i < size; i++)
{
ipAddressArray[i] = new DHCP_IP_ADDRESS();
Marshal.PtrToStructure(current, ipAddressArray[i]);
Marshal.DestroyStructure(current, typeof(DHCP_IP_ADDRESS));
current = (IntPtr)((int)current + Marshal.SizeOf(ipAddressArray[i])); Console.WriteLine("{0}", UInt32IPAddressToString(ipAddressArray[i].IPAddress));
}
Marshal.FreeCoTaskMem(outArray); Console.WriteLine("Elements read {0} of total {1}", nr, total);
}
else
{
int code = 0;
unchecked
{
code = (int)DHCPResult;
}
Win32Exception winex = new Win32Exception(code);
Console.WriteLine(winex.NativeErrorCode + " : " + winex.Message);
}
} [DllImport("dhcpsapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint DhcpEnumSubnets(
string ServerIpAddress,
ref uint ResumeHandle,
uint PreferredMaximum,
out IntPtr EnumInfo,
ref uint ElementsRead,
ref uint ElementsTotal
); [StructLayout(LayoutKind.Sequential)]
public struct DHCP_IP_ARRAY
{
public uint NumElements;
public IntPtr IPAddresses;
} /// This is a custom type/class
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DHCP_IP_ADDRESS
{
public UInt32 IPAddress;
} public static string UInt32IPAddressToString(UInt32 ipAddress)
{
IPAddress ipA = new IPAddress(ipAddress);
string[] sIp = ipA.ToString().Split('.'); return sIp[3] + "." + sIp[2] + "." + sIp[1] + "." + sIp[0];
}