Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get PCName and UserName

Posted on 2001-06-04
13
Medium Priority
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +4
13 Comments
 
LVL 53

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 33

Expert Comment

by:hongjun
ID: 6156098
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 200 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
 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

618 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