Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you get the names of the computers, swiched on, on the network 1

Status
Not open for further replies.

Pete222

Programmer
Dec 29, 2002
85
GB
Im making a simple game but i don't know how I can get the names of the computers on the network in a list box.

Pete.
My site: clix.to/F
 
Option:

If you know the IP addresses of the computers on your network there is a way to ping addresses using VB and a way to return the computer name using the ip address. I have an excel spreadsheet of all the computer IP addresses on my network. There is a button on the form that will ping all the IP addresses in column A and return the status of the address (online or timeout) and return the computer name for that IP address (even if the user has changed the computer name). But once again, you must know the IP addresses first. I can send you the spreadsheet if you want or just post the code if you wish. Its a very handy application since I always need to know the status of my wireless hubs in production areas.

RUN FunnySignatureMessage
 
I don't know the code to return the names of the computers, thats basicly what i need.

Pete.
My site: clix.to/F
 
Here you go. Just copy this into a module and use the function:

GetHostNameFromIP([your ip address STRING])

'---START CODE ---

Option Explicit

Private Const WSADescription_Len As Long = 256
Private Const WSASYS_Status_Len As Long = 128
Private Const WS_VERSION_REQD As Long = &H101
Private Const IP_SUCCESS As Long = 0
Private Const SOCKET_ERROR As Long = -1
Private Const AF_INET As Long = 2

Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To WSADescription_Len) As Byte
szSystemStatus(0 To WSASYS_Status_Len) As Byte
imaxsockets As Integer
imaxudp As Integer
lpszvenderinfo As Long
End Type

Private Declare Function WSAStartup Lib "wsock32" _
(ByVal VersionReq As Long, _
WSADataReturn As WSADATA) As Long

Private Declare Function WSACleanup Lib "wsock32" () As Long

Private Declare Function inet_addr Lib "wsock32" _
(ByVal s As String) As Long

Private Declare Function gethostbyaddr Lib "wsock32" _
(haddr As Long, _
ByVal hnlen As Long, _
ByVal addrtype As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(xDest As Any, _
xSource As Any, _
ByVal nbytes As Long)

Private Declare Function lstrlen Lib "kernel32" _
Alias "lstrlenA" _
(lpString As Any) As Long


Public Function SocketsInitialize() As Boolean

Dim WSAD As WSADATA

SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS

End Function


Public Sub SocketsCleanup()

If WSACleanup() <> 0 Then
MsgBox &quot;Windows Sockets error occurred in Cleanup.&quot;, vbExclamation
End If

End Sub


Public Function GetHostNameFromIP(ByVal sAddress As String) As String

Dim ptrHosent As Long
Dim hAddress As Long
Dim nbytes As Long

If SocketsInitialize() Then

'convert string address to long
hAddress = inet_addr(sAddress)

If hAddress <> SOCKET_ERROR Then

'obtain a pointer to the HOSTENT structure
'that contains the name and address
'corresponding to the given network address.
ptrHosent = gethostbyaddr(hAddress, 4, AF_INET)

If ptrHosent <> 0 Then

'convert address and
'get resolved hostname
CopyMemory ptrHosent, ByVal ptrHosent, 4
nbytes = lstrlen(ByVal ptrHosent)

If nbytes > 0 Then
sAddress = Space$(nbytes)
CopyMemory ByVal sAddress, ByVal ptrHosent, nbytes
GetHostNameFromIP = sAddress
End If

Else: MsgBox &quot;Call to gethostbyaddr failed.&quot;
End If 'If ptrHosent

SocketsCleanup

Else: MsgBox &quot;String passed is an invalid IP.&quot;
End If 'If hAddress

Else: MsgBox &quot;Sockets failed to initialize.&quot;
End If 'If SocketsInitialize

End Function
'--end block--'

 
FORGOT TO MENTION:

I did not create that code, I take no credit for it. I take no responsibility for any adverse results from using that code. (although, there shouldn't be any) It was tested on a Windows2000 PC. Remember, use code from this forum at your own risk!
 
Sorry, im a newbie with modules (i haven't used them before) im a bit confused, I ve copyed the code into a module but what do i put in the form's code?

Pete.
My site: clix.to/F
 
Quick steps to test function:

1 Create a new form - (Make sure you have the module I supplied as part of the project)

2 Add a command button and 2 text boxes DO NOT RENAME THEM OR COPY ONE FROM ANOTHER.

3 Copy this code and paste it into your form.

Private Sub Command1_Click()
text2.text = GetHostNameFromIP(trim(text1.text))
End Sub

4 Run the project and type an IP address you want the host name of into text1

5 Click the command button and the host name should appear in Text2
 
Oh, i understand now...yes that code you gave me works but it isn't quite what i was looking for. Sorry its my fault for asking the wrong qusetion...What i ment was the i have a list box and i want it to show the computers which come up in Network Neighborhood.

Pete.
My site: clix.to/F
 
For the whole exercise try the sample code at the bottom of this page:

It will list all shares and servers on the network by name

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
That tells me every thing about the computer im using, not the names of all the computers in Network Neighborhood. im using a home network which makes the computers path &quot;\\&quot; & 'the computer's name' & &quot;\&quot; like if they are a directory.
If im talking nonsence, im confused.

Pete.
My site: clix.to/F
 
I've just run it on my home network and it gives this:

Domain... WORKGROUP
Server... \\EILEEN
Server... \\LAPTOP
Server... \\CHRIS
Server... \\JOHN1 John1
Share... \\EILEEN\eileens C
Share... \\EILEEN\D
Share... \\EILEEN\Eileens LJ4 HP LaserJet 4Si
Share... \\JOHN1\SharedDocs
Share... \\JOHN1\Johns Pictures
Share... \\JOHN1\Audio CD (D)
Share... \\JOHN1\Audio CD (E)
Share... \\JOHN1\Printer Epson Stylus COLOR 740 ESC/P 2

Just taking the Server entries appears to give just what you asked.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Sorry, i understand now, yes i get:

Date: 30 July 2003

UserName: peter
Computer Name: 98
Network... Microsoft Network
Domain... WORKGROUP
Server... \\95
Server... \\98
Share... \\95\C
Share... \\95\CD ROM (J)
Share... \\95\KEITH (G)
Share... \\95\PETER (F)
Share... \\95\E
Share... \\95\D
Share... \\95\ZIP 100
Share... \\95\H
Share... \\95\DATA
Share... \\95\A
Share... \\98\WEDDING
Share... \\98\DESKTOP
Share... \\98\EPSON 600
Share... \\98\AHEAD
Share... \\98\DATA
Share... \\98\EPSON 1290
Share... \\98\A FLOPPY
Share... \\98\D

but \\95\ isn't swiched on.

Pete.
My site: clix.to/F
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top