Results 1 to 8 of 8

Thread: finding the computer name

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2001
    Location
    Bradenton, FL
    Posts
    87

    finding the computer name

    how do i find the name of the computer using my program, and the current username that is logged into that computer?
    thanks,
    greg

  2. #2
    Megatron
    Guest
    Code:
    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Private Sub Command1_Click()
        
        Dim sName As String
        Dim sComputer As String
        
        sName = Space$(255)
        sComputer = Space$(255)
        
        GetComputerName sComputer, 255
        GetUserName sName, 255
        
        sComputer = Left(sComputer, InStr(1, sComputer, vbNullChar) - 1)
        sName = Left(sName, InStr(1, sName, vbNullChar) - 1)
        
        Print sName & "-" & sComputer
        
    End Sub

  3. #3
    Hyperactive Member MetallicaD's Avatar
    Join Date
    Feb 2001
    Location
    Tallahassee, FL
    Posts
    488
    Thanks Megatron...

    I saw those API calls in the API View, but was under the impression that they returned a long.. how is it that if it does in fact return a long, the result is actually my terminal name and username? Also, do you need to pass it a string that is 255 blanks, could you just past it a 0-length string? and i take it the 255 you are passing is the lenght of that buffer...

    Thanks
    -Matt
    [vbcode]
    '*****************************
    MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
    '*****************************
    [/vbcode]

  4. #4
    Megatron
    Guest
    No. the 255 blanks is the buffer, meaning we have space for up to 255 different characters. If we used 0, then that would mean we have space for 0 characters.

  5. #5
    Megatron
    Guest
    As for the resturn value: It's a Long because it will return a 1 when the function suceeds, or a 0 when the function fails.

    The return value has nothing to do with the actual name being copied to the buffer. the string and the size are both modified in the function (by reference) so no return value is needed (although it would make our lives easier).

  6. #6
    Hyperactive Member MetallicaD's Avatar
    Join Date
    Feb 2001
    Location
    Tallahassee, FL
    Posts
    488
    Thanks, that makes sense.. thats why when you call the API, you arent assigning the return value to any long variable... i guess this is true of most API's

    Thanks for the help!
    -Matt
    [vbcode]
    '*****************************
    MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
    '*****************************
    [/vbcode]

  7. #7
    jim mcnamara
    Guest
    I think MetallicaD means why not
    Code:
    Call GetComputerName sComputer, 255
    Call GetUserName sName, 255
    Since they are both declared as function that retunrs a value?
    I agree, at very least in terms of programming style. And it might be cool to check return values as a matter of from.

    Or am I missing something major?

  8. #8
    Megatron
    Guest
    Checking the return values is either a matter of preference or it's essential (depending on the situation). It you simply don't care about the return value, you could use:
    Code:
    Myfunction Arg1, Arg2
    
    'or
    
    Call MyFunction(Arg1, Arg2)
    But, if the case arises (and sometimes it does) when you need to check if the function suceeds or fails (to report an error message or something) then use:
    Code:
    lResult = MyFunction(Arg1, Arg2)
    If lResult = 0 Then MsgBox "Error:"

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