Get PCName and UserName

Hello, I'm using Windows Nt as my operating system.  Can somebody please help me, how to get the PC Name and User login Name using API function or vb component.  Thanks
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:

This code is useful when you are getting your logon user with domain name. This code is applicable for Windows NT and 9x


Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (lpSystemName As String, ByVal lpAccountName As String, sid As Any, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long

Public Function GetLogonDomainuser() As String
   Dim lResult As Long            ' Result of various API calls.
   Dim I As Integer               ' Used in looping.
   Dim bUserSid(255) As Byte      ' This will contain your SID.
   Dim sUserName As String

   Dim sDomainName As String * 255   ' Domain the user belongs to.
   Dim lDomainNameLength As Long     ' Length of domain name needed.

   Dim lSIDType As Long              ' The type of SID info we are
                                     ' getting back.
   ' Get the SID of the user. (Refer to the MSDN for more information on SIDs
   ' and their function/purpose in the operating system.) Get the SID of this
   ' user by using the LookupAccountName API. In order to use the SID
   ' of the current user account, call the LookupAccountName API
   ' twice. The first time is to get the required sizes of the SID
   ' and the DomainName string. The second call is to actually get
   ' the desired information.
   sUserName = GetLogonUser
   lResult = LookupAccountName(vbNullString, sUserName, _
      bUserSid(0), 255, sDomainName, lDomainNameLength, _

   ' Now set the sDomainName string buffer to its proper size before
   ' calling the API again.
   sDomainName = Space(lDomainNameLength)
   ' Call the LookupAccountName again to get the actual SID for user.
   lResult = LookupAccountName(vbNullString, sUserName, _
      bUserSid(0), 255, sDomainName, lDomainNameLength, _

   ' Return value of zero means the call to LookupAccountName failed;
   ' test for this before you continue.
     If (lResult = 0) Then
        MsgBox "Error: Unable to Lookup the Current User Account: " _
           & sUserName
        Exit Function
     End If
     sDomainName = Left$(sDomainName, InStr(sDomainName, Chr$(0)) - 1)
     GetLogonDomainuser = Trim(sDomainName) & "\" & sUserName

End Function
Private Function GetLogonUser() As String
    Dim strTemp As String, strUserName As String
    'Create a buffer
    strTemp = String(100, Chr$(0))
    'strip the rest of the buffer
    strTemp = Left$(strTemp, InStr(strTemp, Chr$(0)) - 1)

    'Create a buffer
    strUserName = String(100, Chr$(0))
    'Get the username
    GetUserName strUserName, 100
    'strip the rest of the buffer
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
    GetLogonUser = strUserName
End Function
This will get you both user name and computer name

'These go in the Declarations area of a module or add the 'Private' keyword in from and use in a form

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function GetInfo()

   Dim RawUserName As String * 50
   Dim RawCompName As String * 50
   Dim nSize As Long
   Dim Ret As Long

   nSize = 50
   Ret = GetUserName(RawUserName, nSize)
   ActualUserName = Left$(RawUserName, nSize - 1)
   nSize = 50
   Ret = GetComputerName(RawCompName, nSize)
   ActualCompName = Left(RawCompName, nSize)
   MsgBox ActualUserName & "  " & ActualCompName

End Function

Here's another variation:

You can put the following in a module, I call the module gWSinfo for "get workstation info"

Put the following in the declarations area:

Public Declare Function WNetGetUserA Lib "mpr" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long
Public Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long

Now create two functions:
Function GetUserID() As String
   Dim sUserNameBuff As String * 255
   sUserNameBuff = Space(255)
   Call WNetGetUserA(vbNullString, sUserNameBuff, 255&)
   GetUserID = Left$(sUserNameBuff, InStr(sUserNameBuff, vbNullChar) - 1)
End Function
Function GetWSID() As String
   Dim sBuffer As String * 255
   Dim X As Long

   If GetComputerNameA(sBuffer, 255&) > 0 Then
      GetWSID = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
      GetWSID = "?"
   End If

End Function

By creating two functions I can call either one of them separately or both to get the info.
All I do in the form code to get the info is use the following lines of code:

strUsername = GetUserID   'Gets user name
strWorkStation = GetWSID  'Gets workstation ID

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

MsgBox Environ("ComputerName")
MsgBox Environ("UserName")

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hey rspahitz, Works great I like it.

Tonyckx, use rspahitz comment simple easy and returns empty string if it can't find the request variable(no error)

Hey rspahitz,

Where'd you find that at? and are there anymore environmental variables you can get from it?
Ranger use this code, will give you Variable name and number.

    For a = 0 to 50
        list1.Additem a & " - " & Environ(a)
    Next a

tonyckxAuthor Commented:
Thank you very much!

Ranger2 made a typO '1 to 29' not '0 to 50'

   For a = 1 to 29
       list1.Additem a & " - " & Environ(a)
   Next a

Sorry, guys.  Connection out for 2 days.

To see all values, you can do as indicated above, or shell to DOS and enter: SET

I actually picked it up from a project at work where the programmer misused it.  I read up on it in the help files and found I could get the entry in one line!
Oh, and I found that these values are not available in Win95/98/ME unless explicitly put there during boot-up, so some of the other options may be needed for those machines.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.