Visual Basic 6 function "GetComputerName"

Go back

Below you'll find the source for the Visual Basic 6 function GetComputerName.

Attribute VB_Name = "modGetComputerName"
' This function is downloaded from:
' You may freely distribute this file but please leave all comments, including this one, in it.
' @Author Stefan Thoolen <>

Private Declare Function GetComputerNameA Lib "kernel32" (ByVal sBuffer As String, lSize As Long) As Long

Private Const MAX_COMPUTERNAME_LENGTH As Long = 31

' Gets the computer network name
' @return   String      The computername
' @author   Stefan Thoolen <>
Public Function GetComputerName() As String
    Dim strString As String                                              ' Creates a buffer
    strString = String(MAX_COMPUTERNAME_LENGTH + 1, Chr$(0))             ' Fills the buffer to the max size
    GetComputerNameA strString, MAX_COMPUTERNAME_LENGTH + 1              ' Requests the computername
    GetComputerName = Left$(strString, InStr(1, strString, Chr$(0)) - 1) ' Returns the value without additional zero chars
End Function