Skip Navigation Links.
Win32API Example - GetComputerName
Language(s):Visual Basic 6.0

Using the GetComputerName API.

' Win32API Examle - GetComputerName
' Jon Vote 02/2002
' 1) Create a new project. Form1 will be created by default.
' 2) Add a Command Button to the form.
' 3) Paste the following code into the declarations section of Form1:

' --- Begin code for Form1 ---

Option Explicit

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

Private Sub Form_Load()

  Me.Caption = "Win32API Example - GetComputerName"
  Command1.Width = 1680
  Command1.Caption = "&GetComputerName"
  Command1.Left = (Me.ScaleWidth - Command1.Width) / 2
End Sub

Private Sub Command1_Click()
  Dim lngReturnCode As Long
  Dim strBuffer As String
  Dim lngSize As Long
  strBuffer = Space$(80)
  lngSize = Len(strBuffer)
  lngReturnCode = GetComputerName(strBuffer, lngSize)
  MsgBox "The name of this computer is: " & Trim$(strBuffer)
End Sub

' --- End code for Form1 ---

This article has been viewed 4593 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.