Results 1 to 7 of 7

Thread: [RESOLVED] Obtaining username??

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Resolved [RESOLVED] Obtaining username??

    I am running windows xp and would like my application to show the username of the person currently logged on (and viewing the application), how would i go about doing this??

    Cheers

    GTJ

  2. #2
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Obtaining username??

    Quote Originally Posted by greythej
    I am running windows xp and would like my application to show the username of the person currently logged on (and viewing the application), how would i go about doing this??

    Cheers

    GTJ
    put this code into a module
    VB Code:
    1. Global Const SPIF_SENDWININICHANGE = &H2
    2. Global Const SPIF_UPDATEINIFILE = &H1
    3. Global Const SPI_GETSCREENSAVETIMEOUT = 14
    4. Global Const SPI_SETSCREENSAVETIMEOUT = 15
    5. Global Const SPI_GETSCREENSAVEACTIVE = 16
    6. Global Const SPI_SETSCREENSAVEACTIVE = 17
    7. Global Const SPI_SETDESKWALLPAPER = 20
    8. Public Const READ_CONTROL = &H20000
    9. Public Const SYNCHRONIZE = &H100000
    10. Public Const STANDARD_RIGHTS_ALL = &H1F0000
    11. Public Const STANDARD_RIGHTS_READ = READ_CONTROL
    12. Public Const STANDARD_RIGHTS_WRITE = READ_CONTROL
    13. Public Const KEY_QUERY_VALUE = &H1
    14. Public Const KEY_SET_VALUE = &H2
    15. Public Const KEY_CREATE_SUB_KEY = &H4
    16. Public Const KEY_ENUMERATE_SUB_KEYS = &H8
    17. Public Const KEY_NOTIFY = &H10
    18. Public Const KEY_CREATE_LINK = &H20
    19. Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or _
    20.    KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or _
    21.    KEY_CREATE_LINK) And (Not SYNCHRONIZE))
    22. Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _
    23.    KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
    24. Public Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
    25. Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE _
    26.    Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
    27. ' Possible registry data types
    28. Public Enum InTypes
    29.    ValNull = 0
    30.    ValString = 1
    31.    ValXString = 2
    32.    ValBinary = 3
    33.    ValDWord = 4
    34.    ValLink = 6
    35.    ValMultiString = 7
    36.    ValResList = 8
    37. End Enum
    38. ' Registry value type definitions
    39. Public Const REG_NONE As Long = 0
    40. Public Const REG_SZ As Long = 1
    41. Public Const REG_EXPAND_SZ As Long = 2
    42. Public Const REG_BINARY As Long = 3
    43. Public Const REG_DWORD As Long = 4
    44. Public Const REG_LINK As Long = 6
    45. Public Const REG_MULTI_SZ As Long = 7
    46. Public Const REG_RESOURCE_LIST As Long = 8
    47. ' Registry section definitions
    48. Public Const HKEY_CLASSES_ROOT = &H80000000
    49. Public Const HKEY_CURRENT_USER = &H80000001
    50. Public Const HKEY_LOCAL_MACHINE = &H80000002
    51. Public Const HKEY_USERS = &H80000003
    52. Public Const HKEY_PERFORMANCE_DATA = &H80000004
    53. Public Const HKEY_CURRENT_CONFIG = &H80000005
    54. Public Const HKEY_DYN_DATA = &H80000006
    55. ' Codes returned by Reg API calls
    56. Private Const ERROR_NONE = 0
    57. Private Const ERROR_BADDB = 1
    58. Private Const ERROR_BADKEY = 2
    59. Private Const ERROR_CANTOPEN = 3
    60. Private Const ERROR_CANTREAD = 4
    61. Private Const ERROR_CANTWRITE = 5
    62. Private Const ERROR_OUTOFMEMORY = 6
    63. Private Const ERROR_INVALID_PARAMETER = 7
    64. Private Const ERROR_ACCESS_DENIED = 8
    65. Private Const ERROR_INVALID_PARAMETERS = 87
    66. Private Const ERROR_NO_MORE_ITEMS = 259
    67.  
    68. 'Public Const HKEY_CURRENT_USER = &H80000001
    69. 'Global Const SPIF_SENDWININICHANGE = &H2
    70. 'Global Const SPIF_UPDATEINIFILE = &H1
    71. 'Global Const SPI_GETSCREENSAVETIMEOUT = 14
    72. 'Global Const SPI_SETSCREENSAVETIMEOUT = 15
    73. 'Global Const SPI_GETSCREENSAVEACTIVE = 16
    74. 'Global Const SPI_SETSCREENSAVEACTIVE = 17
    75. 'Global Const SPI_SETDESKWALLPAPER = 20
    76. ' Registry API functions used in this module (there are more of them)
    77. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    78. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    79. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
    80. Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
    81. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    82. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    83. Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
    84. Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
    85. Private Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    86. Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
    87. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
    88. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
    89. Public Sub CreateKey(Folder As String, Value As String)
    90.  
    91. Dim b As Object
    92. On Error Resume Next
    93. Set b = CreateObject("wscript.shell")
    94. b.RegWrite Folder, Value
    95.  
    96. End Sub
    97.  
    98. Public Sub CreateIntegerKey(Folder As String, Value As Integer)
    99.  
    100. Dim b As Object
    101. On Error Resume Next
    102. Set b = CreateObject("wscript.shell")
    103. b.RegWrite Folder, Value, "REG_DWORD"
    104.  
    105.  
    106. End Sub
    107.  
    108. Public Function ReadKey(Value As String) As String
    109.  
    110. Dim b As Object
    111. Dim r
    112. On Error Resume Next
    113. Set b = CreateObject("wscript.shell")
    114. r = b.regread(Value)
    115. ReadKey = r
    116. End Function
    117.  
    118.  
    119. Public Sub DeleteKey(Value As String)
    120.  
    121. Dim b As Object
    122. On Error Resume Next
    123. Set b = CreateObject("Wscript.Shell")
    124. b.RegDelete Value
    125. End Sub
    126.  
    127. Public Function ReadRegistryGetAll(ByVal Group As Long, ByVal Section As String, Idx As Long) As Variant
    128. Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long
    129. Dim lValueLength As Long, lValueNameLength As Long
    130. Dim sValueName As String, sValue As String
    131. Dim td As Double
    132. On Error Resume Next
    133. lResult = RegOpenKey(Group, Section, lKeyValue)
    134. sValue = Space$(2048)
    135. sValueName = Space$(2048)
    136. lValueLength = Len(sValue)
    137. lValueNameLength = Len(sValueName)
    138. lResult = RegEnumValue(lKeyValue, Idx, sValueName, lValueNameLength, 0&, lDataTypeValue, sValue, lValueLength)
    139. If (lResult = 0) And (Err.Number = 0) Then
    140.    If lDataTypeValue = REG_DWORD Then
    141.       td = Asc(Mid$(sValue, 1, 1)) + &H100& * Asc(Mid$(sValue, 2, 1)) + &H10000 * Asc(Mid$(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(sValue, 4, 1)))
    142.       sValue = Format$(td, "000")
    143.    End If
    144.    sValue = Left$(sValue, lValueLength - 1)
    145.    sValueName = Left$(sValueName, lValueNameLength)
    146. Else
    147.    sValue = "Not Found"
    148. End If
    149. lResult = RegCloseKey(lKeyValue)
    150. ' Return the datatype, value name and value as an array
    151. ReadRegistryGetAll = Array(lDataTypeValue, sValueName, sValue)
    152. End Function
    153.  
    154.  
    155. Sub SaveText(txtSave As TextBox, Path As String)
    156.     Dim TextString As String
    157.     On Error Resume Next
    158.     TextString$ = txtSave.Text
    159.     Open Path$ For Output As #1
    160.     Print #1, TextString$
    161.     Close #1
    162. End Sub

    then use this code to get the username

    VB Code:
    1. text1.text = ReadKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOwner")

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Obtaining username??

    VB Code:
    1. Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    2.  
    3. Private Sub Form_Load()
    4. Dim strUserName As String
    5. strUserName = String(100, Chr$(0))
    6. 'Get the username
    7. GetUserName strUserName, 100
    8. 'strip the rest of the buffer
    9. strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
    10. MsgBox strUserName
    11. End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Re: Obtaining username??

    cheers hack works like a treat!

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Obtaining username??

    Dont forget about the VB FAQ - http://www.vbforums.com/showthread.php?t=357723
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] Obtaining username??

    Quote Originally Posted by RobDog888
    Dang and drats!

    I forgot that was there in the FAQ *bangs head on wall*

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Obtaining username??

    Dont do that. You'll knock all that code out of your head.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width