Welcome to SPN

Register and Join the most happening forum of Sikh community & intellectuals from around the world.

Sign Up Now!

How to use 'GetUserName' function

Discussion in 'Information Technology' started by Adam@nospam.com, Jul 28, 2006.

  1. Adam@nospam.com

    Adam@nospam.com
    Expand Collapse
    Guest

    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
     
  2. Loading...


  3. microb0x

    microb0x
    Expand Collapse
    Guest

    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.





    Adam@nospam.com wrote:
    > 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
     
  4. Douglas J. Steele

    Douglas J. Steele
    Expand Collapse
    Guest

    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.

    --
    Doug Steele, Microsoft Access MVP
    http://I.Am/DougSteele
    (no private e-mails, please)


    "Adam@nospam.com" <adamevans81@gmail.com> wrote in message
    news:1151356532.657405.238450@p79g2000cwp.googlegroups.com...
    > 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
    >
     
  5. Adam@nospam.com

    Adam@nospam.com
    Expand Collapse
    Guest

    Brilliant, thanks Doug


    Douglas J. Steele wrote:
    > 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.
    >
    > --
    > Doug Steele, Microsoft Access MVP
    > http://I.Am/DougSteele
    > (no private e-mails, please)
    >
    >
    > "Adam@nospam.com" <adamevans81@gmail.com> wrote in message
    > news:1151356532.657405.238450@p79g2000cwp.googlegroups.com...
    > > 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
    > >
     

Share This Page