?
Solved

Need help with RegSavekey and RegRestoreKey

Posted on 2005-05-13
4
Medium Priority
?
1,009 Views
Last Modified: 2008-02-26
I need help on saving a key from a remote computer, then also having the option of restoring that key if needed. I can get the RegSaveKey and RegRestoreKey to work on my local computer, just not to a remote computer.  I first connect to the registry using "RegConnectRegistry", then I use RegOpenKeyEx, both return a success error code of "0". the I call RegSaveKey, it does not get mad , just never writes the file... Here is my code:
One more little thing, it's the HKEY_LOCAL_MACHINE for the remote computer, it is a key call TAIS, under software.

frmMain:
Private Sub cmdSaveKey()

    Const sFile = "c:\test.reg"
        'SaveKey "SOFTWARE\TAIS", sFile, HKEY_LOCAL_MACHINE 'for local machine test

    SaveRemoteKey "SOFTWARE\TAIS", sFile, HKEY_LOCAL_MACHINE, "192.168.85.21"

End Sub

Module:
Option Explicit
Public Type LUID
   lowpart As Long
   highpart As Long
End Type
Public Type LUID_AND_ATTRIBUTES
   pLuid As LUID
   Attributes As Long
End Type
Public Type TOKEN_PRIVILEGES
   PrivilegeCount As Long
   Privileges As LUID_AND_ATTRIBUTES
End Type

Public Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type



Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_DYN_DATA = &H80000006
Public Const HKEY_CURRENT_CONFIG = &H80000005

Public Const TOKEN_QUERY As Long = &H8&
Public Const TOKEN_ADJUST_PRIVILEGES As Long = &H20&
Public Const SE_PRIVILEGE_ENABLED As Long = &H2
Public Const SE_RESTORE_NAME = "SeRestorePrivilege"
Public Const SE_BACKUP_NAME = "SeBackupPrivilege"
Public Const REG_FORCE_RESTORE As Long = 8&
Public Const READ_CONTROL = &H20000
Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const SPECIFIC_RIGHTS_ALL = &HFFFF
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_NOTIFY = &H10
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))

