Hello
How can I get my computer name
Thanks
in addition you can also use:
System.Net.Dns.GetHostName() which returns you a string. Pretty much the same as Environment.MachineName
ahmedilyas wrote:
in what way connected to what to the network you could maybe depending on how the computers are hooked on the network. for example if they are in a domain then you could go through the list of computers in active directory
Yea, computer which are connected to network by LAN.
There're X connected computers by LAN, how can I get their names
"System.Net.Dns.GetHostName() which returns you a string. Pretty much the same as Environment.MachineName"
There's a lot of difference when you don;t have a network. Why go out to the net to find out who you are when the CLR supports it directly
This will only work if you are connected to an active directory. Add
a reference to system.directoryservices. Replace NetworkName with your
network name.
Dim de As New System.DirectoryServices.DirectoryEntry("LDAP://NetworkName")
Dim ds As New System.DirectoryServices.DirectorySearcher(de)
Dim r As System.DirectoryServices.SearchResult
ds.Filter = "(objectClass=computer)"
Try
For Each r In ds.FindAll
Dim s As String
Console.WriteLine(r.GetDirectoryEntry.Name.ToString)
Next
Catch e As Exception
Console.WriteLine(e.ToString)
End Try
You could use NetServerEnum API for list of computer on network without a domain:
Imports System.Runtime.InteropServices
Module Module1
Structure Computer_info_101
Public Platform_ID As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Name As String
Public Version_Major As Integer
Public Version_Minor As Integer
Public Type As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Comment As String
End Structure
Declare Unicode Function NetServerEnum Lib "Netapi32.dll" _
(ByVal Servername As Integer, ByVal level As Integer, _
ByRef buffer As Integer, ByVal PrefMaxLen As Integer, _
ByRef EntriesRead As Integer, ByRef TotalEntries As Integer, _
ByVal ServerType As Integer, ByVal DomainName As String, _
ByRef ResumeHandle As Integer) As Integer
Declare Function NetApiBufferFree Lib "Netapi32.dll" _
(ByVal lpBuffer As Integer) As Integer
Private Const SV_TYPE_SERVER As Integer = &H2 ' All Servers
Sub Main()
Dim ComputerInfo As Computer_info_101
Dim i, MaxLenPref, level, ret, EntriesRead, TotalEntries, ResumeHandle As
Integer
Dim BufPtr As Integer
Dim iPtr As IntPtr
MaxLenPref = -1
level = 101
ret = NetServerEnum(0, level, BufPtr, MaxLenPref, EntriesRead, TotalEntries,
_
SV_TYPE_SERVER, "MSHOME", ResumeHandle) ' Replace MSHOME with your workgroup
name
If ret <> 0 Then
Console.WriteLine("An Error has occured")
Return
End If
' loop thru the entries
For i = 0 To EntriesRead - 1
' copy the stuff into our structure
Dim ptr As IntPtr = New IntPtr(BufPtr)
computerInfo = CType(Marshal.PtrToStructure(ptr,
GetType(Computer_info_101)), _
Computer_info_101)
BufPtr = BufPtr + Len(ComputerInfo)
Console.WriteLine(computerInfo.Name)
Next
NetApiBufferFree(BufPtr)
Console.Write("Press Enter to End")
Dim s As String = Console.ReadLine()
End Sub
End Module
Imports System.Runtime.InteropServices
Module Module1
Structure Computer_info_101
Public Platform_ID As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Name As String
Public Version_Major As Integer
Public Version_Minor As Integer
Public Type As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Comment As String
End Structure
That didn't work in VB 2005.
And I didn't find NetServerEnum in API, how can I get APIs in VB 2005
Sorry for the delayed response.
Here is a better example for you. This example encapsulates the API call for you in the MasterBrowserList Class.
1. Create a new class file for you project and paste this complete code:
Imports
System.Runtime.InteropServicesPublic 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
I think you can get the idea with this example. Its much more clear and concise. Let me know if you have any specific questions.
BTW, This example as in VB.NET 2005