Results 1 to 8 of 8

Thread: Please advise how to obtain computer name

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Location
    Australia
    Posts
    9

    Post

    I am trying to programatically find what a computer's name is on a network. Does anyone know how to do this? I guess it's a Windows API function but I really have no idea.
    Thanks in advance.

  2. #2
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Post Getting desktop

    Here is an api:
    put this in a module
    Code:
    Declare Function GetComputerName Lib "kernel32.dll" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Public Function GetCompName() As String
    Dim compname As String, retval As Long  ' string to use as buffer & return value
    compname = Space(255)  ' set a large enough buffer for the computer name
    retval = GetComputerName(compname, 255)  ' get the computer's name
    ' Remove the trailing null character from the strong
    compname = Left(compname, InStr(compname, vbNullChar) - 1)
    GetCompName = compname
    End Function
    to display do this:
    Code:
    Text1.Text = GetCompName
    ok?

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

    Post

    This should work too.

    Code:
    sMyComputerName = Environ("COMPUTERNAME")

  4. #4
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Post

    IShamel I couldn't get your example to work why

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Location
    Australia
    Posts
    9

    Post

    Thanks Isamal, but I couldn't get it to work either - David's ('da-silvy') code was fine.

  6. #6
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Post

    so i'm not crazy :P

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

    Post

    It does not work on your machine because you do not have an Environment setting on your computer called "COMPUTERNAME"

    This code will list all the Environment variables that are held on your computer.

    Code:
    Private Sub Command1_Click()
        Dim EnvString, Indx,     
    
        Indx = 1
    
        Do
            Debug.Print Environ(Indx)
            Indx = Indx + 1
        Loop Until EnvString = ""
    
    End Sub
    From the list that is produced, you should be able to determine which setting holds the computer name. Then you can use this code to return the Computers Name.

    Code:
    sMyComputerName = Environ("PLACE ENVIRON NAME HERE")
    Hope this helps.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Location
    Australia
    Posts
    9

    Post

    Yep Ishamel, you're right - I've got it to work now.

    Thanks

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