How to use 'GetUserName' function

A

Adam

Hi All,

I want to get the windows logon name from users machines so that I can
verify whether they should access a form or not.

I've found the below code but dont know what to do with it:

'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If ( lngX > 0 ) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
'******************** Code End **************************

Please can someone advise how to use this?

Adam
 
M

microb0x

Put the following into its own module:

~~~~~Start of code paste~~~~~~

Option Compare Database
Option Explicit

Type WKSTA_INFO_101
wki101_platform_id As Long
wki101_computername As Long
wki101_langroup As Long
wki101_ver_major As Long
wki101_ver_minor As Long
wki101_lanroot As Long
End Type

Type WKSTA_USER_INFO_1
wkui1_username As Long
wkui1_logon_domain As Long
wkui1_logon_server As Long
wkui1_oth_domains As Long
End Type

Declare Function WNetGetUser& Lib "Mpr" Alias "WNetGetUserA" _
(lpName As Any, ByVal lpUserName$, lpnLength&)
Declare Function NetWkstaGetInfo& Lib "Netapi32" _
(strServer As Any, ByVal lLevel&, pbBuffer As Any)
Declare Function NetWkstaUserGetInfo& Lib "Netapi32" _
(reserved As Any, ByVal lLevel&, pbBuffer As Any)
Declare Sub lstrcpyW Lib "Kernel32" (dest As Any, ByVal src As Any)
Declare Sub lstrcpy Lib "Kernel32" (dest As Any, ByVal src As Any)
Declare Sub RtlMoveMemory Lib "Kernel32" _
(dest As Any, src As Any, ByVal size&)
Declare Function NetApiBufferFree& Lib "Netapi32" (ByVal buffer&)

Function GetWorkstationInfo()

Dim ret As Long, buffer(512) As Byte, i As Integer
Dim wk101 As WKSTA_INFO_101, pwk101 As Long
Dim wk1 As WKSTA_USER_INFO_1, pwk1 As Long
Dim cbusername As Long, username As String
Dim computername As String, langroup As String, logondomain As String


' Clear all of the display values.
computername = "": langroup = "": username = "": logondomain = ""


' Windows 95 or NT - call WNetGetUser to get the name of the user.
username = Space(256)
cbusername = Len(username)
ret = WNetGetUser(ByVal 0&, username, cbusername)
If ret = 0 Then
' Success - strip off the null.
username = Left(username, InStr(username, Chr(0)) - 1)
Else
username = ""
End If


'================================================================
' The On Error statement will allow Netware users to retrieve
Information
'================================================================


On Error Resume Next
'================================================================
' The following section works only under Windows NT
'================================================================


'NT only - call NetWkstaGetInfo to get computer name and lan Group
ret = NetWkstaGetInfo(ByVal 0&, 101, pwk101)
RtlMoveMemory wk101, ByVal pwk101, Len(wk101)
lstrcpyW buffer(0), wk101.wki101_computername
' Get every other byte from Unicode string.
i = 0
Do While buffer(i) <> 0
computername = computername & Chr(buffer(i))
i = i + 2
Loop
lstrcpyW buffer(0), wk101.wki101_langroup
i = 0
Do While buffer(i) <> 0
langroup = langroup & Chr(buffer(i))
i = i + 2
Loop
ret = NetApiBufferFree(pwk101)


' NT only - call NetWkstaUserGetInfo.
ret = NetWkstaUserGetInfo(ByVal 0&, 1, pwk1)
RtlMoveMemory wk1, ByVal pwk1, Len(wk1)
lstrcpyW buffer(0), wk1.wkui1_logon_domain
i = 0
Do While buffer(i) <> 0
logondomain = logondomain & Chr(buffer(i))
i = i + 2
Loop
ret = NetApiBufferFree(pwk1)


'================================================================
'End NT-specific section
'================================================================

vUserLogin = username
'Debug.Print computername, langroup, username, logondomain
'above is other system information that can be collected with this
module

End Function


~~~~~End of code paste~~~~~~

Then declare the following variable somewhere in your project:

Public vUserLogin As String


Then when you need to call this function use:

Call GetWorkstationInfo


The user's workstation ID will be placed in the variable I asked you to
declare.

let me know if you have any issues as I use this exact code in about 20
of my apps.
 
D

Douglas J. Steele

Create a new module (not a class module, and not a module associated with a
form).

Copy that code to that new module and save the module. Make sure you do not
name the module fOSUserName: the module name must be different than any
functions.

In your application, put fOSUserName() anywhere you want the user's ID.
 
A

Adam

Brilliant, thanks Doug

Create a new module (not a class module, and not a module associated with a
form).

Copy that code to that new module and save the module. Make sure you do not
name the module fOSUserName: the module name must be different than any
functions.

In your application, put fOSUserName() anywhere you want the user's ID.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top