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
Exactly one of the following flags identifying the data type of the data to write:
REG_BINARY = 3
A non-text sequence of bytes.
REG_DWORD = 4
Same as REG_DWORD_LITTLE_ENDIAN.
REG_DWORD_LITTLE_ENDIAN = 4
A 32-bit integer stored in little-endian format. This is the way Intel-based computers store numbers.
REG_DWORD_BIG_ENDIAN = 5
A 32-bit integer stored in big-endian format. This is the opposite of the way Intel-based computers normally store numbers -- the byte order is reversed.
REG_EXPAND_SZ = 2
A vbNullChar-terminated string which contains unexpanded environment variables.
REG_LINK = 6
A Unicode symbolic link.
REG_MULTI_SZ = 7
A series of strings, each separated by a vbNullChar and the entire set terminated by a double vbNullChar.
REG_NONE = 0
No data type.
REG_RESOURCE_LIST = 8
A list of resources in the resource map.
REG_SZ = 1
A string terminated by a vbNullChar.
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.


Back to Paul Kuliniewicz's Home Page
E-mail: Borg953@aol.com
This page is at http://members.aol.com/Borg953/api/functions/regsetvalueex.html