Click here to Skip to main content
15,921,371 members
Home / Discussions / C#
   

C#

 
GeneralRe: n a very big problem with winforms Pin
Jesse Squire11-Jan-05 9:23
Jesse Squire11-Jan-05 9:23 
GeneralRe: n a very big problem with winforms Pin
Nick Parker11-Jan-05 10:13
protectorNick Parker11-Jan-05 10:13 
GeneralRe: n a very big problem with winforms Pin
mav.northwind13-Jan-05 8:51
mav.northwind13-Jan-05 8:51 
GeneralRAM disk Pin
Grimolfr11-Jan-05 7:13
Grimolfr11-Jan-05 7:13 
GeneralRe: RAM disk Pin
benjymous12-Jan-05 1:25
benjymous12-Jan-05 1:25 
GeneralRe: RAM disk Pin
Grimolfr12-Jan-05 4:39
Grimolfr12-Jan-05 4:39 
Generalscan Network Pin
realmontanakid11-Jan-05 6:33
realmontanakid11-Jan-05 6:33 
GeneralRe: scan Network Pin
Salil Khedkar11-Jan-05 18:33
Salil Khedkar11-Jan-05 18:33 
Use NetServerEnum() API to get the list. Here is the complete code:

<code>using System;
using System.Net;
using System.Runtime.InteropServices;

namespace EnumDomainMachines
{
class AppClass
{
[STAThread]
static void Main(string[] args)
{
NetMachinesEnum NetMachinesEnumurator = new NetMachinesEnum();
NetMachinesEnumurator.PrintList();
Console.ReadLine();
}
}

class NetMachinesEnum
{
// server types accepted by the NetServerEnum function
public enum SV_101_TYPES:uint
{
SV_TYPE_WORKSTATION= 0x00000001,
SV_TYPE_SERVER= 0x00000002,
SV_TYPE_SQLSERVER = 0x00000004,
SV_TYPE_DOMAIN_CTRL= 0x00000008,
SV_TYPE_DOMAIN_BAKCTRL= 0x00000010,
SV_TYPE_TIME_SOURCE= 0x00000020,
SV_TYPE_AFP= 0x00000040,
SV_TYPE_NOVELL= 0x00000080,
SV_TYPE_DOMAIN_MEMBER = 0x00000100,
SV_TYPE_PRINTQ_SERVER = 0x00000200,
SV_TYPE_DIALIN_SERVER = 0x00000400,
SV_TYPE_XENIX_SERVER = 0x00000800,
SV_TYPE_SERVER_UNIX= SV_TYPE_XENIX_SERVER,
SV_TYPE_NT= 0x00001000,
SV_TYPE_WFW= 0x00002000,
SV_TYPE_SERVER_MFPN= 0x00004000,
SV_TYPE_SERVER_NT = 0x00008000,
SV_TYPE_POTENTIAL_BROWSER = 0x00010000,
SV_TYPE_BACKUP_BROWSER= 0x00020000,
SV_TYPE_MASTER_BROWSER= 0x00040000,
SV_TYPE_DOMAIN_MASTER = 0x00080000,
SV_TYPE_SERVER_OSF= 0x00100000,
SV_TYPE_SERVER_VMS= 0x00200000,
SV_TYPE_WINDOWS= 0x00400000, SV_TYPE_DFS= 0x00800000, SV_TYPE_CLUSTER_NT= 0x01000000, SV_TYPE_TERMINALSERVER= 0x02000000, SV_TYPE_CLUSTER_VS_NT = 0x04000000, SV_TYPE_DCE= 0x10000000, SV_TYPE_ALTERNATE_XPORT= 0x20000000, SV_TYPE_LOCAL_LIST_ONLY= 0x40000000, SV_TYPE_DOMAIN_ENUM= 0x80000000,
SV_TYPE_ALL= 0xFFFFFFFF
};

// Server info function, returned by the NetServerEnum function
[StructLayout(LayoutKind.Sequential)]
public struct SERVER_INFO_101
{
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
public UInt32 sv101_platform_id;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
public string sv101_name;

[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 sv101_version_major;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 sv101_version_minor;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)] public UInt32 sv101_type;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] public string sv101_comment;
};

public enum PLATFORM_ID
{
PLATFORM_ID_DOS = 300,
PLATFORM_ID_OS2 = 400,
PLATFORM_ID_NT = 500,
PLATFORM_ID_OSF = 600,
PLATFORM_ID_VMS = 700
};

// constants
public const uint ERROR_SUCCESS = 0;
public const uint ERROR_MORE_DATA = 234;

// System functions
// Imported from the NET api (See MSDN for more details)
[DllImport("netapi32.dll",EntryPoint="NetServerEnum")]
public static extern int NetServerEnum( [MarshalAs(UnmanagedType.LPWStr)]string servername,
int level,
out IntPtr bufptr,
int prefmaxlen,
ref int entriesread,
ref int totalentries,
SV_101_TYPES servertype,
[MarshalAs(UnmanagedType.LPWStr)]string domain,
int resume_handle);

[DllImport("netapi32.dll",EntryPoint="NetApiBufferFree")]
public static extern int
NetApiBufferFree(IntPtr buffer);

