VB Code:
  1. Option Explicit
  2.  
  3. Public Const MAX_WSADescription = 256
  4. Public Const MAX_WSASYSStatus = 128
  5. Public Const ERROR_SUCCESS       As Long = 0
  6. Public Const WS_VERSION_REQD     As Long = &H101
  7. Public Const WS_VERSION_MAJOR    As Long = WS_VERSION_REQD \ &H100 And &HFF&
  8. Public Const WS_VERSION_MINOR    As Long = WS_VERSION_REQD And &HFF&
  9. Public Const MIN_SOCKETS_REQD    As Long = 1
  10. Public Const SOCKET_ERROR        As Long = -1
  11.  
  12. Public Type HOSTENT
  13.    hName      As Long
  14.    hAliases   As Long
  15.    hAddrType  As Integer
  16.    hLen       As Integer
  17.    hAddrList  As Long
  18. End Type
  19.  
  20. Public Type WSADATA
  21.    wVersion      As Integer
  22.    wHighVersion  As Integer
  23.    szDescription(0 To MAX_WSADescription)   As Byte
  24.    szSystemStatus(0 To MAX_WSASYSStatus)    As Byte
  25.    wMaxSockets   As Integer
  26.    wMaxUDPDG     As Integer
  27.    dwVendorInfo  As Long
  28. End Type
  29.  
  30. Public Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
  31.  
  32. Public Declare Function WSAStartup Lib "WSOCK32.DLL" _
  33.    (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
  34.    
  35. Public Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
  36.  
  37. Public Declare Function gethostname Lib "WSOCK32.DLL" _
  38.    (ByVal szHost As String, ByVal dwHostLen As Long) As Long
  39.    
  40. Public Declare Function gethostbyname Lib "WSOCK32.DLL" _
  41.    (ByVal szHost As String) As Long
  42.    
  43. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  44.    (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
  45.  
  46. 'RETRIEVES THE IP ADDRESS OF THIS USER
  47.  
  48. Public Function GETIPADDRESS() As String
  49.  
  50.    Dim sHostName    As String * 256
  51.    Dim lpHost    As Long
  52.    Dim HOST      As HOSTENT
  53.    Dim dwIPAddr  As Long
  54.    Dim tmpIPAddr() As Byte
  55.    Dim i         As Integer
  56.    Dim sIPAddr  As String
  57.    
  58.    If Not SocketsInitialize() Then
  59.       GETIPADDRESS = ""
  60.       Exit Function
  61.    End If
  62.    
  63.   'gethostname returns the name of the local host into
  64.   'the buffer specified by the name parameter. The host
  65.   'name is returned as a null-terminated string. The
  66.   'form of the host name is dependent on the Windows
  67.   'Sockets provider - it can be a simple host name, or
  68.   'it can be a fully qualified domain name. However, it
  69.   'is guaranteed that the name returned will be successfully
  70.   'parsed by gethostbyname and WSAAsyncGetHostByName.
  71.  
  72.   'In actual application, if no local host name has been
  73.   'configured, gethostname must succeed and return a token
  74.   'host name that gethostbyname or WSAAsyncGetHostByName
  75.   'can resolve.
  76.    
  77.    If gethostname(sHostName, 256) = SOCKET_ERROR Then
  78.      
  79.       GETIPADDRESS = ""
  80.      
  81.       MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
  82.               " has occurred. Unable to successfully get Host Name."
  83.      
  84.       SocketsCleanup
  85.      
  86.       Exit Function
  87.    
  88.    End If
  89.    
  90.   'gethostbyname returns a pointer to a HOSTENT structure
  91.   '- a structure allocated by Windows Sockets. The HOSTENT
  92.   'structure contains the results of a successful search
  93.   'for the host specified in the name parameter.
  94.  
  95.   'The application must never attempt to modify this
  96.   'structure or to free any of its components. Furthermore,
  97.   'only one copy of this structure is allocated per thread,
  98.   'so the application should copy any information it needs
  99.   'before issuing any other Windows Sockets function calls.
  100.  
  101.   'gethostbyname function cannot resolve IP address strings
  102.   'passed to it. Such a request is treated exactly as if an
  103.   'unknown host name were passed. Use inet_addr to convert
  104.   'an IP address string the string to an actual IP address,
  105.   'then use another function, gethostbyaddr, to obtain the
  106.   'contents of the HOSTENT structure.
  107.    
  108.    sHostName = Trim$(sHostName)
  109.    
  110.    lpHost = gethostbyname(sHostName)
  111.    
  112.    If lpHost = 0 Then
  113.      
  114.       GETIPADDRESS = ""
  115.      
  116.       MsgBox "Windows Sockets are not responding. " & _
  117.               "Unable to successfully get Host Name."
  118.      
  119.       SocketsCleanup
  120.      
  121.       Exit Function
  122.    
  123.    End If
  124.    
  125.   'to extract the returned IP address, we have to copy
  126.   'the HOST structure and its members
  127.    
  128.    CopyMemory HOST, lpHost, Len(HOST)
  129.    
  130.    CopyMemory dwIPAddr, HOST.hAddrList, 4
  131.    
  132.   'create an array to hold the result
  133.    
  134.    ReDim tmpIPAddr(1 To HOST.hLen)
  135.    
  136.    CopyMemory tmpIPAddr(1), dwIPAddr, HOST.hLen
  137.    
  138.   'and with the array, build the actual address,
  139.   'appending a period between members
  140.    
  141.    For i = 1 To HOST.hLen
  142.      
  143.       sIPAddr = sIPAddr & tmpIPAddr(i) & "."
  144.    
  145.    Next
  146.  
  147.   'the routine adds a period to the end of the
  148.   'string, so remove it here
  149.    
  150.    GETIPADDRESS = Mid$(sIPAddr, 1, Len(sIPAddr) - 1)
  151.    
  152.    SocketsCleanup
  153.    
  154. End Function
  155.  
  156. 'RETRIEVES THE SYSTEM NAME
  157.  
  158. Public Function GETIPHOSTNAME() As String
  159.  
  160.     Dim sHostName As String * 256
  161.    
  162.     If Not SocketsInitialize() Then
  163.        
  164.         GETIPHOSTNAME = ""
  165.        
  166.         Exit Function
  167.    
  168.     End If
  169.    
  170.     If gethostname(sHostName, 256) = SOCKET_ERROR Then
  171.        
  172.         GETIPHOSTNAME = ""
  173.        
  174.         MsgBox "Windows Sockets error " & Str$(WSAGetLastError()) & _
  175.                 " has occurred.  Unable to successfully get Host Name."
  176.        
  177.         SocketsCleanup
  178.        
  179.         Exit Function
  180.    
  181.     End If
  182.    
  183.     GETIPHOSTNAME = Left$(sHostName, InStr(sHostName, Chr(0)) - 1)
  184.    
  185.     SocketsCleanup
  186.  
  187. End Function
  188.  
  189. Public Function HiByte(ByVal wParam As Integer) As Byte
  190.  
  191.   'note: VB4-32 users should declare this function As Integer
  192.    
  193.    HiByte = (wParam And &HFF00&) \ (&H100)
  194.  
  195. End Function
  196.  
  197. Public Function LoByte(ByVal wParam As Integer) As Byte
  198.  
  199.   'note: VB4-32 users should declare this function As Integer
  200.    
  201.    LoByte = wParam And &HFF&
  202.  
  203. End Function
  204.  
  205. Public Sub SocketsCleanup()
  206.  
  207.     If WSACleanup() <> ERROR_SUCCESS Then
  208.        
  209.         MsgBox "Socket error occurred in Cleanup."
  210.    
  211.     End If
  212.    
  213. End Sub
  214.  
  215. Public Function SocketsInitialize() As Boolean
  216.  
  217.    Dim WSAD As WSADATA
  218.    
  219.    Dim sLoByte As String
  220.    
  221.    Dim sHiByte As String
  222.    
  223.    If WSAStartup(WS_VERSION_REQD, WSAD) <> ERROR_SUCCESS Then
  224.      
  225.       MsgBox "The 32-bit Windows Socket is not responding."
  226.      
  227.       SocketsInitialize = False
  228.      
  229.       Exit Function
  230.    
  231.    End If
  232.      
  233.    If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
  234.        
  235.         MsgBox "This application requires a minimum of " & _
  236.                 CStr(MIN_SOCKETS_REQD) & " supported sockets."
  237.        
  238.         SocketsInitialize = False
  239.        
  240.         Exit Function
  241.    
  242.     End If
  243.    
  244.    If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or _
  245.      (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
  246.       HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
  247.      
  248.       sHiByte = CStr(HiByte(WSAD.wVersion))
  249.      
  250.       sLoByte = CStr(LoByte(WSAD.wVersion))
  251.      
  252.       MsgBox "Sockets version " & sLoByte & "." & sHiByte & _
  253.              " is not supported by 32-bit Windows Sockets."
  254.      
  255.       SocketsInitialize = False
  256.      
  257.       Exit Function
  258.      
  259.    End If
  260.      
  261.   'must be OK, so lets do it
  262.    
  263.    SocketsInitialize = True
  264.        
  265. End Function
  266.    
  267. ' Usage
  268.  
  269. Call GETIPHOSTNAME