Code to get UserName won't work 
Author Message
 Code to get UserName won't work

I've copied some code to find the User Name of the current user into
my project, but it won't work. sBuffer just returns blanks. Can
someone point out what I've done wrong?

TIA
---------------------------------------------------------------
Private Declare Function GetUserName Lib "advapi32.dll" Alias
"GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long
Public OK As Boolean
Public UName As String

---------------------------------------------------------------
Sub Form_Load()

Dim sBuffer As String
Dim lSize As Long

sBuffer = Space$(255)
lSize = Len(sBuffer)
Call GetUserName(sBuffer, lSize)
If lSize > 0 Then
    UName = Left$(sBuffer, lSize)
Else
    UName = InputBox("Please enter your user ID.")

End If

End Sub

--
---------------------------------------------------
Jeff Dawson

* Swap "z" for "s" to e-mail.



Fri, 02 Feb 2001 03:00:00 GMT  
 Code to get UserName won't work

Quote:

>I've copied some code to find the User Name of the current user into
>my project, but it won't work. sBuffer just returns blanks. Can
>someone point out what I've done wrong?

>TIA
>---------------------------------------------------------------
>Private Declare Function GetUserName Lib "advapi32.dll" Alias
>"GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long
>Public OK As Boolean
>Public UName As String

>---------------------------------------------------------------
>Sub Form_Load()

>Dim sBuffer As String
>Dim lSize As Long

>sBuffer = Space$(255)
>lSize = Len(sBuffer)
>Call GetUserName(sBuffer, lSize)
>If lSize > 0 Then
>    UName = Left$(sBuffer, lSize)
>Else
>    UName = InputBox("Please enter your user ID.")

>End If

>End Sub

Try:

Option Explicit

' Access the GetUserNameA function in advapi32.dll and call the function
GetUserName.
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal
lpBuffer As String, nSize As Long) As Long

Public Function get_User_Name() As String

  Dim lpBuff As String * 25
  Dim ret As Long

  'Get the user name minus any trailing spaces found in the name.
  ret = GetUserName(lpBuff, 25)
  get_User_Name = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
  MsgBox get_User_Name

End Function



Fri, 02 Feb 2001 03:00:00 GMT  
 Code to get UserName won't work
A couple of things:
    1. Does the person have to log into their workstation using the Windows
logon screen?(this could be you for testing purposes when you log in)

    2. GetUserName is a function, it may want you to actually have the
result assigned to a variable, ie
        Dim lngResult as Long
        lngResult= GetUserName(sBuffer, lSize)

Quote:

>I've copied some code to find the User Name of the current user into
>my project, but it won't work. sBuffer just returns blanks. Can
>someone point out what I've done wrong?

>TIA
>---------------------------------------------------------------
>Private Declare Function GetUserName Lib "advapi32.dll" Alias
>"GetUserNameA" (ByVal lpbuffer As String, nSize As Long) As Long
>Public OK As Boolean
>Public UName As String

>---------------------------------------------------------------
>Sub Form_Load()

>Dim sBuffer As String
>Dim lSize As Long

>sBuffer = Space$(255)
>lSize = Len(sBuffer)
>Call GetUserName(sBuffer, lSize)
>If lSize > 0 Then
>    UName = Left$(sBuffer, lSize)
>Else
>    UName = InputBox("Please enter your user ID.")

>End If

>End Sub

>--
>---------------------------------------------------
>Jeff Dawson

>* Swap "z" for "s" to e-mail.



Sun, 11 Feb 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Code won't work in A97

2. Timer code won't work

3. Code won't work in template -- please help

4. Why won't this code work?

5. Why doesn't this code work on win 2000

6. vb4 code won't work in vb5

7. EM_GETSEL won't work with VB3 code windows (class OModule)

8. This code won't work HELP Please

9. why won't this code work?

10. Why won't this code work in NT

11. Q: Getting Usernames in Win 95

12. Getting UserName from Win 95

 

 
Powered by phpBB® Forum Software