Can someone please help me on this?


Posted by Rodrigues on April 22, 2001 6:42 PM

How can I display the computer's IP Address in a textbox or a label?
I'll be really glad if you answer this question.

Rodrigues

Posted by David on April 23, 2001 6:35 AM

Not the answer

In Visual basic I get the computers name using this function in kernel32 maybe they have one for IP address too.

Private Declare Function Get_ComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long


Posted by Kevin James on April 23, 2001 1:35 PM

The exact syntax I don't know, I am a "budding" VBA pgmr., but essentially, you can shell out to DOS, run IPCONFIG, extract the line where the IP is displayed and then return that value. Actually, there would be several more intricate steps, but that is the overview.



Posted by Ivan Moala on April 24, 2001 7:15 AM

Rodrigues
Not as easy as you may think but try this;

Public Const MAX_WSADescription = 256
Public Const MAX_WSASYSStatus = 128
Public Const ERROR_SUCCESS As Long = 0
Public Const WS_VERSION_REQD As Long = 257
Public Const WS_VERSION_MAJOR As Long = WS_VERSION_REQD \ 256 And 255
Public Const WS_VERSION_MINOR As Long = WS_VERSION_REQD And 255
Public Const MIN_SOCKETS_REQD As Long = 1
Public Const SOCKET_ERROR As Long = -1

Public Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLen As Integer
hAddrList As Long
End Type

Public 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
End Type


Public Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long

Public Declare Function WSAStartup Lib "WSOCK32.DLL" _
(ByVal wVersionRequired As Long, lpWSADATA As WSAData) As Long

Public Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long

Public Declare Function gethostname Lib "WSOCK32.DLL" _
(ByVal szHost As String, ByVal dwHostLen As Long) As Long

Public Declare Function gethostbyname Lib "WSOCK32.DLL" _
(ByVal szHost As String) As Long

Public Declare Sub CopyMemoryIPAddr Lib "kernel32" Alias "RtlMoveMemory" _
(hpvDest As Any, ByVal hpvSource As Any, ByVal cbCopy As Long)

Dim wParam As Integer
Dim HiByte As Byte
Dim LoByte As Byte

Public Function IPAddress() As String

Dim sHostName As String * 256
Dim lpHost As Long
Dim HOST As HOSTENT
Dim dwIPAddr As Long
Dim tmpIPAddr() As Byte
Dim i As Integer
Dim sIPAddr As String

If Not SocketsInitialize Then IPAddress = "": Exit Function

If gethostname(sHostName, 256) = SOCKET_ERROR Then
IPAddress = ""
MsgBox "Windows Sockets error " & Str(WSAGetLastError()) & _
" has occurred. Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If


sHostName = Trim(sHostName)
lpHost = gethostbyname(sHostName)

If lpHost = 0 Then
IPAddress = ""
MsgBox "Windows Sockets are not responding. Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If

CopyMemoryIPAddr HOST, lpHost, Len(HOST)
CopyMemoryIPAddr dwIPAddr, HOST.hAddrList, 4

ReDim tmpIPAddr(1 To HOST.hLen)
CopyMemoryIPAddr tmpIPAddr(1), dwIPAddr, HOST.hLen

For i = 1 To HOST.hLen
sIPAddr = sIPAddr & tmpIPAddr(i) & "."
Next

IPAddress = Mid(sIPAddr, 1, Len(sIPAddr) - 1)

SocketsCleanup

End Function


Public Function IPHostName() As String

Dim sHostName As String * 256

If Not SocketsInitialize Then IPHostName = "": Exit Function

If gethostname(sHostName, 256) = SOCKET_ERROR Then
IPHostName = ""
MsgBox "Windows Sockets error " & Str(WSAGetLastError()) & Chr$(13) & _
" has occurred. Unable to successfully get Host Name."
SocketsCleanup
Exit Function
End If

IPHostName = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
SocketsCleanup

End Function

Public Sub SocketsCleanup()

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

End Sub

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


If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
MsgBox "This application requires a minimum of " & _
CStr(MIN_SOCKETS_REQD) & " supported sockets."

SocketsInitialize = False
Exit Function
End If
HiByte = ((WSAD.wVersion And &HFF00&) \ (&H100))
LoByte = (WSAD.wVersion And &HFF&)
If LoByte < WS_VERSION_MAJOR Or LoByte = WS_VERSION_MAJOR And HiByte < WS_VERSION_MINOR Then

MsgBox "Sockets version " & CStr(LoByte) & "." & CStr(HiByte) & Chr(13) & _
" is not supported by 32-bit Windows Sockets."

SocketsInitialize = False
Exit Function

End If

SocketsInitialize = True

End Function

Ivan