Click here to Skip to main content
12,546,448 members (55,384 online)
Click here to Skip to main content
Add your own
alternative version


63 bookmarked

Network computer picker control

, 15 Jul 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
A Windows class library for selecting networked computers.


A Windows class library for selecting networked computers.


While having authored several Windows Forms apps, I've frequently needed to browse the network specifically for selecting a computer, however there is no managed method to accomplish this. I was inspired by Michael Potter's Finding SQL Servers on the Network article, so I decided to take it a little further and allow more granular control of the types of computers one can select.

Enumerating Computers

The first thing we have to do is define the function which will perform this task for us. NetServerEnum is located in the NetApi32.dll library.

// enumerates network computers
[DllImport("Netapi32", CharSet=CharSet.Unicode)]
private static extern int NetServerEnum( 
    string servername,        // must be null
    int level,        // 100 or 101
    out IntPtr bufptr,        // pointer to buffer receiving data
    int prefmaxlen,        // max length of returned data
    out int entriesread,    // num entries read
    out int totalentries,    // total servers + workstations
    uint servertype,        // server type filter
    string domain,        // domain to enumerate
    IntPtr resume_handle );

The third parameter of NetServerEnum will populate a structure containing information about the computers it finds. With the exception of sv101_platform_id & sv101_type, these values are exposed as public properties in the NetworkComputers struct.

