VB and VBA Users Source Code: Set and get a Computer's name
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Set and get a Computer's name
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Saturday, October 21, 2000
Hits:
772
Category:
Windows API
Article:
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 'Author : Andrew Baker 'Date : 21/10/2000 21:15 'Notes : Uses a private static for speed 'Revisions : 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 'Author : Andrew Baker 'Date : 21/10/2000 21:14 'Notes : 'Revisions : Function MachineNameSet(sName As String) As Boolean MachineNameSet = IIf(SetComputerName(sName & vbNullChar) = 0, False, True) End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder