I found the following example in a response by Cimperiali in regards to how to get the ComputerName. It seems pretty straightforward, but I have a question...
Is there a reason that MAX_COMPUTERNAME_LENGTH is set in a Constant? Can't you just put 'dwLen = 32' instead of 'dwLen = MAX_COMPUTERNAME_LENGTH + 1' since we know that 'MAX_COMPUTERNAME_LENGTH + 1' is always 32?
Code:'example by Donavon Kuhn ([email protected]) Private Const MAX_COMPUTERNAME_LENGTH As Long = 31 Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long Private Sub Form_Load() Dim dwLen As Long Dim strString As String 'Create a buffer dwLen = MAX_COMPUTERNAME_LENGTH + 1 strString = String(dwLen, "X") 'Get the computer name GetComputerName strString, dwLen 'get only the actual data strString = Left(strString, dwLen) 'Show the computer name MsgBox strString End Sub




Reply With Quote