Как получить список (имена) компов локальных машин - C#
Формулировка задачи:
Здравствуйте , подскажите пожалуйста как можно получить список имен всех компьютеров подключенных в локальную сеть ? Я нашел готовый пример на вижуал бэйсик но мне нужен на C# . Просто так то я быстро могу разобраться в чужом коде и переделать его для своих нужд но тут жесть какая то. Помогите пожалуйста его переделать в код на c# если это вообще то (
Imports System.Runtime.InteropServices Public Class MasterBrowseList Private Const MAX_PREFERRED_LENGTH As Integer = -1& Private Const NERR_SUCCESS As Long = 0& Private Const ERROR_MORE_DATA As Long = 234& Private Const SV_TYPE_WORKSTATION As Long = &H1 Private Const SV_TYPE_SERVER As Long = &H2 Private Const SV_TYPE_SQLSERVER As Long = &H4 Private Const SV_TYPE_DOMAIN_CTRL As Long = &H8 Private Const SV_TYPE_DOMAIN_BAKCTRL As Long = &H10 Private Const SV_TYPE_TIME_SOURCE As Long = &H20 Private Const SV_TYPE_AFP As Long = &H40 Private Const SV_TYPE_NOVELL As Long = &H80 Private Const SV_TYPE_DOMAIN_MEMBER As Long = &H100 Private Const SV_TYPE_PRINTQ_SERVER As Long = &H200 Private Const SV_TYPE_DIALIN_SERVER As Long = &H400 Private Const SV_TYPE_XENIX_SERVER As Long = &H800 Private Const SV_TYPE_SERVER_UNIX As Long = SV_TYPE_XENIX_SERVER Private Const SV_TYPE_NT As Long = &H1000 Private Const SV_TYPE_WFW As Long = &H2000 Private Const SV_TYPE_SERVER_MFPN As Long = &H4000 Private Const SV_TYPE_SERVER_NT As Long = &H8000 Private Const SV_TYPE_POTENTIAL_BROWSER As Long = &H10000 Private Const SV_TYPE_BACKUP_BROWSER As Long = &H20000 Private Const SV_TYPE_MASTER_BROWSER As Long = &H40000 Private Const SV_TYPE_DOMAIN_MASTER As Long = &H80000 Private Const SV_TYPE_SERVER_OSF As Long = &H100000 Private Const SV_TYPE_SERVER_VMS As Long = &H200000 Private Const SV_TYPE_WINDOWS As Long = &H400000 'Windows95 + Private Const SV_TYPE_DFS As Long = &H800000 'Root of a DFS tree Private Const SV_TYPE_CLUSTER_NT As Long = &H1000000 'NT Cluster Private Const SV_TYPE_TERMINALSERVER As Long = &H2000000 'Terminal Server Private Const SV_TYPE_DCE As Long = &H10000000 'IBM DSS Private Const SV_TYPE_ALTERNATE_XPORT As Long = &H20000000 'return alternate transport Private Const SV_TYPE_LOCAL_LIST_ONLY As Long = &H40000000 'return local only Private Const SV_TYPE_DOMAIN_ENUM As Long = &H80000000 Private Const SV_TYPE_ALL As Long = &HFFFFFFFF Private Const MAJOR_VERSION_MASK As Long = &HF Private Structure SERVER_INFO_101 Dim PlatformID As Integer Dim Name As IntPtr Dim VersionMajor As Integer Dim VersionMinor As Integer Dim Type As Integer Dim Comment As IntPtr End Structure Private Declare Auto Function NetApiBufferFree Lib "netapi32.dll" (ByVal bufptr As IntPtr) As Integer Private Declare Auto Function NetServerEnum Lib "netapi32.dll" _ (ByVal nullptr As Integer, _ ByVal Level As Integer, _ ByRef BufPtr As IntPtr, _ ByVal BufMaxLen As Integer, _ ByRef EntriesRead As Integer, _ ByRef TotalEntries As Integer, _ ByVal ServerType As Integer, _ ByVal Domain As IntPtr, _ ByVal ResumeHandle As Integer) As Integer Public Enum MACHINEPLATFORM As Integer DOS = 300 OS2 = 400 NT_2K_XP = 500 OSF = 600 VMS = 700 End Enum <Flags()> Public Enum MACHINETYPE As Integer Workstation = &H1 Server = &H2 SQLServer = &H4 DomainController = &H8 BackupDomainController = &H10 TimeSource = &H20 AppleFileProtocolServer = &H40 NovellServer = &H80 DomainMember = &H100 PrintServer = &H200 DialinServer = &H400 UnixOrXenix = &H800 NT_2K_XP = &H1000 WinForWorkgroups = &H2000 ServerFilePrintForNetware = &H4000 ServerNT = &H8000 PotentialBrowser = &H10000 BackupBrowser = &H20000 MasterBrowser = &H40000 DomainMaster = &H80000 ServerOSF = &H100000 ServerVMS = &H200000 Windows95OrHigher = &H400000 DFSRoot = &H800000 ClusterNode = &H1000000 TerminalServer = &H2000000 ClusterVirtualServer = &H4000000 End Enum Public Structure MACHINE Dim Platform As MACHINEPLATFORM Dim Name As String Dim VersionMajor As Integer Dim VersionMinor As Integer Dim TypeBits As MACHINETYPE Dim Comment As String End Structure Public Shared Function GetMasterBrowseList() As MACHINE() Return GetMasterBrowseList(Nothing) End Function Public Shared Function GetMasterBrowseList(ByVal Domain As String) As MACHINE() Dim BrowseList As New ArrayList Dim BufPtr As IntPtr Dim se101 As SERVER_INFO_101 Dim StructSize As Integer = Marshal.SizeOf(se101) Dim EntriesRead As Integer Dim TotalEntries As Integer Dim ServerType As Integer = SV_TYPE_WORKSTATION Or SV_TYPE_SERVER Dim DomainPtr As IntPtr If Domain Is Nothing Or Domain = "" Then DomainPtr = New IntPtr(0) Else 'we are faking a constant string here with a BSTR by appending 'a null character. we'll free it afterwards DomainPtr = Marshal.StringToBSTR(Domain & Chr(0)) End If Dim ret As Integer = NetServerEnum(0, _ 101, _ BufPtr, _ MAX_PREFERRED_LENGTH, _ EntriesRead, _ TotalEntries, _ ServerType, _ DomainPtr, _ 0) Marshal.FreeBSTR(DomainPtr) Try If ret = NERR_SUCCESS Then Dim nextbufptr As IntPtr For t As Integer = 0 To EntriesRead - 1 'copy memory chunk OF SIZE StructSize ' FROM LOC bufptr + (StructSize + t) ' INTO LOC se101 nextbufptr = New IntPtr(BufPtr.ToInt32 + (StructSize * t)) se101 = Marshal.PtrToStructure(nextbufptr, GetType(SERVER_INFO_101)) Dim themachine As MACHINE themachine.Name = Marshal.PtrToStringUni(se101.Name) themachine.Comment = Marshal.PtrToStringUni(se101.Comment) themachine.Platform = se101.PlatformID themachine.TypeBits = se101.Type themachine.VersionMajor = se101.VersionMajor And MAJOR_VERSION_MASK themachine.VersionMinor = se101.VersionMinor BrowseList.Add(themachine) Next End If Catch ex As Exception Throw ex Finally NetApiBufferFree(BufPtr) End Try Return BrowseList.ToArray(GetType(MACHINE)) End Function End Class 2. Create a test container. I used a console application and ran this code: Module Module1 Sub Main() Dim machine As MasterBrowseList.MACHINE Dim machineList() As MasterBrowseList.MACHINE machineList = MasterBrowseList.GetMasterBrowseList Console.WriteLine("Name" & vbTab & "Platform" & vbTab & "Version") Console.WriteLine("==========================================================") For Each machine In machineList With machine Console.WriteLine(.Name & vbTab & .Platform & vbTab & .VersionMajor & "." & .VersionMinor) End With Next Console.WriteLine("Press any key to exit") Console.Read() End Sub End Module
Решение задачи: «Как получить список (имена) компов локальных машин»
textual
Листинг программы
using System; using System.Collections; using System.Runtime.InteropServices; namespace ServerEnum { internal class Class1 { [DllImport("netapi32.dll", EntryPoint="NetServerEnum")] public static extern NERR 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 ResumeHandle); [DllImport("netapi32.dll", EntryPoint="NetApiBufferFree")] public static extern NERR NetApiBufferFree(IntPtr Buffer); [Flags] 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, } [StructLayout(LayoutKind.Sequential)] public struct SERVER_INFO_101 { [MarshalAs(UnmanagedType.U4)] public uint sv101_platform_id; [MarshalAs(UnmanagedType.LPWStr)] public string sv101_name; [MarshalAs(UnmanagedType.U4)] public uint sv101_version_major; [MarshalAs(UnmanagedType.U4)] public uint sv101_version_minor; [MarshalAs(UnmanagedType.U4)] public uint sv101_type; [MarshalAs(UnmanagedType.LPWStr)] public string sv101_comment; } public enum PLATFORM_ID : uint { PLATFORM_ID_DOS = 300, PLATFORM_ID_OS2 = 400, PLATFORM_ID_NT = 500, PLATFORM_ID_OSF = 600, PLATFORM_ID_VMS = 700, } public enum NERR { NERR_Success = 0, ERROR_ACCESS_DENIED = 5, ERROR_NOT_ENOUGH_MEMORY = 8, ERROR_BAD_NETPATH = 53, ERROR_NETWORK_BUSY = 54, ERROR_INVALID_PARAMETER = 87, ERROR_INVALID_LEVEL = 124, ERROR_MORE_DATA = 234, ERROR_EXTENDED_ERROR = 1208, ERROR_NO_NETWORK = 1222, ERROR_INVALID_HANDLE_STATE = 1609, ERROR_NO_BROWSER_SERVERS_FOUND = 6118, } public static ArrayList GetServerList(SV_101_TYPES type) { SERVER_INFO_101 si; IntPtr pInfo = IntPtr.Zero; int etriesread = 0; int totalentries = 0; ArrayList srvs = new ArrayList(); try { NERR err = NetServerEnum(null, 101, out pInfo, -1, ref etriesread, ref totalentries, type, null, 0); if ((err == NERR.NERR_Success || err == NERR.ERROR_MORE_DATA) && pInfo != IntPtr.Zero) { int ptr = pInfo.ToInt32(); for (int i = 0; i < etriesread; i++) { si = (SERVER_INFO_101) Marshal.PtrToStructure(new IntPtr(ptr), typeof (SERVER_INFO_101)); srvs.Add(si.sv101_name); ptr += Marshal.SizeOf(si); } } } catch (Exception) { } finally { if (pInfo != IntPtr.Zero) { NetApiBufferFree(pInfo); } } return (srvs); } [STAThread] static void Main() { Console.WriteLine("{0} WORKSTATION", new string('=', 10)); ArrayList list = GetServerList(SV_101_TYPES.SV_TYPE_WORKSTATION); foreach (string name in list) { Console.WriteLine(name); } Console.WriteLine("{0} UNIX", new string('=', 10)); list = GetServerList(SV_101_TYPES.SV_TYPE_SERVER_UNIX); foreach (string name in list) { Console.WriteLine(name); } Console.ReadLine(); } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д