各位大哥,在系统中需要调用API函数DhcpEnumSubnets,DhcpEnumSubnetClients,现需要用C#代码实现,高分求此代码?

解决方案 »

  1.   

    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]; 
      

  2.   

    感谢二楼提供代码,请问一下,这样就已经取出了DHCP服务器分配IP地址,可是相应IP对应的机器名,以及租用到期,机器MAC地址并没有取到,请求再帮忙提供相关代码?
      

  3.   

    二楼大哥,根据你的代码可以取出相应的子网IP,但如何根据此子网IP取到该子网下所有客户端的IP,如何调用DhcpEnumSubnetClients此方法? 谢谢