// Holds computer information
[StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
internal struct SERVER_INFO_101
    public int sv101_platform_id;
    public string sv101_name;
    public int sv101_version_major;
    public int sv101_version_minor;
    public int sv101_type;
    public string sv101_comment;

To get our list, we simply call one of the CompEnum constructors and pass it one of the ServerType values

ce = new CompEnum(CompEnum.ServerType.SV_TYPE_DOMAIN_CTRL | 
or it's equivalent bit-mapped value (GetServerTypeValues performs this step for us)
private void GetServerTypeValues(object sender, System.EventArgs e)
    int filterVal = 0x00;
    bool itemsChecked = false;
    foreach (CheckBox cb in groupBoxServerTypes.Controls)
        if (cb.Checked)
            filterVal += Int32.Parse((string)cb.Tag,
            itemsChecked = true;

    checkBoxAll.Enabled = !itemsChecked;

Now we can enumerate through our collection of computers.

internal void DisplayComputerTypes(uint serverType)
    Cursor.Current = Cursors.WaitCursor;
    ce = new CompEnum(serverType, cbDomainList.SelectedItem.ToString());
    int numServer = ce.Length;
    if (ce.LastError.Length == 0)
        IEnumerator enumerator = ce.GetEnumerator();

        int i = 0;
        while (enumerator.MoveNext())

Enumerating SQL Servers Using SQL-DMO

Short for SQL Server Distributed Management Objects, SQL-DMO is a far more reliable way to retrieve the names of SQL Servers on your network. My code first performs a check to see if SQL-DMO is possible, otherwise it uses the regular NetServerEnum API.

private void GetSqlServersUsingSQLDMO(object sender, System.EventArgs e)
    SQLDMO.Application app = new SQLDMO.ApplicationClass();
    SQLDMO.NameList nameList = app.ListAvailableSQLServers();
    string srvName = "";
    _sqlServerList = new string[nameList.Count];

    for (int i=0; i<nameList.Count; i++)
        srvName = nameList.Item(i + 1);
        _sqlServerList[i] = srvName;

ListAvailableSQLServers() returns a NameList object that enumerates SQL Server names. From here we use the Item method of NameList to retrieve the actual server name. That's all there is to it.

Sample Usage

CompPicker cp = new CompPicker();

// show selected computer
if (cp.ShowDialog(this) == DialogResult.OK)
    MessageBox.Show(this, cp.SelectedComputerName);


To extend the library to fit your needs, simply add or remove checkboxes in the groupBoxServerTypes group box and place their tag values equal to one of the ServerType bitmap values. These values are originally defined in LMServer.h and can also be found in the CompEnum class.

Performance Issues

Performance is hampered in a non-domain environment, presumably because there is no browse master defined? The same holds true when browsing in a different domain than the one you are currently in.


Since the NetServerEnum WinAPI is only available for Windows NT or greater, it will not work on Win9x machines.


  • Version 1.0 - 06.01.2004 - First release version.
  • Version 1.1 - 07.15.2004 - Utilize SQL-DMO for enumerating SQL Servers, if available.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Marc Merritt
United States United States
I live in beautiful Arizona, USA.

You may also be interested in...


Comments and Discussions

QuestionIs it depend on Win 7 Computer browser service? Pin
chenthooran1-Mar-13 0:32
memberchenthooran1-Mar-13 0:32 
GeneralWorks the first couple of times then no computers are returned. Pin
pipipip8-Dec-09 13:38
memberpipipip8-Dec-09 13:38 
QuestionGet computer IP address Pin
Radu_2020-Apr-08 5:03
memberRadu_2020-Apr-08 5:03 
GeneralRe: Get computer IP address Pin
Marc Merritt22-Apr-08 7:35
memberMarc Merritt22-Apr-08 7:35 
QuestionGetSqlServersUsingSQLDMO return only one Computer Pin
Umer Khan30-Sep-07 21:48
memberUmer Khan30-Sep-07 21:48 
AnswerRe: GetSqlServersUsingSQLDMO return only one Computer Pin
Marc Merritt1-Oct-07 15:08
memberMarc Merritt1-Oct-07 15:08 
QuestionRe: GetSqlServersUsingSQLDMO return only one Computer Pin
Umer Khan2-Oct-07 5:43
memberUmer Khan2-Oct-07 5:43 
GeneralError 6118 Pin
tkotia13-Mar-07 10:11
membertkotia13-Mar-07 10:11 
GeneralRe: Error 6118 Pin
Marc Merritt28-Jun-07 16:26
memberMarc Merritt28-Jun-07 16:26 
GeneralThank you very much for this article Pin
H. S. Masud7-May-06 3:22
memberH. S. Masud7-May-06 3:22 
GeneralRe: Thank you very much for this article Pin
Marc Merritt7-May-06 14:58
memberMarc Merritt7-May-06 14:58 
GeneralThank you Pin
shawn_b4-Mar-06 17:33
membershawn_b4-Mar-06 17:33 
GeneralOh, it's perfect Pin
TastoEsc24-Jan-06 23:20
memberTastoEsc24-Jan-06 23:20 
GeneralThanks Pin
Bret Williams4-May-05 5:17
memberBret Williams4-May-05 5:17 
GeneralAn SQLDMO exception Pin
RandyY11-Jan-05 16:32
memberRandyY11-Jan-05 16:32 
QuestionCan I implement This program in WinCE Pin
The illiterate29-Oct-04 20:15
memberThe illiterate29-Oct-04 20:15 
AnswerRe: Can I implement This program in WinCE Pin
Marc Merritt5-Nov-04 2:29
memberMarc Merritt5-Nov-04 2:29 
Generalcan't see xp computers Pin
choiceplus27-Oct-04 10:10
memberchoiceplus27-Oct-04 10:10 
GeneralRe: can't see xp computers Pin
Marc Merritt5-Nov-04 2:21
memberMarc Merritt5-Nov-04 2:21 
GeneralRe: can't see xp computers Pin
choiceplus5-Nov-04 3:00
memberchoiceplus5-Nov-04 3:00 
GeneralIt has unhandled exceptions Pin
jrpally30-Jun-04 9:35
memberjrpally30-Jun-04 9:35 
GeneralRe: It has unhandled exceptions Pin
Marc Merritt1-Jul-04 10:25
memberMarc Merritt1-Jul-04 10:25 
Generalnice Pin
Vladimir Ralev9-Jun-04 10:59
memberVladimir Ralev9-Jun-04 10:59 
GeneralLarge Domains Pin
netclectic7-Jun-04 23:52
membernetclectic7-Jun-04 23:52 
GeneralRe: Large Domains Pin
Marc Merritt8-Jun-04 2:17
memberMarc Merritt8-Jun-04 2:17 
GeneralEnumerating MSDE Sql Servers Pin
vbnetuk7-Jun-04 1:50
membervbnetuk7-Jun-04 1:50 
GeneralRe: Enumerating MSDE Sql Servers Pin
reflex@codeproject7-Jun-04 11:33
memberreflex@codeproject7-Jun-04 11:33 
GeneralRe: Enumerating MSDE Sql Servers Pin
vbnetuk14-Jun-04 21:21
membervbnetuk14-Jun-04 21:21 
GeneralRe: Enumerating MSDE Sql Servers Pin
Marc Merritt15-Jun-04 5:06
memberMarc Merritt15-Jun-04 5:06 
GeneralRe: Enumerating MSDE Sql Servers Pin
vbnetuk15-Jun-04 22:18
membervbnetuk15-Jun-04 22:18 
GeneralRe: Enumerating MSDE Sql Servers Pin
Marc Merritt16-Jun-04 4:52
memberMarc Merritt16-Jun-04 4:52 
GeneralRe: Enumerating MSDE Sql Servers Pin
Marc Merritt15-Jul-04 8:26
memberMarc Merritt15-Jul-04 8:26 
GeneralRe: Enumerating MSDE Sql Servers Pin
tupacs014-Nov-04 17:05
membertupacs014-Nov-04 17:05 
GeneralRe: Enumerating MSDE Sql Servers Pin
tupacs017-Sep-04 7:33
membertupacs017-Sep-04 7:33 
GeneralRe: Enumerating MSDE Sql Servers Pin
Luis Abreu4-Nov-04 12:05
memberLuis Abreu4-Nov-04 12:05 
GeneralRe: Enumerating MSDE Sql Servers Pin
tupacs014-Nov-04 16:45
membertupacs014-Nov-04 16:45 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161018.1 | Last Updated 16 Jul 2004
Article Copyright 2004 by Marc Merritt
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid