Retrieve local NetBIOS name with VB6

By | 2015-01-17

There are a two really easy ways to get the netBIOS name of the local machine. One leverages API calls to Win32, while the other simply queries an environment variable.

The first method is the simplest of the two. From within Visual Basic 6 (VB6), you can query any of the system 39 or so system environment variables. One of this variables, called ComputerName holds the netBIOS name of the local machine. To access these environment variables from VB6, you’ll want to use the ENVIRON built in function, thus:

Dim strMyComputerName as string
strMyComputerName = Environ("computername")

This code simply grabs the value stored in the Computername environment variable, and stores it in a string variable for later use. Easy, right?

Now this method is all well and good. It’s simple and to the point, doing what it needs to do with no muss or fuss. However, there are some place out there which restrict access to environment variables for security reasons. Yes, those places exist. In this case, you’ll want to use an API call.

The API call that I’ll be using for this is GetComputerNameA, from Win32. This does not return the fully qualified domain name of the local machine; that is a different API call, which functions similarly.

The first thing you’ll want to do is create a new module, or you can use an existing one. Place the following line at the top of the module:

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

This is telling VB that we are going to define a function within our program called GetComputerName, and it will be referencing the Win32 API (kernel32). The actual code with the kernel32 dll that we will be using is GetComputerNameA. The code within kernel32 requires two parameters, a pointer to a buffer that receives the computer name (lpBuffer), and the size of the buffer (nSize). Now we need to define a function to grab the information from the API. This is what will be referencing from everywhere else in our program.

Public Function LocalComputerName() As String

Dim sBuffer As String
Dim lAns As Long

sBuffer = Space$(255)
lAns = GetComputerName(sBuffer, 255)
If lAns <> 0 Then
LocalComputerName = Left$(sBuffer, InStr(sBuffer, Chr(0)) - 1)
Else
Err.Raise Err.LastDllError, , "A system call returned an error code of " & Err.LastDllError
LocalComputerName = ""
End If

End Function

If you are a VB6 coder, this will look pretty straightforward to you. However, if you have not worked with building API-enabled functions before, I’ll break it down a bit for you.

First, we define this function as Public, which means it can be called from anywhere else in our program, including forms and other modules or functions. The function will return a string value.

Next, the two variables that need to come back from the API call are defined, one string (sBuffer) and one long integer (lAns). sBuffer is then filled with spaces. After that is taken care of, the call is made to the API, requesting the returned value be placed in sBuffer, and telling the API how big the buffer is.

An If / Then / Else structure is included here, in the event that, for some reason, the API can’t get the name. If the name is found, The return value (LocalComputerName) is set with the netBIOS name returned from the API. If the returned value is 0, then the phrase “” is set, and an error message is raised.

Finally, the function ends, and control is passed back to the caller. This function that we’ve just created can be called from anywhere. For example, if you have a button on a form, which you want to pop up a message box with the name of the computer, you would do this:

Private Sub Command1_Click()

MsgBox "computername = " & LocalComputerName

End Sub

And there you have it, the netBIOS name of the computer, easily retrieved by two different methods. Both of these methods should work on Windows operating systems from 9x through Windows 10 preview.

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.