Visual Basic Core Snippet Routines
GetComputerName
          
                 
   Retrieves the name of the local machine.          
           Updated:   Monday December 26, 2011   
click to copy code:  



   Applies to:   VB4-32, VB5, VB6   
OS restrictions:   None  
 Code Snippet
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'MAX_COMPUTERNAME is defined as 15; 
'add one to allow for trailing null
Private Const MAX_COMPUTERNAME As Long = 16

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

   Dim tmp As String

  'return the name of the computer
   tmp = Space$(MAX_COMPUTERNAME)

   If GetComputerName(tmp, Len(tmp)) <> 0 Then
      GetLocalComputerName = TrimNull(tmp)
   End If

End Function


Private Function TrimNull(startstr As String) As String

   TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
   
End Function

 Calling Syntax
   sComputername = GetLocalComputerName()

 Comments/Related
     
demo in use:   NetMessageBufferSend: Broadcasting System Messages
Related:   gethostbyname: Determine Network Host Name and IP Address
     

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter