Apr 19th, 2004, 12:47 PM
#1
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Apr 19th, 2004, 09:48 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Apr 19th, 2004, 10:26 PM
#3
Frenzied Member
What OS? I don't have any SID on XP.
Apr 20th, 2004, 12:00 AM
#4
2000 Pro SP4 & another system with XP SP1.
On your XP there is no sid under the HKEY_USER?
Its on mine.
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Apr 20th, 2004, 01:58 AM
#5
Frenzied Member
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.
Apr 20th, 2004, 02:10 AM
#6
Frenzied Member
Another link that might help you out.
Apr 22nd, 2004, 10:40 AM
#7
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:
Private Sub Form_Load()
Const ADS_SID_HEXSTRING = 1
Const ADS_SID_WINNT_PATH = 5
Const ADS_RIGHT_EXCH_MODIFY_USER_ATT = &H2
Const ADS_RIGHT_EXCH_MAIL_SEND_AS = &H8
Const ADS_RIGHT_EXCH_MAIL_RECEIVE_AS = &H10
Dim sid As New ADsSID
Dim sec As New ADsSecurity
Dim oSID As Variant
Dim sSID As String
' Gettting a security descriptor
Dim x As IADs
Dim y As IADs
Dim sd As IADsSecurityDescriptor
Dim acl As IADsAccessControlList
Set x = GetObject("LDAP://DC=mydomain, DC=com")
Set sd = x.Get("ntSecurityDescriptor")
Debug.Print "sd.Control: " & sd.Control
Debug.Print "sd.Group: " & sd.Group
Debug.Print "sd.Owner: " & sd.Owner
Debug.Print "sd.Revision: " & sd.Revision
Debug.Print "sd.DaclDefaulted: " & sd.DaclDefaulted
Debug.Print "sd.SaclDefaulted: " & sd.SaclDefaulted
Set acl = sd.DiscretionaryAcl
Debug.Print "acl.AceCount: " & acl.AceCount
Debug.Print "acl.AclRevision: " & acl.AclRevision
Set sacl = sd.SystemAcl
Set y = GetObject("WinNT://myserver/me,user")
oSID = y.Get("objectSID")
For i = LBound(oSID) To UBound(oSID)
sSID = sSID & CStr(oSID(i)) & ", "
Next
Debug.Print "sSID: " & Mid$(sSID, 1, InStrRev(sSID, ",") - 1)
sid.SetAs ADS_SID_WINNT_PATH, "WinNT://myserver/me,user"
sidHex = sid.GetAs(ADS_SID_HEXSTRING)
Debug.Print "sidHex: " & sidHex
'Results like...
'sd.Control: 35888
'sd.Group: BUILTIN\Administrators
'sd.Owner: BUILTIN\Administrators
'sd.Revision: 1
'sd.DaclDefaulted: False
'sd.SaclDefaulted: False
'acl.AceCount: 21
'acl.AclRevision: 4
'sSID: 1, 5, 0, 0, 0, 0, 0, 5, 21, 0, 0, 0, 110, 57...
'sidHex: 0105000000000005150000006E3900729...
'Registry SID = "S-1-5-21-1912617...-...
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Apr 27th, 2004, 11:46 AM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Apr 27th, 2004, 12:54 PM
#9
Frenzied Member
What are these types defined?
Dim sid As New ADsSID
Dim sec As New ADsSecurity
Dim sd As IADsSecurityDescriptor
Dim acl As IADsAccessControlList
Apr 27th, 2004, 01:11 PM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
May 28th, 2004, 12:12 PM
#11
Addicted Member
Robdog,
Can you post a sample of the code you used to get this to work? I have been looking everywhere...
Thanks.
May 28th, 2004, 12:26 PM
#12
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
May 28th, 2004, 01:02 PM
#13
Addicted Member
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.
May 28th, 2004, 01:06 PM
#14
Place in module then call from anywhere in your program.
VB Code:
Option Explicit
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long
Public Function CurrentUser() As String
'*********************************************************
'* Function to get the current logged on user in windows *
'*********************************************************
Dim strBuff As String * 512
Dim x As Long
CurrentUser = ""
x = GetUserName(strBuff, Len(strBuff) - 1)
If x > 0 Then
x = InStr(strBuff, vbNullChar)
If x > 0 Then
CurrentUser = UCase(Left$(strBuff, x - 1))
End If
End If
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
May 28th, 2004, 05:59 PM
#15
Addicted Member
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?
May 28th, 2004, 06:03 PM
#16
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
Forum Rules
Click Here to Expand Forum to Full Width