Results 1 to 16 of 16

Thread: Retrieve SID of current user

  1. #1

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

    Question Retrieve SID of current user

    I need to read and write to the current logged in users
    HKEY_USERS\SID#\Software\Microsoft\Windows
    NT\CurrentVersion\Windows Messaging Subsystem\ hive, but if a
    system has more than one user that has logged on there will be
    more than one set of registry sid keys. How can I get the sid from
    the current username? I can get the username, but I need to be
    able to map it to the sid#.

    Thanks in advance for any assistance.
    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

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Bump???
    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

  3. #3
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    What OS? I don't have any SID on XP.
    Please rate my post.

  4. #4

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    2000 Pro SP4 & another system with XP SP1.
    On your XP there is no sid under the HKEY_USER?

    Its on mine.

    Attached Images Attached Images  
    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

  5. #5
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    K - I'm going off the thought that you have the API-Guide installed so look at the example for advapi32.dll's LookupAccountName function.
    Please rate my post.

  6. #6
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Another link that might help you out.
    Please rate my post.

  7. #7

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Thanks for the link, but I am getting the SID now and it doesn't
    match the registry SID. Looks like there is some alogrithim used on it.
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Const ADS_SID_HEXSTRING = 1
    4.     Const ADS_SID_WINNT_PATH = 5
    5.     Const ADS_RIGHT_EXCH_MODIFY_USER_ATT = &H2
    6.     Const ADS_RIGHT_EXCH_MAIL_SEND_AS = &H8
    7.     Const ADS_RIGHT_EXCH_MAIL_RECEIVE_AS = &H10
    8.  
    9.     Dim sid As New ADsSID
    10.     Dim sec As New ADsSecurity
    11.     Dim oSID As Variant
    12.     Dim sSID As String
    13.    
    14.     ' Gettting a security descriptor
    15.     Dim x As IADs
    16.     Dim y As IADs
    17.     Dim sd As IADsSecurityDescriptor
    18.     Dim acl As IADsAccessControlList
    19.    
    20.     Set x = GetObject("LDAP://DC=mydomain, DC=com")
    21.     Set sd = x.Get("ntSecurityDescriptor")
    22.      
    23.     Debug.Print "sd.Control: " & sd.Control
    24.     Debug.Print "sd.Group: " & sd.Group
    25.     Debug.Print "sd.Owner: " & sd.Owner
    26.     Debug.Print "sd.Revision: " & sd.Revision
    27.     Debug.Print "sd.DaclDefaulted: " & sd.DaclDefaulted
    28.     Debug.Print "sd.SaclDefaulted: " & sd.SaclDefaulted
    29.    
    30.     Set acl = sd.DiscretionaryAcl
    31.     Debug.Print "acl.AceCount: " & acl.AceCount
    32.     Debug.Print "acl.AclRevision: " & acl.AclRevision
    33.    
    34.     Set sacl = sd.SystemAcl
    35.  
    36.     Set y = GetObject("WinNT://myserver/me,user")
    37.     oSID = y.Get("objectSID")
    38.     For i = LBound(oSID) To UBound(oSID)
    39.         sSID = sSID & CStr(oSID(i)) & ", "
    40.     Next
    41.     Debug.Print "sSID: " & Mid$(sSID, 1, InStrRev(sSID, ",") - 1)
    42.    
    43.     sid.SetAs ADS_SID_WINNT_PATH, "WinNT://myserver/me,user"
    44.     sidHex = sid.GetAs(ADS_SID_HEXSTRING)
    45.     Debug.Print "sidHex: " & sidHex
    46.  
    47.     'Results like...
    48.     'sd.Control: 35888
    49.     'sd.Group: BUILTIN\Administrators
    50.     'sd.Owner: BUILTIN\Administrators
    51.     'sd.Revision: 1
    52.     'sd.DaclDefaulted: False
    53.     'sd.SaclDefaulted: False
    54.     'acl.AceCount: 21
    55.     'acl.AclRevision: 4
    56.     'sSID: 1, 5, 0, 0, 0, 0, 0, 5, 21, 0, 0, 0, 110, 57...
    57.     'sidHex: 0105000000000005150000006E3900729...    
    58.     'Registry SID = "S-1-5-21-1912617...-...
    59.  
    60. End Sub
    How can query the registry key if the sid does not look same?
    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

  8. #8

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    *Bump*
    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

  9. #9
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    What are these types defined?

    Dim sid As New ADsSID
    Dim sec As New ADsSecurity
    Dim sd As IADsSecurityDescriptor
    Dim acl As IADsAccessControlList
    Please rate my post.

  10. #10

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    They are reference: "ADsSecurity 2.5 Type Library" (ADsSecurity.dll)

    I got it from the SDK download from M$.

    The IADsSecurityDescriptor and IADsAccessControlList are not
    defined, but I have added references to Active DS Type Library,
    Active DS IIS Extension DLL, Active DS IIS Namespace Provider.
    They are the same references that an example vb project has
    from the sdk.

    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

  11. #11
    Addicted Member
    Join Date
    Mar 2001
    Posts
    152
    Robdog,

    Can you post a sample of the code you used to get this to work? I have been looking everywhere...

    Thanks.

  12. #12

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Never got it to work. Try downloading the SDK from M$.

    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

  13. #13
    Addicted Member
    Join Date
    Mar 2001
    Posts
    152
    Can you at least share a good way to get the current logged in user's name? I am using the following:

    Code:
    Public Declare Function getUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Public Const MAX_BUFFER_LENGTH = 100
    
    Dim strBufferString As String
    Dim lngResult As Long
    
    strBufferString = String(MAX_BUFFER_LENGTH, "X")
    
    lngResult = getUserName(strBufferString, MAX_BUFFER_LENGTH)
    
    getLoggedUserName = Mid(strBufferString, 1, MAX_BUFFER_LENGTH)
    But sometimes get some nasty trailing data after the name and was wondering if there was something a bit more "elegant".

    Thanks again.

  14. #14

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Place in module then call from anywhere in your program.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetUserName Lib "advapi32.dll"  Alias "GetUserNameA" _
    4. (ByVal lpBuffer As String, nSize As Long) As Long
    5.  
    6. Public Function CurrentUser() As String
    7. '*********************************************************
    8. '* Function to get the current logged on user in windows *
    9. '*********************************************************
    10.     Dim strBuff As String * 512
    11.     Dim x As Long
    12.    
    13.     CurrentUser = ""
    14.     x = GetUserName(strBuff, Len(strBuff) - 1)
    15.     If x > 0 Then
    16.         x = InStr(strBuff, vbNullChar)
    17.         If x > 0 Then
    18.             CurrentUser = UCase(Left$(strBuff, x - 1))
    19.         End If
    20.     End If
    21.  
    22. End Function
    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

  15. #15
    Addicted Member
    Join Date
    Mar 2001
    Posts
    152
    Thanks for the clean code. I had one other question on this top and hoped you might be able to help. My application is actually running as an NT Service and when I call GetUserName it always returns SYSTEM as that is the current account in the mind of my app. Is there any way to figure what actual user is using the machine when my service is running in a different context?

  16. #16

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Initially I would have to say no. Since the service is running under
    the system account. The system is running the program. It would
    be hard to determine which user is logged on because there is no
    link between the service and a particular user.
    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