Advertisement

05.13.2005 at 02:18PM PDT, ID: 21423899
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.6

Need help with RegSavekey and RegRestoreKey

Asked by matrixnetworks in Visual Basic Programming

Tags: ,

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


 Start Free Trial
[+][-]05.13.2005 at 07:47PM PDT, ID: 14001008

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.13.2005 at 07:52PM PDT, ID: 14001020

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.13.2005 at 09:16PM PDT, ID: 14001197

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.14.2005 at 05:34PM PDT, ID: 14004045

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Visual Basic Programming
Tags: regsavekey, regrestorekey
Sign Up Now!
Solution Provided By: MarkSteward
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20081112-EE-VQP-43