public static void DhcpCreateClientInfo(string ServerIpAddress, DhcpClient dhcpClient) { DHCP_CLIENT_INFO clientInfo = ConvertClientInfoToNative(dhcpClient); int error = dhcpsapimethods.DhcpCreateClientInfo(ServerIpAddress, ref clientInfo); if (error != 0) { Console.WriteLine(error); Console.ReadLine(); } }
static ArrayList findDhcpClients(string server, string subnet) { // set up container for processed clients ArrayList foundClients = new ArrayList(); // make call to unmanaged code uint parsedMask = StringIPAddressToUInt32(subnet); uint resumeHandle = 0; uint numClientsRead = 0; uint totalClients = 0; IntPtr info_array_ptr; uint response = DhcpEnumSubnetClients( server, parsedMask, ref resumeHandle, 65536, out info_array_ptr, ref numClientsRead, ref totalClients ); // set up client array casted to a DHCP_CLIENT_INFO_ARRAY // using the pointer from the response object above DHCP_CLIENT_INFO_ARRAY rawClients = (DHCP_CLIENT_INFO_ARRAY)Marshal.PtrToStructure(info_array_ptr, typeof(DHCP_CLIENT_INFO_ARRAY)); // loop through the clients structure inside rawClients // adding to the dchpClient collection IntPtr current = rawClients.Clients; for (int i = 0; i < (int)rawClients.NumElements; i++) { // 1. Create machine object using the struct DHCP_CLIENT_INFO rawMachine = (DHCP_CLIENT_INFO)Marshal.PtrToStructure(Marshal.ReadIntPtr(current), typeof(DHCP_CLIENT_INFO)); // 2. create new C# dhcpClient object and add to the // collection (for hassle-free use elsewhere!!) dhcpClient thisClient = new dhcpClient(); thisClient.ip = UInt32IPAddressToString(rawMachine.ip); thisClient.hostname = rawMachine.ClientName; thisClient.mac = String.Format("{0:x2}{1:x2}.{2:x2}{3:x2}.{4:x2}{5:x2}", Marshal.ReadByte(rawMachine.mac.Data), Marshal.ReadByte(rawMachine.mac.Data, 1), Marshal.ReadByte(rawMachine.mac.Data, 2), Marshal.ReadByte(rawMachine.mac.Data, 3), Marshal.ReadByte(rawMachine.mac.Data, 4), Marshal.ReadByte(rawMachine.mac.Data, 5)); foundClients.Add(thisClient); // 3. move pointer to next machine current = (IntPtr)((int)current + (int)Marshal.SizeOf(typeof(IntPtr))); } return(foundClients); }
public static DHCP_CLIENT_INFO ConvertClientInfoToNative(DhcpClient client) { DHCP_CLIENT_INFO ClientInfo = new DHCP_CLIENT_INFO(); ClientInfo.ClientIpAddress = StringToUint(client.ClientIpAddress); ClientInfo.SubnetMask = StringToUint(client.SubnetMask); ClientInfo.ClientHardwareAddress = ConverToNativeMac(client.ClientHardwareAddress); ClientInfo.ClientName = client.ClientName; ClientInfo.ClientComment = client.ClientComment; ClientInfo.ClientLeaseExpires = ConvertDateTimeToNative(client); DHCP_HOST_INFO host = new DHCP_HOST_INFO(); host.HostName = client.OwnerHost.HostName; host.IpAddress = StringToUint(client.OwnerHost.IpAddress); host.NetBiosName = client.OwnerHost.NetBiosName; ClientInfo.OwnerHost = host; return(ClientInfo); }
public static extern int DhcpCreateClientInfo( string IpAddress, ref DHCP_CLIENT_INFO ClientInfo);
public static DHCP_CLIENT_INFO ConvertClientInfoToNative(DhcpClient client) { DHCP_CLIENT_INFO ClientInfo = new DHCP_CLIENT_INFO(); ClientInfo.ClientIpAddress = StringToUint(client.ClientIpAddress); ClientInfo.SubnetMask = StringToUint(client.SubnetMask); ClientInfo.ClientHardwareAddress = ConverToNativeMac(client.ClientHardwareAddress); ClientInfo.ClientName = client.ClientName; ClientInfo.ClientComment = client.ClientComment; ClientInfo.ClientLeaseExpires = ConvertDateTimeToNative(client); DHCP_HOST_INFO host = new DHCP_HOST_INFO(); host.HostName = client.OwnerHost.HostName; host.IpAddress = StringToUint(client.OwnerHost.IpAddress); host.NetBiosName = client.OwnerHost.NetBiosName; ClientInfo.OwnerHost = host; return ClientInfo; }