Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Visual Basic question

Posted on 1997-09-12
11
Medium Priority
?
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 800 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

715 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