Solved

Visual Basic question

Posted on 1997-09-12
11
488 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA filters 2 51
Problem to open text file 11 73
MsgBox 4 48
VB6 ListBox Question 4 34
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

895 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

15 Experts available now in Live!

Get 1:1 Help Now