用API的RegSaveKey导出注册表的 Reg 文件用RegRestoreKey怎么导回注册表?

xiaoxinghappy 2002-09-26 06:39:49
用RegSaveKey 导出注册表 Reg文件用 RegRestoreKey 可以导加注册表吗?
我用下面的代码保存和和恢复注册表,可是在恢复的时候会出错,提示只能在Win32模式下使用?
怎么办,还有其它办法导回用RegSaveKey导出的注册表文件吗?

Private Sub Command1_Click()
Dim hKey As Long
Dim ret As Long
Dim Buffer As String
ret = RegOpenKey(HKEY_CURRENT_USER, "Software\VB and VBA Program Settings\MySoft", hKey)
If ret <> 0 Then GoTo PROC_EXIT
ret = RegSaveKey(hKey, "C:\abc.reg", ByVal 0&)
If ret <> 0 Then GoTo PROC_EXIT
PROC_EXIT:
If ret <> 0 Then
Buffer = Space$(200)
Call FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, ret, LANG_NEUTRAL, Buffer, 200, ByVal 0&)
Call MsgBox(Buffer, vbCritical, "ERROR")
End If
If hKey <> 0 Then Call RegCloseKey(hKey)
End Sub

Private Sub Command2_Click()
Dim hKey As Long
Dim ret As Long
Dim Buffer As String
ret = RegOpenKey(HKEY_CURRENT_USER, "Software\VB and VBA Program Settings\", hkey)
If ret <> 0 Then GoTo PROC_EXIT
ret = RegRestoreKey(hkey, "C:\abc.reg", ByVal 0&)
'ret = RegRestoreKey(HKEY_CURRENT_USER, "D:\abc.reg", 0&)
If ret <> 0 Then GoTo PROC_EXIT
PROC_EXIT:
If ret <> 0 Then
Buffer = Space$(200)
Call FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, ret, LANG_NEUTRAL, Buffer, 200, ByVal 0&)
Call MsgBox(Buffer, vbCritical, "ERROR")
End If
If hKey <> 0 Then Call RegCloseKey(hKey)
End Sub
...全文
402 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Azi03 2002-10-21
  • 打赏
  • 举报
回复
呵呵,这个权限问题别说98,连95都有的。
xiaoxinghappy 2002-09-27
  • 打赏
  • 举报
回复
我是在Win98 下也有权限的问题吗?
Azi03 2002-09-27
  • 打赏
  • 举报
回复
实际上就是个权限的问题,向注册表导入注册表文件需要有SE_RESTORE_NAME权限,所以只需要加入zyl910(910:分儿,我来了!) 说的这个函数,在你导入之前调用一下就可以了。
Function EnablePrivilege(seName As String) As Boolean
zyl910 2002-09-27
  • 打赏
  • 举报
回复
'Save/Restore Key
'example by Scott Watters (scottw@racewaves.com)

' No rhyme or reason for making some private and some public. Use your own discretion...
Const HKEY_CURRENT_USER = &H80000001
Const TOKEN_QUERY As Long = &H8&
Const TOKEN_ADJUST_PRIVILEGES As Long = &H20&
Const SE_PRIVILEGE_ENABLED As Long = &H2
Const SE_RESTORE_NAME = "SeRestorePrivilege" 'Important for what we're trying to accomplish
Const SE_BACKUP_NAME = "SeBackupPrivilege"
Const REG_FORCE_RESTORE As Long = 8& ' Almost as import, will allow you to restore over a key while it's open!
Const READ_CONTROL = &H20000
Const SYNCHRONIZE = &H100000
Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Const STANDARD_RIGHTS_ALL = &H1F0000
Const SPECIFIC_RIGHTS_ALL = &HFFFF
Const KEY_QUERY_VALUE = &H1
Const KEY_SET_VALUE = &H2
Const KEY_CREATE_SUB_KEY = &H4
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_CREATE_LINK = &H20
Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
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))
Private Type LUID
lowpart As Long
highpart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges As LUID_AND_ATTRIBUTES
End Type
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long ' Always close your keys when you're done with them!
Private 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 ' Need to open the key to be able to restore to it.
Private Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hKey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As Long ' Main function
Private 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 'Used to adjust your program's security privileges, can't restore without it!
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As Any, ByVal lpName As String, lpLuid As LUID) As Long 'Returns a valid LUID which is important when making security changes in NT.
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, lpSecurityAttributes As Any) As Long
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 ' Failed
ElseIf Err.LastDllError <> 0 Then
Exit Function ' Failed
End If
p_lngRtn = LookupPrivilegeValue(0&, seName, p_typLUID) 'Used to look up privileges LUID.
If p_lngRtn = 0 Then
Exit Function ' Failed
End If
' Set it up to adjust the program's security privilege.
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) ' Must open key to restore it
'The file it's restoring from was created using the RegSaveKey function
Call RegRestoreKey(hKey, sFileName, REG_FORCE_RESTORE)
RegCloseKey hKey ' Don't want to keep the key ope. It causes problems.
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) ' Must open key to save it
'Don't forget to "KILL" any existing files before trying to save the registry key!
If Dir(sFileName) <> "" Then Kill sFileName
Call RegSaveKey(hKey, sFileName, ByVal 0&)
RegCloseKey hKey ' Don't want to keep the key ope. It causes problems.
End Function
Private Sub Form_Load()
Const sFile = "c:\test.reg"
SaveKey "SOFTWARE\KPD-Team\API-Guide", sFile, HKEY_CURRENT_USER
RestoreKey "SOFTWARE\KPD-Team\API-Guide", sFile, HKEY_CURRENT_USER
End Sub
dsclub 2002-09-27
  • 打赏
  • 举报
回复
RegReplaceKey
The RegReplaceKey function replaces the file backing a registry key and all its subkeys with another file, so that when the system is next started, the key and subkeys will have the values stored in the new file.

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