Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than dhcpsapi, prefix the name with the module name and a period.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DHCPDS_SERVER
{
public UInt32 Version;
[MarshalAs(UnmanagedType.LPWStr)]
public string ServerName;
public UInt32 ServerAddress;
public UInt32 Flags;
public UInt32 State;
[MarshalAs(UnmanagedType.LPWStr)]
public string DsLocation;
public UInt32 DsLocType;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DHCP_SERVER_INFO_ARRAY
{
public UInt32 Flags;
public UInt32 NumElements;
public IntPtr Servers;
}
// This is a custom type/class
public struct DHCP_SERVERS
{
public string ServerName;
public string ServerAddress;
}
public DHCP_SERVERS[] GetDHCPServers()
{
UInt32 DHCPResult = 0;
uint nr = 0;
IntPtr svrs;
DHCPResult = DhcpEnumServers(nr, ref nr, out svrs, ref nr, ref nr);
if (DHCPResult == 0)
{
DHCP_SERVER_INFO_ARRAY dsArray = (DHCP_SERVER_INFO_ARRAY)Marshal.PtrToStructure(svrs, typeof(DHCP_SERVER_INFO_ARRAY));
int size = (int)dsArray.NumElements;
IntPtr outArray = dsArray.Servers;
DHCPDS_SERVER[] serverList = new DHCPDS_SERVER[size];
DHCP_SERVERS[] outlist = new DHCP_SERVERS[size];
IntPtr current = outArray;
for (int i = 0; i < size; i++)
{
serverList[i] = new DHCPDS_SERVER();
Marshal.PtrToStructure(current, serverList[i]);
Marshal.DestroyStructure(current, typeof(DHCPDS_SERVER));
current = (IntPtr)((int)current + Marshal.SizeOf(serverList[i]));
outlist[i].ServerName = serverList[i].ServerName;
outlist[i].ServerAddress = UInt32IPAddressToString(serverList[i].ServerAddress);
}
Marshal.FreeCoTaskMem(outArray);
return outlist;
}
return null; // no server found
}
private string UInt32IPAddressToString(uint ipAddress)
{
IPAddress ipA = new IPAddress(ipAddress);
string[] sIp = ipA.ToString().Split('.');
return sIp[3] + "." + sIp[2] + "." + sIp[1] + "." + sIp[0];
}
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).