Solved

Get Corresponding Key to HKEY_CURRENT_USER in HKEY_USER

Posted on 2006-11-22
31
754 Views
Last Modified: 2010-07-27
How do I get the key that corresponds the HKEY_CURRENT_USER thats in HKEY_USER? It's something like HKEY_USERS\S-????????????????????????????????????
0
Comment
Question by:orangutang
  • 17
  • 7
  • 7
31 Comments
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17997968
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
 
LVL 22

Author Comment

by:orangutang
ID: 17998002
VB6. I think it is the SID. Can you please give me a code to get that?
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17998029
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
 
LVL 22

Author Comment

by:orangutang
ID: 17998148
But is there some other way to get it using APIs? I'm almost sure I saw some example like that in C++
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17998211
maybe you can show me the c++ example ?
0
 
LVL 22

Author Comment

by:orangutang
ID: 17998219
That's the point, I don't remember where I saw it.
0
 
LVL 22

Author Comment

by:orangutang
ID: 17998234
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
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17998264
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
 
LVL 22

Author Comment

by:orangutang
ID: 17998288
Yeah, that code works but I just don't really like using winmgmts or maybe I just like using APIs more.
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17998336
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
 
LVL 22

Author Comment

by:orangutang
ID: 17999386
If you can find one that uses APIs, I'll raise the points.
0
 
LVL 23

Expert Comment

by:gecko_au2003
ID: 17999819
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
 
LVL 22

Author Comment

by:orangutang
ID: 18000664
But where is the SID? It should look something like S-?-?-???????-????-???-??
0
 
LVL 23

Assisted Solution

by:gecko_au2003
gecko_au2003 earned 250 total points
ID: 18001312
read the code comments .... If that does not help I can give it a go later on and test it out
0
 
LVL 12

Expert Comment

by:jkaios
ID: 18005048
See if this can help:

   http://www.mvps.org/access/api/api0062.htm
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Author Comment

by:orangutang
ID: 18005836
Well, the example should return something like GetMySID from the first reply.
0
 
LVL 12

Accepted Solution

by:
jkaios earned 250 total points
ID: 18015305
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
 
LVL 22

Author Comment

by:orangutang
ID: 18016021
Do you think you could post a code example? Thanks
0
 
LVL 22

Author Comment

by:orangutang
ID: 18024228
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
 
LVL 12

Expert Comment

by:jkaios
ID: 18024236
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
 
LVL 22

Author Comment

by:orangutang
ID: 18024273
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
 
LVL 22

Author Comment

by:orangutang
ID: 18024290
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
 
LVL 12

Expert Comment

by:jkaios
ID: 18025869
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
 
LVL 22

Author Comment

by:orangutang
ID: 18025955
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
 
LVL 22

Author Comment

by:orangutang
ID: 18030257
An what do the other registry keys in HKEY_USERS correspond to? (The shorter keys such as S-?-?-??)
0
 
LVL 12

Expert Comment

by:jkaios
ID: 18031168
The shortest ones represent the "built-in" accounts, such as Everyone, Network Service, Interactive, Anonymous Logon, System, etc.
0
 
LVL 22

Author Comment

by:orangutang
ID: 18032714
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
 
LVL 12

Expert Comment

by:jkaios
ID: 18040229
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
 
LVL 22

Author Comment

by:orangutang
ID: 18041437
Well, have you tried Option Explicit yet? If you did, did VB crash?
0
 
LVL 12

Expert Comment

by:jkaios
ID: 18042136
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
 
LVL 22

Author Comment

by:orangutang
ID: 18042338
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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now