Get Corresponding Key to HKEY_CURRENT_USER in HKEY_USER

How do I get the key that corresponds the HKEY_CURRENT_USER thats in HKEY_USER? It's something like HKEY_USERS\S-????????????????????????????????????
LVL 22
orangutangAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
jkaiosConnect With a Mentor IT DirectorCommented:
The API's to check are:

  1. LookupAccountSID - This function accepts a security identifier (SID) as input. It retrieves the name of the account for this SID and the name of the first domain on which this SID is found.

  2. LookupAccountName - This function accepts the name of a system and an account as input. It retrieves a security identifier (SID) for the account and the name of the domain on which the account was found.

0
 
gecko_au2003Commented:
You mean the SID of the current user ?

Also what language are you using exactly ?

vb 6, vbscript, vb.net 2003, vb.net 2005 ?
0
 
orangutangAuthor Commented:
VB6. I think it is the SID. Can you please give me a code to get that?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gecko_au2003Commented:
A PAQ that I answered before some time ago :

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21680715.html

The code it self is here :

For reference, here is the full code I used:

Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal UserName As String, ByRef BuffSize As Long) As Long

Public Function GetMySID()
Dim objWMIService, colItems
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Account", , 48)
For Each objitem In colItems
    If objitem.Name = CurrentUser Then
        GetMySID = objitem.sid
        Exit For
    End If
Next
End Function

Public Function CurrentUser() As String
Dim sBuff As String * 512
Dim X As Long
CurrentUser = ""
X = GetUserName(sBuff, Len(sBuff) - 1)
If X > 0 Then
X = InStr(sBuff, vbNullChar)
If X > 0 Then CurrentUser = Left$(sBuff, X - 1)
End If
End Function
0
 
orangutangAuthor Commented:
But is there some other way to get it using APIs? I'm almost sure I saw some example like that in C++
0
 
gecko_au2003Commented:
maybe you can show me the c++ example ?
0
 
orangutangAuthor Commented:
That's the point, I don't remember where I saw it.
0
 
orangutangAuthor Commented:
Maybe this could be of help but I'm sure this isn't the code I saw:

http://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/Q_21938879.html
0
 
gecko_au2003Commented:
That is just using the registry and reading the registry keys that you need and using if else statements from what I can see. Maybe im wrong as Im no c++ expert but the above example works afaik
0
 
orangutangAuthor Commented:
Yeah, that code works but I just don't really like using winmgmts or maybe I just like using APIs more.
0
 
gecko_au2003Commented:
Might be worth while putting a pointer question from the C++ TA or relevant programming TA to here and asking someone to port the c++ code to vb 6, if thats what you are after :)

I would do it myself but dont have access to vb 6 to test it nor do I have the time or experience with c++ to be able to decipher what some of the code does.
0
 
orangutangAuthor Commented:
If you can find one that uses APIs, I'll raise the points.
0
 
gecko_au2003Commented:
I think I found what you are looking for here :

http://vbcode.com/asp/showsn.asp?theID=3795

Let me know if that fufills your requirements :)
0
 
orangutangAuthor Commented:
But where is the SID? It should look something like S-?-?-???????-????-???-??
0
 
gecko_au2003Connect With a Mentor Commented:
read the code comments .... If that does not help I can give it a go later on and test it out
0
 
jkaiosIT DirectorCommented:
See if this can help:

   http://www.mvps.org/access/api/api0062.htm
0
 
orangutangAuthor Commented:
Well, the example should return something like GetMySID from the first reply.
0
 
orangutangAuthor Commented:
Do you think you could post a code example? Thanks
0
 
orangutangAuthor Commented:
I found this:

http://support.microsoft.com/kb/286182

I'm just going to use the Local_Convert_BIN_To_SDDL function
Thanks everyone for the help
0
 
jkaiosIT DirectorCommented:
Try this:


Private Sub Command1_Click()
   Debug.Print GetSid("DOMAIN\Joe")  '<-- replace DOMAIN with the actual name of your domain and Joe with a valid domain userID
End Sub

'=================================================================================================
Option Explicit

