Click here to Skip to main content
13,792,910 members
Click here to Skip to main content
Add your own
alternative version


44 bookmarked
Posted 1 Feb 2001

A C# Server Drop-down Control

, 29 May 2002
Rate this:
Please Sign up or sign in to vote.
A C# usercontrol that enumerates servers and displays the list in a drop-down list
<!-- Link to source file download -->
  • Download source files - 6 Kb
  • <!-- Link to demo file download -->
  • Download demo project - 7 Kb
  • <!-- Article image -->

    Sample Image - ServerDropDown.jpg

    <!-- Add the rest of your HTML here -->


    The sample code below shows the API's and code necessary to enumerate the servers on a domain. The ServerType enum determines the filter for the NetServerEnum call.

    The tricky part (I found) was using the unsafe keyword to actually get the pointer to the array of structures, and then marshalling the pointers to strings (server names) back into managed code. I spent a couple of hours trying to get this to work...but the frustration has paid off.

    The demo project contains a usercontrol that implements the NetServerEnum API. The user of the control then only has to set the ServerType property, and the drop-down will be populated.

    public enum ServerTypeEnum
        steNone = 0,
        steWorkstation = 0x00000001,
        steAll = 0x00000002,
        steSQLServer = 0x00000004,
        steDomainController = 0x00000008
    private static extern void NetApiBufferFree([marshal(UnmanagedType.U4)]uint bufptr);
    unsafe private static extern uint NetServerEnum([marshal(UnmanagedType.LPWStr)] string ServerName, 
            uint level,
            [marshal(UnmanagedType.LPVoid)]uint* bufptr,
            uint prefmaxlen,
            ref uint entriesread,
            ref uint totalentries,
            uint servertype,
            [marshal(UnmanagedType.LPWStr)] string domain, 
            uint resume_handle);
    [System.Runtime.InteropServices.StructLayoutAttribute (LayoutKind.Sequential, CharSet=CharSet.Auto)]
    public struct SERVER_INFO_101 
        public int dwPlatformID; 
        public int lpszServerName;
        public int dwVersionMajor; 
        public int dwVersionMinor; 
        public int dwType; 
        public int lpszComment;
    protected void GetServers()
        string servername = null;
        string domain = "YourDomainName"; 
        uint level = 101, prefmaxlen = 0xFFFFFFFF, entriesread = 0, 
             totalentries = 0, resume_handle = 0; 
            //get a pointer to the server info structure
            SERVER_INFO_101* si = null;
            SERVER_INFO_101* pTmp;    //temp pointer for use when looping 
                                      //through returned (pointer) array
            //this api requires a pointer to a byte array...
            //which is actually a pointer to an array of SERVER_INFO_101 structures
            //If the domain parameter is NULL, the primary domain is implied. 
            uint nRes = NetServerEnum(servername, level, 
                    (uint *) &si, prefmaxlen, ref entriesread, ref totalentries, 
                    (uint)_ServerType, domain, resume_handle);
            if (nRes == 0)
                if ((pTmp = si) != null)    //assign the temp pointer 
                    for (int i = 0; i < entriesread; i++)    //loop through the entries
                            //the lpszServerName member of the structure contains a pointer to 
                            //the server name string...marshal the pointer from unmanaged
                            //memory to a managed string
                        catch (Exception e)
                            MessageBox.Show(e.Message) ;
                        pTmp++;        // increment the pointer...essentially move to the next 
                                       // structure in the array


    30 May 2002 - updated source files


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Marty Cerisano
    United States United States
    No Biography provided

    You may also be interested in...


    Comments and Discussions

    GeneralWhy cant you write Pin
    kaasee6-May-10 3:29
    memberkaasee6-May-10 3:29 
    GeneralTranslation Pin
    kaasee6-May-10 3:23
    memberkaasee6-May-10 3:23 
    QuestionDatabases servers Pin
    Thiago Berti22-Mar-07 5:50
    memberThiago Berti22-Mar-07 5:50 
    Hi there, i downloaded your example, and it worked well in my project.
    But, since I work with MS SQL Server, IBM DB2 and Sybase, I kind of needed to be able to see those servers too.
    Can anybody help me with that?

    Thiago Berti Thomaz
    GeneralI don't convert project... Pin
    Begin Programing7-Sep-06 17:50
    memberBegin Programing7-Sep-06 17:50 
    GeneralI need Help Pin
    CumhurKose12-Nov-04 2:05
    memberCumhurKose12-Nov-04 2:05 
    GeneralTranslation to VB.NET Pin
    DarkKnightWI9-Aug-04 4:11
    memberDarkKnightWI9-Aug-04 4:11 
    GeneralRe: Translation to VB.NET Pin
    Damien Guard4-Nov-04 7:00
    memberDamien Guard4-Nov-04 7:00 
    Generalkaaa-rist! Pin
    3-Jun-02 12:14
    suss3-Jun-02 12:14 
    GeneralRe: kaaa-rist! Pin
    James T. Johnson3-Jun-02 12:54
    memberJames T. Johnson3-Jun-02 12:54 
    Generaltranslation to Final Visual Studio Pin
    dumbatz1-Apr-02 14:10
    memberdumbatz1-Apr-02 14:10 
    GeneralRe: translation to Final Visual Studio Pin
    shilin22-May-02 8:33
    membershilin22-May-02 8:33 
    GeneralRe: translation to Final Visual Studio Pin
    kaasee6-May-10 3:31
    memberkaasee6-May-10 3:31 
    GeneralBeta2 version of the "C# Server Drop-down Control" Pin
    shilin22-Nov-01 8:20
    membershilin22-Nov-01 8:20 
    GeneralRe: Beta2 version of the "C# Server Drop-down Control" Pin
    rbrock038122-Mar-07 12:51
    memberrbrock038122-Mar-07 12:51 
    GeneralCrazy Win32 API function call Pin
    2-Mar-01 7:19
    suss2-Mar-01 7:19 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
    Web01 | 2.8.181207.3 | Last Updated 30 May 2002
    Article Copyright 2001 by Marty Cerisano
    Everything else Copyright © CodeProject, 1999-2018
    Layout: fixed | fluid