[DllImport ("Netapi32", CharSet=CharSet.Unicode)]
public static extern int NetMessageBufferSend(
string servername,
string msgname,
string fromname,
string buf,
int buflen);

public string GetIPAddress(string sHostName)
{
try
{
IPHostEntry ipEntry = Dns.GetHostByName(sHostName);
IPAddress [] addr = ipEntry.AddressList;
string sIPAddress = addr[0].ToString();
return sIPAddress;
}
catch
{
return("Unknown");
}
}

public void PrintList()
{
int entriesread=0,totalentries=0;

do
{
// Buffer to store the available servers
// Filled by the NetServerEnum function
IntPtr buf = new IntPtr();

// Server structure
SERVER_INFO_101 server;

// call NetServerEnum function. The '101' defines the type of
// structure returned, in this case, SERVER_INFO_101 which contains
// the type of information we need. In the preferred max length parameter
// '-1' is passed to tell the function to allocate the necessary amount of
// memory for the data. The SV_TYPE_WORKSTATION tells the function to only
// return workstation type servers.
int ret = NetServerEnum(null,101,out buf,-1,
ref entriesread,ref totalentries,
SV_101_TYPES.SV_TYPE_ALL,null,0);

// if the function returned any data, fill the tree view
if( ret == ERROR_SUCCESS ||
ret == ERROR_MORE_DATA ||
entriesread > 0)
{
Int32 ptr = buf.ToInt32();

Console.WriteLine("--------------------------------------------------------------------------");
Console.WriteLine(string.Format("{0,15} {1,17} {2,15} {3} {4} {5}",
"PLATFORM_ID",
"IP_ADDR",
"SERVER_TYPE",
"V",
"NO",
"COMMENT"));
Console.WriteLine("--------------------------------------------------------------------------");

for(int i=0; i<entriesread; i++)
{
// cast the pointer to a SERVER_INFO_101 structure
server = (SERVER_INFO_101)Marshal.PtrToStructure(new IntPtr(ptr),typeof(SERVER_INFO_101));

ptr += Marshal.SizeOf(server);

// add the workstation name to the tree view
Console.WriteLine(string.Format("{0,15} {1,17} {2,15} v{3}.{4} ({5})",
server.sv101_name.ToLower(),
GetIPAddress(server.sv101_name),
(PLATFORM_ID)server.sv101_platform_id,
server.sv101_version_major,
server.sv101_version_minor,
server.sv101_comment));
}
Console.WriteLine("--------------------------------------------------------------------------");
}
else
{
// no servers found
Console.WriteLine("No servers found in network");
}

// free the buffer
NetApiBufferFree(buf);

} while(
entriesread < totalentries &&
entriesread != 0
);
}
}
}
</code>
GeneralRe: scan Network Pin
realmontanakid12-Jan-05 5:08
realmontanakid12-Jan-05 5:08 
GeneralRe: scan Network Pin
Salil Khedkar12-Jan-05 18:18
Salil Khedkar12-Jan-05 18:18 
GeneralSetting Datgrid Columns width for Pocket PC Pin
Memor11-Jan-05 6:32
Memor11-Jan-05 6:32 
QuestionHow to hide a inherited method or property? Pin
Wender Oliveira11-Jan-05 5:52
Wender Oliveira11-Jan-05 5:52 
AnswerRe: How to hide a inherited method or property? Pin
turbochimp11-Jan-05 7:28
turbochimp11-Jan-05 7:28 
AnswerRe: How to hide a inherited method or property? Pin
Jesse Squire11-Jan-05 9:31
Jesse Squire11-Jan-05 9:31 
AnswerRe: How to hide a inherited method or property? Pin
Daniel Zaharia11-Jan-05 20:50
Daniel Zaharia11-Jan-05 20:50 
Generalquestionnaire builidng application Pin
ramrod197911-Jan-05 5:23
ramrod197911-Jan-05 5:23 
QuestionCan you load any application in MDI Child Window? Pin
James Bolles (elpaso)11-Jan-05 5:12
James Bolles (elpaso)11-Jan-05 5:12 
Generalcreating mapfile for c# projects Pin
anurik11-Jan-05 5:09
anurik11-Jan-05 5:09 
Generalcreating mapile for c# projects Pin
anurik11-Jan-05 5:09
anurik11-Jan-05 5:09 
Generalcreating mapile for c# projects Pin
anurik11-Jan-05 5:08
anurik11-Jan-05 5:08 
Generaldisplay arabic characters in windows CE .net Pin
asmb198011-Jan-05 5:07
asmb198011-Jan-05 5:07 
GeneralAdd-in Menus Pin
Guinness4Strength11-Jan-05 4:30
Guinness4Strength11-Jan-05 4:30 
GeneralUserControl problem Pin
Daniel Zaharia11-Jan-05 3:49
Daniel Zaharia11-Jan-05 3:49 
GeneralSize in bytes of a serialized object Pin
fmarcos11-Jan-05 3:47
fmarcos11-Jan-05 3:47 
GeneralRe: Size in bytes of a serialized object Pin
turbochimp11-Jan-05 7:35
turbochimp11-Jan-05 7:35 

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.