Click here to Skip to main content
15,914,642 members
Home / Discussions / C#
   

C#

 
Question[Message Deleted] Pin
anithagaraga21-Oct-08 21:40
anithagaraga21-Oct-08 21:40 
QuestionRe: Computer Names from the Server System Pin
Eddy Vluggen21-Oct-08 21:54
professionalEddy Vluggen21-Oct-08 21:54 
AnswerRe: Computer Names from the Server System Pin
anithagaraga21-Oct-08 22:06
anithagaraga21-Oct-08 22:06 
AnswerRe: Computer Names from the Server System Pin
Eddy Vluggen21-Oct-08 22:48
professionalEddy Vluggen21-Oct-08 22:48 
GeneralRe: Computer Names from the Server System Pin
anithagaraga21-Oct-08 23:00
anithagaraga21-Oct-08 23:00 
GeneralRe: Computer Names from the Server System Pin
Eddy Vluggen21-Oct-08 23:11
professionalEddy Vluggen21-Oct-08 23:11 
GeneralRe: Computer Names from the Server System Pin
anithagaraga21-Oct-08 23:25
anithagaraga21-Oct-08 23:25 
GeneralRe: Computer Names from the Server System Pin
Eddy Vluggen22-Oct-08 0:13
professionalEddy Vluggen22-Oct-08 0:13 
anithagaraga wrote:
Sorry to Use the word Urgent

Ah, lots of people with questions tend to add the phrase "urgent". We're not being paid, so we don't like to be rushed..

I downloaded the source-code that was attached to the article, and have modified it to work in a web-environment. The modifications are simple; added two using-directives and removed exception-handling. Also, I added the NetworkBrowser-class to the code-file of the ASP.NET page. The rest of the code hasn't been modified.

The code below is a simple webpage, which uses the code from the aforementioned article;

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Runtime.InteropServices;
using System.Security;

public partial class t_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
NetworkBrowser nb = new NetworkBrowser();
foreach (string pc in nb.getNetworkComputers())
{
ListBox1.Items.Add(pc);
}
}
}

#region NetworkBrowser CLASS
///
/// Provides a mechanism for supplying a list of all PC names in the local network.
/// This collection of PC names is used in the <see cref="ListNetworkComputers.frmMain">
/// form
///


/// This class makes use of a DllImport instruction. The purpose of which is as follows:
/// When a DllImport declaration is made in managed code (C#) it is a call to a legacy
/// unmanaged code module, normally a C++ Dynamic Link Library. These C++ Dll's are
/// usually part of the operating system API, or some other vendors API, and must be
/// used to carry out operations that are not native within the managed code C# framework.
/// This is fairly normal within the windows world. The only thing that needs careful consideration
/// is the construction of the correct type of STRUCTS, object pointers, and attribute markers,
/// which all contribute to making the link between managed (C#) and unmanaged code (C++)
/// more seamless
///


/// This class makes use of the following Dll calls
/// <list type="bullet">/// <item>
/// <description> Netapi32.dll : NetServerEnum, The NetServerEnum function lists all servers
/// of the specified type that are visible in a domain. For example, an application can call
/// NetServerEnum to list all domain controllers only or all SQL servers only.
/// You can combine bit masks to list several types. For example, a value of 0x00000003
/// combines the bit masks for SV_TYPE_WORKSTATION (0x00000001) and SV_TYPE_SERVER (0x00000002).
///
///
/// <item>
/// <description> Netapi32.dll : NetApiBufferFree, The NetApiBufferFree function frees
/// the memory that the NetApiBufferAllocate function allocates. Call NetApiBufferFree
/// to free the memory that other network management functions return.
///
/// ///

