Solved

Find User Name

Posted on 2000-03-31
6
227 Views
Last Modified: 2013-11-13
By using Winsock control u can findout the Computer Name.My Question is that How to Find out  the User Name (of Windows that is Login Name).
0
Comment
Question by:atreya
6 Comments
 
LVL 27

Expert Comment

by:Ark
Comment Utility
Hi
Add reference to Windows Scripting Host
'(VB menu, project, reference)
Private Sub Command1_Click()
  Dim wsh As New IWshNetwork_Class
  Text1 = wsh.UserName
End Sub
0
 
LVL 3

Expert Comment

by:wpsjr1
Comment Utility
If you dont have access to the scripting hosts its:

Option Explicit

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


Private Sub Form_Load()
  Dim s$, x&
 
  s = String$(255, 0)
  x = GetUserName(s, Len(s))
  Debug.Print Left$(s, x)
End Sub


Good Luck!  :)

Paul
0
 

Expert Comment

by:AshokKumar
Comment Utility
wpsjr1. You came close but u might choose to print debub.print s. that's all. y all those left functions. Otherwise, atreya, u got the answer.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
here, much easier :
ENVIRON("USERNAME")
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
Same for computer name:
ENVIRON("COMPUTERNAME")
0
 

Accepted Solution

by:
romualdt earned 10 total points
Comment Utility
Try this out


'Declerations
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long


       

Private Sub Command1_Click()
x = GetUser
Debug.Print x

End Sub

Function GetUser()
    ' This function uses a windows dll to qu
    '     ery the registry automatically ti return
    '     the user name
    Dim sBuffer As String
    Dim lSize As Long
    ' Parameters for the dll declaration are
    '     set
    sBuffer = Space$(255)
    lSize = Len(sBuffer)
    Call GetUserName(sBuffer, lSize) ' Call the declared dll Function


    If lSize > 0 Then
        GetUser = Left$(sBuffer, lSize) ' Remove empty spaces
    Else
        GetUser = vbNullString ' Return empty If no user is found
    End If
End Function
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

771 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

10 Experts available now in Live!

Get 1:1 Help Now