Results 1 to 6 of 6

Thread: Machine name

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    67

    Post

    How do use an API or other method to find the name of the computer that you are on.
    Any help is much appricated

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    You can use GetComputerName API:

    Code:
    Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    
    '--------Put this on any event
        Dim lRet As Long
        Dim strComputerName As String
        
        strComputerName = Space(125)
        lRet = GetComputerName(strComputerName, Len(strComputerName))
        If lRet Then strComputerName = Left(strComputerName, InStr(strComputerName, vbNullChar) - 1)
    strComputerName now holds the name of the computer.

    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819


  3. #3
    Lively Member Ishamel's Avatar
    Join Date
    Nov 1999
    Location
    Edinburgh, Scotland
    Posts
    112

    Post

    I use this Environ() array to return a PC name.

    Code:
    Public Function GetComputerName() As String
        'This Function will return the current PC's number
        Dim sEnvironmentString As String
        Dim Indx As Integer
        
        Indx = 1    ' Initialize index to 1.
        
        Do
            sEnvironmentString = Environ(Indx)   ' Get environment
    
            If Left(sEnvString, 13) = "COMPUTERNAME=" Then    ' Check PATH entry.
                GetComputerName = Right(sEnvironmentString, Len(sEnvironmentString) - 13)
                Exit Function
            Else
                Indx = Indx + 1 ' Not COMPUTERNAME entry, so increment
            End If
        Loop Until sEnvironmentString = ""
        
        GetComputerName = "(Unable to identify PC)"
    End Function

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Yes, you can, but API is much faster and you don't have to use any loops.

    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819


  5. #5
    Lively Member Ishamel's Avatar
    Join Date
    Nov 1999
    Location
    Edinburgh, Scotland
    Posts
    112

    Post

    Ye - I've already replaced my code with yours.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    67

    Post

    Thanks to both of you
    I used the first piece of code as that seems to be the general consensus of oppinion
    thanks again
    Death

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