Public Const ACCESS_DENIED = 5
Public Const BAD_NETPATH = 53
Public Const BAD_PATHNAME = 161
Public Const BADDB = 1009
Public Const BADKEY = 1010
Public Const CALL_NOT_IMPLEMENTED = 120
Public Const CANTOPEN = 1011
Public Const CANTREAD = 1012
Public Const CANTWRITE = 1013
Public Const DLL_INIT_FAILED = 1114
Public Const FILE_NOT_FOUND = 2
Public Const INSUFFICIENT_BUFFER = 122
Public Const INVALID_HANDLE = 6
Public Const INVALID_PARAMETER = 87
Public Const KEY_DELETED = 1018
Public Const KEY_NOT_FOUND = FILE_NOT_FOUND
Public Const LOCK_FAILED = 167
Public Const MORE_DATA = 234
Public Const NO_MORE_ITEMS = 259
Public Const NOT_REGISTRY_FILE = 1017
Public Const REGISTRY_CORRUPT = 1015
Public Const REGISTRY_IO_FAILED = 1016
Public Const REGISTRY_RECOVERED = 1014
Public Const SUCCESS = 0
Public Const TRANSFER_TOO_LONG = 222

Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
    ByVal samDesired As Long, phkResult As Long) As Long
   
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
    (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
   
Public Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" _
    (ByVal hKey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As Long
   
Public Declare Function AdjustTokenPrivileges Lib "advapi32.dll" _
    (ByVal TokenHandle As Long, ByVal DisableAllPriv As Long, _
    NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, _
    ReturnLength As Long) As Long
   
Public Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As Any, ByVal lpName As String, lpLuid As LUID) As Long

Public Declare Function OpenProcessToken Lib "advapi32.dll" _
(ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long

Public Declare Function GetCurrentProcess Lib "kernel32" () As Long

Public Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" _
(ByVal hKey As Long, ByVal lpFile As String, lpSecurityAttributes As Any) As Long

Public Declare Function RegConnectRegistry Lib "advapi32.dll" Alias "RegConnectRegistryA" _
(ByVal sRemoteServer As String, ByVal hKey As Long, phkResult As Long) As Long




Public Function EnablePrivilege(seName As String) As Boolean
    Dim p_lngRtn As Long
    Dim p_lngToken As Long
    Dim p_lngBufferLen As Long
    Dim p_typLUID As LUID
    Dim p_typTokenPriv As TOKEN_PRIVILEGES
    Dim p_typPrevTokenPriv As TOKEN_PRIVILEGES
    p_lngRtn = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, p_lngToken)
    If p_lngRtn = 0 Then
        Exit Function
    ElseIf Err.LastDllError <> 0 Then
        Exit Function
    End If
    p_lngRtn = LookupPrivilegeValue(0&, seName, p_typLUID)
    If p_lngRtn = 0 Then
        Exit Function
    End If
   
    p_typTokenPriv.PrivilegeCount = 1
    p_typTokenPriv.Privileges.Attributes = SE_PRIVILEGE_ENABLED
    p_typTokenPriv.Privileges.pLuid = p_typLUID
    EnablePrivilege = (AdjustTokenPrivileges(p_lngToken, False, p_typTokenPriv, Len(p_typPrevTokenPriv), p_typPrevTokenPriv, p_lngBufferLen) <> 0)
End Function
Public Function RestoreKey(ByVal sKeyName As String, ByVal sFileName As String, lPredefinedKey As Long) As Boolean
    If EnablePrivilege(SE_RESTORE_NAME) = False Then Exit Function
    Dim hKey As Long, lRetVal As Long
    Call RegOpenKeyEx(lPredefinedKey, sKeyName, 0&, KEY_ALL_ACCESS, hKey)
    Call RegRestoreKey(hKey, sFileName, REG_FORCE_RESTORE)
    RegCloseKey hKey
End Function
Public Function SaveKey(ByVal sKeyName As String, ByVal sFileName As String, lPredefinedKey As Long) As Boolean
    If EnablePrivilege(SE_BACKUP_NAME) = False Then Exit Function
    Dim hKey As Long, lRetVal As Long
    Call RegOpenKeyEx(lPredefinedKey, sKeyName, 0&, KEY_ALL_ACCESS, hKey)
    '
    If Dir(sFileName) <> "" Then Kill sFileName
    Call RegSaveKey(hKey, sFileName, ByVal 0&)
    RegCloseKey hKey
   
End Function

Public Function SaveRemoteKey(ByVal sKeyName As String, ByVal sFileName As String, lPredefinedKey As Long, ByVal sRemoteServer)
    Dim hRemoteKey, lReturnCode, lhRemoteRegistry, res As Long
    If EnablePrivilege(SE_BACKUP_NAME) = False Then Exit Function
   
    lReturnCode = RegConnectRegistry(sRemoteServer, lPredefinedKey, lhRemoteRegistry)
    res = RegOpenKeyEx(lhRemoteRegistry, sKeyName, 0, KEY_ALL_ACCESS, hRemoteKey)
    If res <> SUCCESS Then GoTo Errore
    If Dir(sFileName) <> "" Then Kill sFileName
    Call RegSaveKey(hRemoteKey, sFileName, ByVal 0&)
    RegCloseKey hKey
    frmMain.lblStatus.Caption = "Registry Saved"
    Exit Function
   
Errore:
    SaveRemoteKey = ""
    RegCloseKey hKey
    frmMain.lblStatus.Caption = "Error in saving Registry " & res
End Function


 
0
Comment
Question by:matrixnetworks
  • 2
4 Comments
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 14001008
Do you have the right privilages to write to the registry?
Pherhaps remote registry needs to be enabled in order for it to work.

You should try to connect to the remote computer using the option in regedit see if it works that way if it doesn't work it might not be the code as the issue.  If it works then we can assume it might be the code.

Just a suggestion
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 14001020
I know when I try connecting to a network computer registry that I can connect but am denied access to the HKEY_LOCAL_MACHINE folder. So I assume you must enable remote registry before you can do this. somewere in windows you can allow remote registry access and turn it on.
0
 

Author Comment

by:matrixnetworks
ID: 14001197
No I don't think that is the issus, I can connect via regedt32, just fine. Also in my SaveRemoteKey function my lReturnCode and the res returns zero, which means SUCCESS.  
0
 
LVL 2

Accepted Solution

by:
MarkSteward earned 2000 total points
ID: 14004045
The file will be saved on the *remote* computer, not on your local one.  Make sure you have permissions.  If you want it saved on the local one, use \\[LocalComputerName]\c$\test.hiv.  The same applies to RegRestoreKey.

If it's still not there, please change "call" for RegSaveKey to "res =", followed by:

  If res <> SUCCESS Then GoTo Errore

so it's the same as the call to RegOpenKeyEx, and change:

  frmMain.lblStatus.Caption = "Error in saving Registry " & res

to:

  frmMain.lblStatus.Caption = "Error in saving Registry " & res & " (" & GetLastError() & ")"

and add:

  Public Declare Function GetLastError Lib "kernel32.dll" () As Long

in your declarations, and let us know what happens?

Cheers,

 Mark
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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.
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 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…
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…
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 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