Results 1 to 15 of 15

Thread: Computer Name

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2002
    Posts
    46

    Computer Name

    Hopefully a simple one... Trying to find a call to get the local workstations computer name. Anyone?

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

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

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    2.  
    3. Private MachineName As String
    4.  
    5. Private Sub GetMachineName()
    6.     Dim sBuffer As String
    7.     Dim lSize As Long
    8.     sBuffer = Space$(255)
    9.     lSize = Len(sBuffer)
    10.  
    11.     Call GetComputerName(sBuffer, lSize)
    12.     If lSize > 0 Then
    13.         MachineName = Left$(sBuffer, lSize)
    14.     Else
    15.         MachineName = vbNullString
    16.     End If
    17.    
    18. End Sub

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    geeez james.....

    VB Code:
    1. 'example by Donavon Kuhn ([email protected])
    2. Private Const MAX_COMPUTERNAME_LENGTH As Long = 31
    3. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    4. Private Sub Form_Load()
    5.     Dim dwLen As Long
    6.     Dim strString As String
    7.     'Create a buffer
    8.     dwLen = MAX_COMPUTERNAME_LENGTH + 1
    9.     strString = String(dwLen, "X")
    10.     'Get the computer name
    11.     GetComputerName strString, dwLen
    12.     'get only the actual data
    13.     strString = Left(strString, dwLen)
    14.     'Show the computer name
    15.     MsgBox strString
    16. End Sub

  5. #5
    Hyperactive Member phrozeman's Avatar
    Join Date
    Apr 2000
    Location
    Netherlands
    Posts
    442
    damn james, think Hacks api is a little shorter
    There's a certain mystique when I speak, that you notice that it's sorta unique, cause you know it's me

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2002
    Posts
    46
    Thanks hack and kleinma, i knew there had to be something easier than the first reply. Thanks again gentelmen.


  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by phrozeman
    damn james, think Hacks api is a little shorter
    it also does something different

    i just think james mistook the question for finding out remote computer info versus local computer name... impressive code though

  8. #8
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Hell, it works.

    Yes both the other guys code is shorter, but

    DOES SIZE REALLY MATTER
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  9. #9
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    wELL

    Originally posted by kleinma
    it also does something different

    i just think james mistook the question for finding out remote computer info versus local computer name... impressive code though
    Me=Dope. Sorry fellows...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    no james.. just be happy that all that code works and does what it is supposed to do... if all that code really did get the computer name of the local PC... then I would be wondering about you.

  11. #11
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: Well

    Originally posted by James Stanich

    DOES SIZE REALLY MATTER
    If it doesn't, try this

    Dim ws As String
    ws = Environ("COMPUTERNAME")

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    those sneaky environment variables....

  13. #13
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Originally posted by kleinma
    no james.. just be happy that all that code works and does what it is supposed to do... if all that code really did get the computer name of the local PC... then I would be wondering about you.
    Need to get my glasses check, can't read these days, oh well...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  14. #14
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    At the risk of being accused of chatting, I have to say that I've found this thread amusing.

    When I first read it, and saw James response, I chuckled.

    Not because I was laughing at you James, but because I have misread questions, and as a result posted totally off the wall responses, many, many times on this forum. As a result, I got "blasted" by other respondants who questioned my manhood, the legitimacy of my birth, and the maritial status of my parents at the time!

    James: At the risk of being attacked for doing nothing more than "upping" my post count (and I have been accused of that), I'd like to say: Welcome to the "I read the question too fast to understand what was being asked" club! Its a pleasure having company!

  15. #15
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Thanks Hack for the vote of confidence...

    I will try to read better next time (but I am sure that I will do it again)...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width