[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

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
0
tonyckx
Asked:
tonyckx
  • 4
  • 3
  • 2
  • +4
1 Solution
 
Ryan ChongCommented:
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
 
Hornet241Commented:
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
 
Ranger2081200Commented:
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.

 
rspahitzCommented:
MsgBox Environ("ComputerName")
MsgBox Environ("UserName")
0
 
Hornet241Commented:
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
 
bhhCommented:
Listening...
0
 
Ranger2081200Commented:
Hey rspahitz,

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

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

0
 
tonyckxAuthor Commented:
Thank you very much!
0
 
Hornet241Commented:

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

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

0
 
rspahitzCommented:
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
 
rspahitzCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now