Click here to Skip to main content
15,881,709 members
Articles / Desktop Programming / MFC
Article

List Peer Servers and their IP Addresses inside a Workgroup.

Rate me:
Please Sign up or sign in to vote.
1.50/5 (2 votes)
26 Aug 20023 min read 64K   382   21   3
How to list the peer servers and IP addresses in the workgroup - A simple approach.

Sample Image - PeerLister.jpg

Introduction

How can peer servers inside a work group be listed. I wanted to demonstrate a simple method to perform this operation. System.Remoting namespace provides certain APIs using which this can be performed. At CodeProject, I saw a couple of articles performing similar operations in MC++ and they printout the IP addresses too.

The articles are available at these links:

Overview

I chose to use the Shell basics. Using Shell basics, several tasks can be done very simply. The Shell32 DLL is interoped with the project and it exposes a number of methods, classes and interfaces to perform a lot of operations, like using the Windows UI.

The Shell organizes these objects into a hierarchical structure called the namespace, which provides users and applications with a consistent and efficient way to access and manage objects. Users interact with the namespace through the Shell's graphical UI or through an application. Applications interact with the namespace through the Shell's application programming interface (API). This section is an introduction to the Shell API.

There are plenty of objects used to create applications and manage the operating system. The most familiar of these objects are the folders and files that reside on computer disk drives and over networks.

Some of the basics ,how-tos and the APIs used will be explained in this article.

The platform considered is Windows 2000 Server and such a server present under a workgroup server. The requirement is to view the peer servers under the workgroup. Shell basics are used to perform the above said task.

Some Preliminaries

The workgroup when opened using an Explorer, displays the servers that are present in it. One has to understand the differences between:

  1. Folder
  2. NameSpace
  3. FolderItem
  4. Link, etc.

An object of type Shell32.Shell is created and another object of type Shell32.Folder is also created. There are some special folders specified. Such as ShellSpecialFolderConstants.ssfNETWORK represents the Network Places folder. My Documents, Control Panel and several special folders are defined and are identified by separate constants.

The folder is parsed only when isFileSystem return false. The link is opened by using Shell.NameSpace(object) and iterated until the workgroup server folder is reached.

Once the namespace is known, the items() gives a collection, from which an individual item can be retrieved. The code is given below:

C#
Shell32.Shell sh = new Shell32.Shell();
Shell32.Folder currFolder = 
  sh.NameSpace(Shell32.ShellSpecialFolderConstants.ssfNETWORK);
int num = 
  sh.NameSpace(Shell32.ShellSpecialFolderConstants.ssfNETWORK).Items().Count;
MessageBox.Show("The Num of Items in special folder in the local",
  currFolder.Items().Count.ToString());
					
for(int i=0;i<num;i++)
{

 if(currFolder.Items().Item(i).IsFileSystem==false)
 {
  if(currFolder.Items().Item(i).Type.ToString()=="")
  {
   MessageBox.Show(currFolder.Items().Item(i).Name);
   int no =sh.NameSpace(currFolder.Items().Item(i)).Items().Count;
   currFolder = sh.NameSpace(currFolder.Items().Item(i));
   MessageBox.Show(no.ToString());

   MessageBox.Show(currFolder.Items().Item(0).Name+": Cool");
   currFolder=
    sh.NameSpace(sh.NameSpace(currFolder.Items().Item(0)).Items().Item(0));
   MessageBox.Show(currFolder.Items().Count+": Count");

   MessageBox.Show(mycount.ToString());
						
   for(int k=1;k<mycount;k++)
   {
    try
    {

     listView1.Items.Add(new ListViewItem
      (currFolder.Items().Item(k).Name+k.ToString()));

    }
    catch(Exception myexcep)
    {
								
     MessageBox.Show(myexcep.ToString()+" : Error");

    }
   }
  }
 }
}

Update

There is a very simple method to find the IP address of the servers under the workgroup server. These are found in the System.Net namespace. The peer servers are added in to a ListView control as listviewItem. The Net Namespace contains a plethora of classes and APIs for network access. The IP address can be found by passing the domain name of the server.

Sample Image - IP Address.jpg

The giveAddress method returns the IP address of the Server.

C#
this.listView1.DoubleClick += new System.EventHandler(this.giveAddress);

The following is an example of how it is done....

C#
private void giveAddress(object sender, System.EventArgs e)
{
 MessageBox.Show(System.Net.Dns.Resolve(listView1.FocusedItem.Text).
 AddressList.GetValue(0).ToString());
}

The MessageBox displays the IP address of the selected server.

Conclusion

This is an illustration of the use of the Shell basics and a method for how the peers in a workgroup are listed and the IP addresses of the servers found.

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


Written By
Web Developer
United States United States
Ragavendran was born in South India. He holds a Bacnelor of Computer Science and Engineering. Currently he works as a Software Engineer. He is techno Savvy and All he wishes to do in life is to get to know a lotta things....
He is always intrigued about learning new things.

His skills include C,C++,Java ,.NET.He also experience working in Legacy Systems. He feels most comfortable with C++ and Java.
Computers are his first love and he likes reading books, music and trekking.

He is a voracious reader and loves reading books. A Leo by birth, he loves trekking and gardening. One of his main ambitions is to write his autobiography, though he is not sure who will read it.

Comments and Discussions

 
QuestionShell32.Shell sh = new Shell32.Shell(); Pin
Sam Hobbs26-Jan-18 13:37
Sam Hobbs26-Jan-18 13:37 
The line:
Shell32.Shell sh = new Shell32.Shell();
Works in this program but not in programs I write and I have seen questions about that and I could not find a solution until I found this. I can't get it to work in my program. I wish I know what the difference is. I get the following, and others do too.
System.InvalidCastException
  HResult=0x80004002
  Message=Unable to cast COM object of type 'Shell32.ShellClass' to interface type 'Shell32.IShellDispatch6'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{286E6F1B-7113-4355-9562-96B7E9D64C54}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
  Source=mscorlib

Generalerrors Pin
Anonymous2-Nov-02 8:13
Anonymous2-Nov-02 8:13 
GeneralRe: errors Pin
Ragavendran Vaidhyanadhan1-Apr-03 2:02
Ragavendran Vaidhyanadhan1-Apr-03 2:02 

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.