How to capture current user name?

Roach051198
Roach051198 used Ask the Experts™
on
How to capture the current user name on PC......the computer name
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Use the API function WNetGetUser()

I will post a working VB example here in a few...

zsi
zsi

Commented:
In your [declarations] section:

Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long

In your code:
   Dim sName As String
   Dim sUserName As String * 20

   Call WNetGetUser(sName, sUserName, 20)
   MsgBox sUserName


Hope this helps!
zsi

Author

Commented:
Oh no!
A message box pop up:
"Compile error: Constants, fixed-length strings, arrays, and Declare statements not allowed as Public members of object modules"

I have no idea about API. Please help.
zsi

Commented:
When you put an API declaration (such as that one above) in a form, you must declare it as private, (by putting Private before the keyword Declare).  The function, however, will only be available to that form.

If you want the function to be available to the entire program, you have to move the declaration into a Bas file.  In fact, this is a good thing to do all of the time.

zsi

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial