Results 1 to 8 of 8

Thread: Getting Computer Name

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Posts
    25

    Talking Getting Computer Name

    Is there any code Snippit that can retreve tmy computers name ?

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Posts
    25
    I am running NT

  3. #3
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Code:
    '\\ To get/set the local computer name...
    Private Const MAX_COMPUTERNAME_LENGTH = 15
    Private Declare Function GetComputername Lib "kernel32.dll" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Private Declare Function SetComputername Lib "kernel32.dll" Alias "SetComputerNameA" (ByVal lpBuffer As String) As Long
    ..and the code to use it (from ApiSystem class

    Code:
    Public Property Let ComputerName(ByVal newName As String)
    
    Dim lRet As Long
    
    If LenB(newName) > (MAX_COMPUTERNAME_LENGTH) Then
        'Computer length exceeds MAX_COMPUTERNAME_LEMGTH
        Exit Property
    End If
    
    lRet = SetComputername(newName)
    If Err.LastDllError <> 0 Then
        'error occured
    End If
    
    End Property
    
    Public Property Get ComputerName() As String
    
        Dim sTmp As String
        Dim lRet As Long
    
        sTmp = String(MAX_COMPUTERNAME_LENGTH, 0)
        lRet = GetComputername(sTmp, Len(sTmp))
        If Err.LastDllError <> 0 And Err.LastDllError <> 203 Then
           'error occured
        Else
            ComputerName = Left$(sTmp, InStr(sTmp, Chr$(0)) - 1)
        End If
    
    End Property
    HTH,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    2.  
    3. Private MachineName As String
    4.  
    5. Private Sub GetMachineName()
    6.     Dim sBuffer As String
    7.     Dim lSize As Long
    8.     sBuffer = Space$(255)
    9.     lSize = Len(sBuffer)
    10.  
    11.     Call GetComputerName(sBuffer, lSize)
    12.     If lSize > 0 Then
    13.         MachineName = Left$(sBuffer, lSize)
    14.     Else
    15.         MachineName = vbNullString
    16.     End If
    17.    
    18. End Sub

  5. #5
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    Even easier:

    environ("COMPUTERNAME")

    returns

    Computer Name

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Posts
    25
    yrwyddfa does that code work on 98, because i am working at home 2 nite, and i cant get it 2 work

    this is what i tryed

    Private Sub CommandButton1_Click()
    MsgBox Environ("COMPUTERNAME")

    End Sub

  7. #7
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    just use the api call.... seems to work everywhere i try it .....

    admitedly that has only been nt & 2000 but i'm fairly sure it will work
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  8. #8
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    if you try this:

    VB Code:
    1. for n = 1 to 254
    2.    debug.print environ(n)
    3. next

    it will list all of the environment variables for the platform you run it on.

    I cannot check '98 myself because I don't have access to a copy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width