Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

DhcpEnumServers (dhcpsapi)
 
.
Summary
Enumerate directory server for DHCP servers. Works with Active Directory.

C# Signature:

    [DllImport("dhcpsapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern uint DhcpEnumServers(
        uint Flags,
        ref uint IdInfo,
        out IntPtr Servers,
        ref uint CallbackFn,
        ref uint CallbackData
    );
    )

VB Signature:

Declare Function DhcpEnumServers Lib "dhcpsapi.dll" (TODO) As TODO

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Completed library DHCP functions.

http://www.rupj.net/portfolio/dhcp-web-services.html

Sample Code:

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

Documentation

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).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions