VB 6.0 - How to get the computer name and IP Address?

Asked By Ralph Acido on 04-Oct-05 09:43 PM
helo! i need help... im creating an application that display all the computer's name and all their IP Addresses that are connected in a workgroup e.g. "LanNet"...  is there a way to do this? Thanks in advance...

getting name - Asked By Venkat K on 04-Oct-05 10:17 PM

'this is for getting computer name
'write this in module and try u have to provide the ip address to get the name 

'In a module
Private Const MIN_SOCKETS_REQD As Long = 1
Private Const WS_VERSION_REQD As Long = &H101
Private Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD \ &H100 And &HFF&
Private Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And &HFF&
Private Const SOCKET_ERROR As Long = -1
Private Const WSADESCRIPTION_LEN = 257
Private Const WSASYS_STATUS_LEN = 129
Private Const MAX_WSADescription = 256
Private Const MAX_WSASYSStatus = 128
Private Type WSAData
wVersion As Integer
wHighVersion As Integer
szDescription(0 To MAX_WSADescription) As Byte
szSystemStatus(0 To MAX_WSASYSStatus) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type
Declare Function WSACleanup Lib "WSOCK32" () As Long
Declare Function WSAStartup Lib "WSOCK32" (ByVal wVersionRequired As Long, lpWSADATA As WSAData) As Long

Public Function SocketsInitialize() As Boolean
Dim WSAD As WSAData

If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
MsgBox "The 32-bit Windows Socket is not responding."
SocketsInitialize = False
Exit Function
End If

SocketsInitialize = True

End Function

Public Sub SocketsCleanup()

If WSACleanup() <> ERROR_SUCCESS Then
MsgBox "Socket error occurred in Cleanup."
End If

End Sub


In form1 (with Command1 button):


Option Explicit

Private Declare Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long
Private Declare Function gethostbyaddr Lib "wsock32.dll" (haddr As Long, ByVal hnlen As Long, ByVal addrtype As Long) As Long
Private Declare Function lstrlenA Lib "kernel32" (ByVal Ptr As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

Private Sub Command1_Click()
Dim nRet&, sName$

'start socket session
If Not Module1.SocketsInitialize Then Exit Sub

'get long addr
nRet = inet_addr("10.16.40.25")

'get address of host entry
nRet = gethostbyaddr(nRet, 4, 2)

sName = ""

If nRet <> 0 Then
'get pointer to host entry name
CopyMemory nRet, ByVal nRet, 4
'get host name
sName = Space$(lstrlenA(nRet))
CopyMemory ByVal sName, ByVal nRet, Len(sName)
End If

'display name
MsgBox sName

'end socket session
Module1.SocketsCleanup

End Sub

links - Asked By mv ark on 04-Oct-05 11:13 PM

http://www.mvps.org/access/api/api0009.htm  to find MachineName
http://www.mvps.org/access/api/api0067.htm  for IP Lookup

GetComputerName - Asked By Dharshana Jagoda on 05-Oct-05 05:37 AM

GetComputerName API can be used to get the computer name,

you can then use GetHostByName API to get the ipaddress

there is another easy way of doing it, you can use the winsock control to do it

eg

WinsockControl.LocalIp

WinsockControl.LocalHostName
Link - Asked By Aarthi Saravanakumar on 05-Oct-05 06:19 AM
http://searchvb.techtarget.com/vsnetTip/1,293823,sid8_gci881362_tax293037,00.html