Solved

Get PCName and UserName

Posted on 2001-06-04
13
277 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 50

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 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