PDA

Click to See Complete Forum and Search --> : Exporting Registry key


Serge
Nov 4th, 1999, 12:31 AM
Sure thing:


Option Explicit

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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_CURRENT_CONFIG = &H80000005
Private Const ERROR_SUCCESS = 0&
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_NOTIFY = &H10
Private Const READ_CONTROL = &H20000
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const SYNCHRONIZE = &H100000
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Private Const ERROR_SUCCESS = 0
Private Const REG_SZ = 1

Public Function GetRegistryValueData(pRoot As Long, pKey As String, pValue As String) As String
Dim lKeyHandle As Long
Dim lRet As Long
Dim strBuffer As String

lRet = RegOpenKeyEx(pRoot, pKey, 0, KEY_READ, lKeyHandle)
If lRet = ERROR_SUCCESS Then
strBuffer = Space(255)
lRet = RegQueryValueEx(lKeyHandle, pValue, 0, REG_SZ, strBuffer, Len(strBuffer))
If lRet = ERROR_SUCCESS Then
GetRegistryValueData = Left(strBuffer, InStr(strBuffer, vbNullChar) - 1)
End If
RegCloseKey lKeyHandle
End If
End Function




Usage: GetRegistryValueData RootKey, KeyName, ValueName

Example:strText = GetRegistryValueData(HKEY_LOCAL_MACHINE, "Software\MyProgram", "MyProgram.exe")

strText now has the value.


Regards,

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com

Serge
Nov 4th, 1999, 01:04 AM
Oops, I think I missunderstood your question.
So here's the routine to load the Key from a file:


Option Explicit
Private Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hKey As Long, ByVal lpFile As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Private Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_CURRENT_CONFIG = &H80000005
Private Const ERROR_SUCCESS = 0&
Private Const KEY_QUERY_VALUE = &H1
Private Const KEY_NOTIFY = &H10
Private Const READ_CONTROL = &H20000
Private Const KEY_ENUMERATE_SUB_KEYS = &H8
Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Private Const SYNCHRONIZE = &H100000
Private Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Private Const REG_SZ = 1

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

Public Function GetRegistryKeyFromFile(pRoot As Long, pSubKey As String, pFileName As String) As Boolean
Dim lKeyHandle As Long
Dim lRet As Long
Dim sec As SECURITY_ATTRIBUTES


lRet = RegOpenKeyEx(pRoot, pSubKey, 0, KEY_READ, lKeyHandle)
sec.nLength = Len(sec)
sec.bInheritHandle = True
If lRet = ERROR_SUCCESS Then
lRet = RegLoadKey(lKeyHandle, pSubKey, pFileName)
If lRet = ERROR_SUCCESS Then
GetRegistryKeyFromFile = True
End If
RegCloseKey lKeyHandle
End If
End Function




Usage: GetRegistryKeyFromFile RootKey, SubKey, FileName

Example: GetRegistryKeyFromFile HKEY_LOCAL_MACHINE, "Software", "f:\SergeReg.reg"

Note: If you're running WindowsNT, make sure you have rights to import to the registry.


Regards,

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com

TonyFernandez
Nov 4th, 1999, 11:22 AM
I have used the RegSaveKey to export a registry key, this works fine, but when I try to reimport it I get an error saying that the file is not in the correct format.

Does anyone have a sample to do this

Thanks

Tony

TonyFernandez
Nov 7th, 1999, 10:23 PM
Thanks