# RegSetValueEx Function

`Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long`
Alternate Declare for use with strings:
`Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long`

RegSetValueEx writes a value to a registry key. If the value does not already exist, it will be created. The value can be of a number of different data types. Notice that a variant of the declare must be used if a type of string is being set because of the way Visual Basic handles strings. The only difference is the method in which the string is passed to the API function. The function returns zero if successful, or a non-zero value error code if an error occured.

hKey
  A handle to the registry key to write the value under. This cannot be one of the predefined (HKEY_) keys.
lpValueName
  The name of the value to set.
Reserved
  Reserved. Set to 0.
dwType
  The registry data type of the piece of data to write.
lpData
  The number, string, or other data to write. NOTE: You must use the alternate declare for any type of string when using Visual Basic!
cbData
  The size in bytes of the data to set, including any `vbNullChars. For numbers, set the number of bytes of memory the number uses.`

Example:

    ' Create and set some data under the key:
    ' HKEY_CURRENT_USER\Software\Dummy\DummyApp\1.0\
    Dim secattr As SECURITY_ATTRIBUTES
    Dim dword As Long, vbstring As String
    ' The next three lines give default values for secattr
    secattr.lpSecurityDescriptor = 0  ' default security level
    secattr.bInheritHandle = True  ' might as well allow it
    secattr.nLength = Len(secattr)  ' store size of variable
    subkey = "Software\Dummy\DummyApp\1.0"  ' key name
    x = RegCreateKeyEx(HKEY_CURRENT_USER, subkey, 0, "", 0, KEY_READ, secattr, hregkey, newopen)
    ' if created, newopen = 1; if not, newopen = 2
    If x <> 0 Then  ' error creating key -- abort!
      Debug.Print "Could not open registry key."
      Exit Sub
    End If
    ' We want to write these two variables to registry key hregkey
    dword = 123456789  ' a DWORD is a 32-bit (4 byte) number
    vbstring = "This is a line of text." & vbNullChar  ' vbNullChar at the end is required
    ' --Write the data to the registry--
    ' Set "number" to the contents of dword
    x = RegSetValueEx(hregkey, "number", 0, REG_DWORD, dword, 4)
    If x <> 0 Then Goto WriteError  ' abort to error handler if an error occured
    ' Set "string" to the contents of vbstring, using the mandatory alternate declare
    x = RegSetValueExString(hregkey, "string", 0, REG_SZ, vbstring, Len(vbstring))
    If x <> 0 Then Goto WriteError  ' again, abort if an error occured
    x = RegCloseKey(hregkey)
    Exit Sub  ' quit normal execution
    WriteError:
    Debug.Print "Error writing registry data."
    x = CloseKey(hregkey)  ' closing key frees up resources
    Exit Sub  ' abort

Related Call: RegQueryValueEx
Category: Registry
Back to the index.

ML>