public sealed class NetworkBrowser
{

#region Dll Imports

//declare the Netapi32 : NetServerEnum method import
[DllImport("Netapi32", CharSet = CharSet.Auto, SetLastError = true),
SuppressUnmanagedCodeSecurityAttribute]

///
/// Netapi32.dll : The NetServerEnum function lists all servers
/// of the specified type that are visible in a domain. For example, an
/// application can call NetServerEnum to list all domain controllers only
/// or all SQL servers only.
/// You can combine bit masks to list several types. For example, a value
/// of 0x00000003 combines the bit masks for SV_TYPE_WORKSTATION
/// (0x00000001) and SV_TYPE_SERVER (0x00000002)
///

public static extern int NetServerEnum(
string ServerNane, // must be null
int dwLevel,
ref IntPtr pBuf,
int dwPrefMaxLen,
out int dwEntriesRead,
out int dwTotalEntries,
int dwServerType,
string domain, // null for login domain
out int dwResumeHandle
);

//declare the Netapi32 : NetApiBufferFree method import
[DllImport("Netapi32", SetLastError = true),
SuppressUnmanagedCodeSecurityAttribute]

///
/// Netapi32.dll : The NetApiBufferFree function frees
/// the memory that the NetApiBufferAllocate function allocates.
/// Call NetApiBufferFree to free the memory that other network
/// management functions return.
///

public static extern int NetApiBufferFree(
IntPtr pBuf);

//create a _SERVER_INFO_100 STRUCTURE
[StructLayout(LayoutKind.Sequential)]
public struct _SERVER_INFO_100
{
internal int sv100_platform_id;
[MarshalAs(UnmanagedType.LPWStr)]
internal string sv100_name;
}
#endregion
#region Public Constructor
///
/// Constructor, simply creates a new NetworkBrowser object
///

public NetworkBrowser()
{

}
#endregion
#region Public Methods
///
/// Uses the DllImport : NetServerEnum with all its required parameters
/// (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
/// for full details or method signature) to retrieve a list of domain SV_TYPE_WORKSTATION
/// and SV_TYPE_SERVER PC's
///

/// <returns>Arraylist that represents all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER
/// PC's in the Domain
public ArrayList getNetworkComputers()
{
//local fields
ArrayList networkComputers = new ArrayList();
const int MAX_PREFERRED_LENGTH = -1;
int SV_TYPE_WORKSTATION = 1;
int SV_TYPE_SERVER = 2;
IntPtr buffer = IntPtr.Zero;
IntPtr tmpBuffer = IntPtr.Zero;
int entriesRead = 0;
int totalEntries = 0;
int resHandle = 0;
int sizeofINFO = Marshal.SizeOf(typeof(_SERVER_INFO_100));


try
{
//call the DllImport : NetServerEnum with all its required parameters
//see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
//for full details of method signature
int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH,
out entriesRead,
out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out
resHandle);
//if the returned with a NERR_Success (C++ term), =0 for C#
if (ret == 0)
{
//loop through all SV_TYPE_WORKSTATION and SV_TYPE_SERVER PC's
for (int i = 0; i < totalEntries; i++)
{
//get pointer to, Pointer to the buffer that received the data from
//the call to NetServerEnum. Must ensure to use correct size of
//STRUCTURE to ensure correct location in memory is pointed to
tmpBuffer = new IntPtr((int)buffer + (i * sizeofINFO));
//Have now got a pointer to the list of SV_TYPE_WORKSTATION and
//SV_TYPE_SERVER PC's, which is unmanaged memory
//Needs to Marshal data from an unmanaged block of memory to a
//managed object, again using STRUCTURE to ensure the correct data
//is marshalled
_SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
Marshal.PtrToStructure(tmpBuffer, typeof(_SERVER_INFO_100));

//add the PC names to the ArrayList
networkComputers.Add(svrInfo.sv100_name);
}
}
}
catch (Exception ex)
{
networkComputers.Add(ex.Message);
return networkComputers;
}
finally
{
//The NetApiBufferFree function frees
//the memory that the NetApiBufferAllocate function allocates
NetApiBufferFree(buffer);
}
//return entries found
return networkComputers;

}
#endregion
}
#endregion

I wish thee "Happy Programming" Cool | :cool:
GeneralRe: Computer Names from the Server System Pin
anithagaraga22-Oct-08 0:39
anithagaraga22-Oct-08 0:39 
GeneralRe: Computer Names from the Server System Pin
anithagaraga22-Oct-08 0:43
anithagaraga22-Oct-08 0:43 
GeneralRe: Computer Names from the Server System Pin
Eddy Vluggen22-Oct-08 1:05
professionalEddy Vluggen22-Oct-08 1:05 
GeneralRe: Computer Names from the Server System Pin
anithagaraga22-Oct-08 1:40
anithagaraga22-Oct-08 1:40 
QuestionRe: Computer Names from the Server System Pin
Eddy Vluggen22-Oct-08 1:51
professionalEddy Vluggen22-Oct-08 1:51 
AnswerRe: Computer Names from the Server System Pin
anithagaraga22-Oct-08 2:06
anithagaraga22-Oct-08 2:06 
GeneralRe: Computer Names from the Server System Pin
Eddy Vluggen22-Oct-08 2:09
professionalEddy Vluggen22-Oct-08 2:09 
GeneralRe: Computer Names from the Server System Pin
Dave Kreskowiak22-Oct-08 5:39
mveDave Kreskowiak22-Oct-08 5:39 
GeneralRe: Computer Names from the Server System Pin
anithagaraga22-Oct-08 18:29
anithagaraga22-Oct-08 18:29 
GeneralRe: Computer Names from the Server System Pin
Eddy Vluggen22-Oct-08 21:19
professionalEddy Vluggen22-Oct-08 21:19 
QuestionHow to display data as grid layout in Crystal reprot Pin
Maddie from Dartford21-Oct-08 19:46
Maddie from Dartford21-Oct-08 19:46 
AnswerRe: How to display data as grid layout in Crystal reprot Pin
selcuks21-Oct-08 22:40
selcuks21-Oct-08 22:40 
QuestionCreate trail version application Pin
Member 369523621-Oct-08 19:19
Member 369523621-Oct-08 19:19 
AnswerRe: Create trail version application Pin
Simon P Stevens21-Oct-08 21:59
Simon P Stevens21-Oct-08 21:59 
QuestionRe: Create trail version application Pin
Member 369523622-Oct-08 0:14
Member 369523622-Oct-08 0:14 
AnswerRe: Create trail version application Pin
Simon P Stevens22-Oct-08 1:51
Simon P Stevens22-Oct-08 1:51 
GeneralRe: Create trail version application Pin
Member 369523622-Oct-08 2:56
Member 369523622-Oct-08 2:56 

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.