Solved

Get PCName and UserName

Posted on 2001-06-04
13
268 Views
Last Modified: 2008-03-17
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
0
Comment
Question by:tonyckx
  • 4
  • 3
  • 2
  • +4
13 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6154697
Hi

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, _
      lSIDType)

   ' 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, _
      lSIDType)

   ' 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
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6154802
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
0
 
LVL 1

Expert Comment

by:Ranger2081200
ID: 6155142
tonyckx,

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)
   Else
      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

Later...
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6156098
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 50 total points
ID: 6156943
MsgBox Environ("ComputerName")
MsgBox Environ("UserName")
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6157692
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)

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Expert Comment

by:bhh
ID: 6157777
Listening...
0
 
LVL 1

Expert Comment

by:Ranger2081200
ID: 6158845
Hey rspahitz,

Where'd you find that at? and are there anymore environmental variables you can get from it?
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6161969
Ranger use this code, will give you Variable name and number.

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

0
 

Author Comment

by:tonyckx
ID: 6162523
Thank you very much!
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6165210

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

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

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6165368
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!
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6165595
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.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now