API Function to get the computer name of the PC

Posted by VIJI Tuesday, June 30, 2009

The following VB Function is used to get the computer name of the current PC.

It uses GetComputerName library.

We need to declare the dll GetComputerName in the top the VB Module.


' API declared to find the current computer name.
Public Declare Function GetComputerName Lib "Kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long


Public Function ComputerName_FX() As String

' Function calls the API function and returns a string of the computer name.


On Error Resume Next
Dim lSize As Long
Dim lpstrBuffer As String
lSize = 255
lpstrBuffer = Space$(lSize)

'Call GetComputerName API
If GetComputerName(lpstrBuffer, lSize) Then
ComputerName_FX = Left$(lpstrBuffer, lSize)
Else
ComputerName_FX = ""
End If


End Function


Example:

ComputerNameStr = ComputerName_FX
Debug.Print ComputerNameStr

Output:
RAJ-PC

0 Responses to API Function to get the computer name of the PC

Post a Comment