Solved

Visual Basic question

Posted on 1997-09-12
11
477 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

14 Experts available now in Live!

Get 1:1 Help Now