Private Declare Function ConvertSidToStringSid Lib "ADVAPI32.dll" Alias "ConvertSidToStringSidA" (ByVal Sid As String, ByRef lpStringSid As Long) As Long
Private Declare Function IsValidSid Lib "ADVAPI32.dll" (ByRef pSid As Any) As Long
Private Declare Function LookupAccountName Lib "ADVAPI32.dll" Alias "LookupAccountNameA" (ByVal lpSystemName As String, ByVal lpAccountName As String, ByVal Sid As String, ByRef cbSid As Long, ByVal ReferencedDomainName As String, ByRef cbReferencedDomainName As Long, ByRef peUse As Long) As Long
Private Declare Function LocalFree Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)

Public Function GetSid(ByVal sAccountName As String) As String

   Dim userName As String
   Dim UserNameSize As Long
   Dim Sid As String
   Dim SidSize As Long
   Dim Domain As String
   Dim DomainSize As Long
   Dim snu As Long
   Dim sReturn As String
   
   userName = String$(255, 0&)
   Sid = String$(255, 0&)
   Domain = String$(255, 0&)
   sReturn = String$(255, 0&)
   
   UserNameSize = 255
   SidSize = 255
   DomainSize = 255
   
   Call LookupAccountName(vbNullString, sAccountName, Sid, SidSize, Domain, DomainSize, snu)
   If IsValidSid(ByVal Sid) = 0& Then Exit Function
   
   Call ConvertSidToStringSid(Sid, snu)
   Call CopyMemory(ByVal sReturn, ByVal snu, 255)
   
   sReturn = Left$(sReturn, InStr(vbNull, sReturn, vbNullChar, vbBinaryCompare) - vbNull)
   Call LocalFree(snu)
   
   GetSid = sReturn

End Function
'=================================================================================================
0
 
orangutangAuthor Commented:
I'm having a slight problem with the code. I'm trying to clean it up and get rid of unneeded functions and APIs. I can't get the code to work if I put in "Option Explicit"
0
 
orangutangAuthor Commented:
Thanks for the code, jkaios! But I'm just curious why I can't get the Local_Convert_BIN_To_SDDL function from my previous post to work with "Option Explicit". Can you figure out why?
0
 
jkaiosIT DirectorCommented:
The problem is that some of the variables (such as the pDomain and IReturn variables) that are used in that particular function are declared "implicitly", meaning they are NOT declared using the Dim keyword.  Therefore, when the Option Explicit is turned on, it tells Visual Basic that BEFORE each variable are used, it MUST BE declared by using either the Private or the Dim keywords.

To make the long story short, the example provided by Microsoft was simply a "quick and dirty" solution as it does not completely follow the best practices of "managed code".

Hope this answers your question regarding the inconvenience in the Local_Convert_BIN_To_SDDL function.
0
 
orangutangAuthor Commented:
Yeah, that's what I did. I declared all of the variables and then VB crashed when I ran it. Try it yourself. Maybe I didn't dim them correctly.
0
 
orangutangAuthor Commented:
An what do the other registry keys in HKEY_USERS correspond to? (The shorter keys such as S-?-?-??)
0
 
jkaiosIT DirectorCommented:
The shortest ones represent the "built-in" accounts, such as Everyone, Network Service, Interactive, Anonymous Logon, System, etc.
0
 
orangutangAuthor Commented:
Okay, thanks. But can you figure out why the Local_Convert_BIN_To_SDDL function crashes VB when I put "Option Explicit" in it and declare the variables?
0
 
jkaiosIT DirectorCommented:
It'll take some time to figure that out, but in the meantime try turning off the option explicit (remove it) if that's what causes the problem.
0
 
orangutangAuthor Commented:
Well, have you tried Option Explicit yet? If you did, did VB crash?
0
 
jkaiosIT DirectorCommented:
Nope, and I don't even bother because all I need (if I were the asker) to to get the SID for the Current User.

Why do I have to bother with the buggy Local_Convert_BIN_To_SDDL function if (again, if I were the asker) what I asked for has already been provided AND is working perfectly?
0
 
orangutangAuthor Commented:
I'm just curious, that's all. You don't have to figure it out. I'm not even going to use that Microsoft code but I would like to know, maybe for future references, why the Microsoft code doesn't work with "Option Explicit".
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.