Adding registry values 
Author Message
 Adding registry values

I am using VB 6 to delete a registry key and I need to then add an updated
key with several subkeys and DWORD and String values.  I have successfully
deleted the keys and subkeys, but I am have a very difficult time adding the
new stuff in.  I have the .reg file that I want to add in, so I have all of
the necessary values.  How do I add these values into the registry?  I am at
an impass....

TIA,

Rob Kime



Mon, 19 Apr 2004 11:24:06 GMT  
 Adding registry values
This can help you:

Public Enum RegistryKeyEnum
   HKEY_CLASSES_ROOT = &H80000000
   HKEY_CURRENT_USER = &H80000001
   HKEY_LOCAL_MACHINE = &H80000002
   HKEY_USERS = &H80000003
End Enum

Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias
"RegCreateKeyExA" (ByVal lKey As Long, ByVal sSubKey As String, ByVal
lReserved As Long, ByVal sClass As String, ByVal lOptions As Long, ByVal
lSamDesired As Long, ByVal lSecurityAttributes As Long, ByRef r_lResult As
Long, ByRef r_lDisposition As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lKey As Long)
As Long

Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias
"RegSetValueExA" (ByVal lKey As Long, ByVal sValueName As String, ByVal
lReserved As Long, ByVal lType As Long, ByVal sValue As String, ByVal
lCbData As Long) As Long

Private Declare Function RegSetValueExDWord Lib "advapi32.dll" Alias
"RegSetValueExA" (ByVal lKey As Long, ByVal sValueName As String, ByVal
lReserved As Long, ByVal lType As Long, lValue As Long, ByVal lCbData As
Long) As Long

Public Function bSetRegistryValue(ByVal ePredefinedKey As RegistryKeyEnum, _
   ByVal sKey As String, ByVal sValueName As String, ByVal
eRegistryValueType As RegistryValueTypeEnum, _
   ByVal vValue As Variant) As Boolean

   Const NULL_VALUE As Long = 0

   Const REG_SZ = 1 ' Unicode nul terminated string
   Const REG_DWORD = 4 ' 32-bit number

   Const REG_OPTION_NON_VOLATILE = 0

   Const SYNCHRONIZE = &H100000
   Const READ_CONTROL = &H20000
   Const STANDARD_RIGHTS_WRITE = READ_CONTROL
   Const KEY_CREATE_SUB_KEY = &H4
   Const KEY_SET_VALUE = &H2
   Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or
KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))

   Const ERROR_SUCCESS = 0

   Dim lKeyHandle As Long
   Dim lDisposition As Long
   Dim bResult As Boolean
   Dim sValue As String
   Dim lValue As Long

   bResult = False

   lKeyHandle = 0

   ' Es mejor utilizar RegCreateKeyEx que RegOpenkeyEx porque si no existe
la clave o el nombre del valor los crea. Si ya
   ' existen, abre la clave.

   If RegCreateKeyEx(ePredefinedKey, sKey, NULL_VALUE, vbNullString, _
      REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL_VALUE, lKeyHandle,
lDisposition) = ERROR_SUCCESS Then

      Select Case eRegistryValueType

         Case RegistryValueTypeString

            On Error Resume Next
            sValue = CStr(vValue)
            On Error GoTo 0

            sValue = sValue & vbNullChar
            If RegSetValueExString(lKeyHandle, sValueName, NULL_VALUE,
REG_SZ, sValue, Len(sValue)) = ERROR_SUCCESS Then
               bResult = True
            End If

         Case RegistryValueTypeDWord
            On Error Resume Next
            lValue = CLng(vValue)
            On Error GoTo 0

            If RegSetValueExDWord(lKeyHandle, sValueName, NULL_VALUE,
REG_DWORD, lValue, 4) = ERROR_SUCCESS Then
               bResult = True
            End If

      End Select

      Call RegCloseKey(lKeyHandle)

   End If

   bSetRegistryValue = bResult

End Function

--

Best regards,

Carlos J. Quintero

MZ-Tools 3.0 (New version) freeware 'all-in-one' Add-In for VB6 and VBA:
Code Templates, Detect dead code, Favorite Procedures, Favorite Projects,
External Utilities, XML Documentation,  Private Clipboards, Select Case
Assistant, Sort Procedures, Collapse Projects, Split/Combine Lines, Open
Folder of File, Edit File As Text, MsgBox Assistant, Connect String
Assistant and many more features:
www.mztools.com



Quote:
> I am using VB 6 to delete a registry key and I need to then add an updated
> key with several subkeys and DWORD and String values.  I have successfully
> deleted the keys and subkeys, but I am have a very difficult time adding
the
> new stuff in.  I have the .reg file that I want to add in, so I have all
of
> the necessary values.  How do I add these values into the registry?  I am
at
> an impass....

> TIA,

> Rob Kime



Mon, 19 Apr 2004 16:31:11 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Adding registry values

2. How to add registry values

3. Adding values to registry while installing

4. Adding a value to the registry of a remote PC using REGOBJ.DLL

5. Adding a value to the registry with API

6. ADDING MULTIPLE VALUES TO A REGISTRY SUBKEY

7. How can add and delete registry keys and values remotelly? How can I manipulate REG_EXPAND_SZ values?

8. Calling Registry.LocalMachine, Registry.CurrentUser by Registry.regHive?

9. VB Code to Set Registry Values

10. Amending Registry Values in NT using VBA

11. Newbie get registered user and company name values from registry

12. reading registry key values

 

 
Powered by phpBB® Forum Software