Solved

Visual Basic question

Posted on 1997-09-12
11
497 Views
Last Modified: 2013-11-25
How can I use the RegLoadKey API call in a Visual Basic 4.0 to load a Windows NT 4.0 user profile hive?  If possible, please give a detailed example.
0
Comment
Question by:tomlinsorm
  • 4
  • 4
  • 3
11 Comments
 
LVL 4

Expert Comment

by:Chizl
ID: 1434888
I have all the code you need for accessing registries using VB5..   I'm sure that the code is easlily converted to VB4 since it is all 32bit API calls anyway..  I put in on http://www.karland.com/registry/ 

L8r,
Chizl
0
 

Author Comment

by:tomlinsorm
ID: 1434889
On going to http://www.karland.com/registry, I found no mention whatsoever that mentioned RegLoadKey or anything else to load an NT 4.0 user profile hive.
0
 
LVL 4

Expert Comment

by:Chizl
ID: 1434890
32bit call to a registry is a 32bit call.  I'm not using RegLoadKey, but there are other 32bit registry calls there that do the exact same thing..

L8r,
Chizl
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Expert Comment

by:y96andha
ID: 1434891
What exactly is it that you want to do? Will it be enough if I write you a sample on how to load it and how to remove it from memory when done with it?
0
 

Author Comment

by:tomlinsorm
ID: 1434892
My apologies if I wasn't specific enough.  I would like to write a VB4 program that will load and unload an abitrary user's profile hive (c:\winnt\profiles\username\ntuser.dat)into the registry under the HKEY_USERS\username key.  In addition, I hope to be able to do this from the command line.  All I need to know are the command(s) to load the hive.  It's similiar to what can be accomplished by going to regedt32, selecting HKEY_USERS, going to File and clicking "Load Hive", giving it the path c:\winnt\profiles\username\ntuser.dat and telling WinNT it's arbitrary name under the KEY_USERS key (ie. the username).  Any and all help has been and is appreciated.  

0
 
LVL 5

Accepted Solution

by:
y96andha earned 200 total points
ID: 1434893
Here is an example I wrote for you. You need to enable SeRestorePrivilege before you can call the RegLoadKey function.


Private Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
Private Const HKEY_USERS = &H80000003

Private Const SE_RESTORE_PRIVILEGE_NAME = "SeRestorePrivilege"
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8



Private Type LUID
    LowPart As Long
    HighPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
        LowPart As Long
        HighPart As Long
        Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    LowPart As Long
    HighPart As Long
    Attributes As Long
End Type

Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, ByVal PreviousState As Long, ByVal ReturnLength As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Sub Command1_Click()
    Dim tok As Long
    Dim sht As LUID
    Dim st As TOKEN_PRIVILEGES
    Dim success As Long
    success = OpenProcessToken(GetCurrentProcess, TOKEN_QUERY + TOKEN_ADJUST_PRIVILEGES, tok)
    Debug.Print "OpenProcessToken "; success
    success = LookupPrivilegeValue(vbNullString, SE_RESTORE_PRIVILEGE_NAME, sht)
    Debug.Print "LookupPrivilegeValue "; success
    st.PrivilegeCount = 1
    st.LowPart = sht.LowPart
    st.HighPart = sht.HighPart
    st.Attributes = SE_PRIVILEGE_ENABLED
    success = AdjustTokenPrivileges(tok, 0, st, 0, 0, 0)
    Debug.Print "AdjustTokenPrivileges "; success
    Debug.Print "RegLoadKey "; RegLoadKey(HKEY_USERS, "tobias" & Chr(0), "f:\winnt\profiles\Tobias\ntuser.dat" & Chr(0))
    st.Attributes = 0
    success = AdjustTokenPrivileges(tok, 0, st, 0, 0, 0)
    Debug.Print "AdjustTokenPrivileges "; success
End Sub

0
 

Author Comment

by:tomlinsorm
ID: 1434894
Thank you, y96andha, for your answer.  It went above and beyond the required.
0
 
LVL 4

Expert Comment

by:Chizl
ID: 1434895
y96andha...
Is this also possible in Win95?

L8r,
Chizl
0
 
LVL 5

Expert Comment

by:y96andha
ID: 1434896
The docs say that the RegLoadKey function is supported in Windows 95. So it should be possible. The other functions are not supported though, so I guess that just
 Debug.Print "RegLoadKey "; RegLoadKey(HKEY_USERS, "tobias" & Chr(0), "f:\winnt\profiles\Tobias\ntuser.dat" & Chr(0))
should be enough on Win95. I haven't tested it though.
0
 

Author Comment

by:tomlinsorm
ID: 1434897
FYI, I was not able to get it to work under Win95, even when I renamed the "ntuser.dat" file to "ntuser.".  I may have been going about it the wrong way, though.
0
 
LVL 5

Expert Comment

by:y96andha
ID: 1434898
I don't think you can load an NT profile under Win95, but a Win95 profile should be fine. NT has security settings for the registry hives, Win95 has not, so the file formats are probably different between them.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

770 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