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.
Printable View
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.
Here is an api:
put this in a module
to display do this: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
ok?Code:Text1.Text = GetCompName
This should work too.
Code:sMyComputerName = Environ("COMPUTERNAME")
IShamel I couldn't get your example to work why :confused:
Thanks Isamal, but I couldn't get it to work either - David's ('da-silvy') code was fine.
so i'm not crazy :P
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.
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:Private Sub Command1_Click()
Dim EnvString, Indx,
Indx = 1
Do
Debug.Print Environ(Indx)
Indx = Indx + 1
Loop Until EnvString = ""
End Sub
Hope this helps. :)Code:sMyComputerName = Environ("PLACE ENVIRON NAME HERE")
Yep Ishamel, you're right - I've got it to work now.
Thanks