Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Applications VBA Code Examples

Set and get a Computer's name

Set and get a Computer's name To set and get the name of the local computer/machine, use the following code: Option Explicit Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpbuffer As String, nsize As Long) As Long Private Declare Function SetComputerName Lib "kernel32" Alias "SetComputerNameA" (ByVal lpComputerName As String) As Long 'Purpose : Returns the name of the local machine/computer 'Inputs : N/A 'Outputs : Returns the name of the local machine 'Notes : Uses a private static for speed Function MachineName() As String Dim lRet As Long Dim lMaxLen As Long Static ssMachineName As String If Len(ssMachineName) = 0 Then lMaxLen = 100 ssMachineName = String$(lMaxLen, vbNullChar) lRet = GetComputerName(ssMachineName, lMaxLen) ssMachineName = Left$(ssMachineName, lMaxLen) End If MachineName = ssMachineName End Function 'Purpose : Sets the name of the local machine/computer 'Inputs : sName The new name of the local machine/computer 'Outputs : Returns True if failed to change local machine name Function MachineNameSet(sName As String) As Boolean MachineNameSet = IIf(SetComputerName(sName & vbNullChar) = 0, False, True) End Function