Get Computer Name


Question

Is there a function to get a users computer name ?

Answer


Doug,

This code should work:

Declare Function api32_GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
                (ByVal lpBuffer As String, nSize As Long) As Long

Public Function smsGetComputerName() As String
    On Error Resume Next
    Dim strComputerName As String * 512
    Dim lngBufLen As Long
    Dim lngRet As Long
    lngBufLen = 503
    strComputerName = String(512, 0)
    lngRet = api32_GetComputerName(strComputerName, lngBufLen)
    If lngRet <> 0 Then
        smsGetComputerName = Left(strComputerName, lngBufLen)
    Else
        smsGetComputerName = ""
    End If
End Function

HTH,
Shamil


Copyright 1999-2008 by Shamil Salakhetdinov. All rights reserved.