Click here to Skip to main content
6,595,444 members and growing! (17,904 online)
Email Password   helpLost your password?
Desktop Development » Combo & List Boxes » General     Intermediate

A C# Server Drop-down Control

By Marty Cerisano

A C# usercontrol that enumerates servers and displays the list in a drop-down list
C#, VC7.NET 1.0, Win2K, Visual Studio, Dev
Posted:1 Feb 2001
Updated:29 May 2002
Views:108,622
Bookmarked:38 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 4.21 Rating: 3.58 out of 5

1

2
1 vote, 25.0%
3
2 votes, 50.0%
4
1 vote, 25.0%
5
  • Download source files - 6 Kb
  • Download demo project - 7 Kb
  • Sample Image - ServerDropDown.jpg

    Introduction

    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
    }
    
    [sysimport(dll="netapi32.dll")]
    private static extern void NetApiBufferFree([marshal(UnmanagedType.U4)]uint bufptr);
    
    [sysimport(dll="netapi32.dll")] 
    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; 
        
        cboServers.Items.Clear();
    
        unsafe
        {    
            //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
    
                    {
                        try
                        {
                            //the lpszServerName member of the structure contains a pointer to 
    
                            //the server name string...marshal the pointer from unmanaged
    
                            //memory to a managed string
    
                            cboServers.Items.Add(Marshal.PtrToStringAuto(pTmp->lpszServerName));
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message) ;
                        }
                        pTmp++;        // increment the pointer...essentially move to the next 
    
                                       // structure in the array
    
                    }
                }
            }
            NetApiBufferFree((uint)si);
        }             
    }
    

    History

    30 May 2002 - updated source files

    License

    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


    Member

    Location: United States United States

    Other popular Combo & List Boxes articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
    QuestionDatabases servers PinmemberThiago Berti5:50 22 Mar '07  
    GeneralI don't convert project... PinmemberBegin Programing17:50 7 Sep '06  
    GeneralI need Help Pinmembercumhur.kose@msn.com2:05 12 Nov '04  
    GeneralTranslation to VB.NET PinmemberDarkKnightWI4:11 9 Aug '04  
    GeneralRe: Translation to VB.NET PinmemberDamien Guard7:00 4 Nov '04  
    Generalkaaa-rist! PinmemberAnonymous12:14 3 Jun '02  
    GeneralRe: kaaa-rist! PinsubeditorJames T. Johnson12:54 3 Jun '02  
    Generaltranslation to Final Visual Studio Pinmemberdumbatz14:10 1 Apr '02  
    GeneralRe: translation to Final Visual Studio PinmemberShilin8:33 22 May '02  
    GeneralBeta2 version of the "C# Server Drop-down Control" PinmemberShilin8:20 22 Nov '01  
    GeneralRe: Beta2 version of the "C# Server Drop-down Control" Pinmemberrbrock038112:51 22 Mar '07  
    GeneralCrazy Win32 API function call PinmemberDimon aka Manowar7:19 2 Mar '01  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 29 May 2002
    Editor: Chris Maunder
    Copyright 2001 by Marty Cerisano
    Everything else Copyright © CodeProject, 1999-2009
    Web21 | Advertise on the Code Project