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
RegSetValueEx writes a value to a registry key. If the value does not already exist, it will be created. The value can be of any of the registry data types.
If an error occured, the function returns a non-zero error code. If successful, the function returns 0.
When writing a string or a single numeric value, the lpData parameter must be prefixed by the ByVal keyword. Any other values (such as byte arrays) do not need the ByVal keyword.
Const HKEY_CLASSES_ROOT = &H80000000 Const HKEY_CURRENT_CONFIG = &H80000005 Const HKEY_CURRENT_USER = &H80000001 Const HKEY_DYN_DATA = &H80000006 Const HKEY_LOCAL_MACHINE = &H80000002 Const HKEY_PERFORMANCE_DATA = &H80000004 Const HKEY_USERS = &H80000003 Const REG_BINARY = 3 Const REG_DWORD = 4 Const REG_DWORD_BIG_ENDIAN = 5 Const REG_DWORD_LITTLE_ENDIAN = 4 Const REG_EXPAND_SZ = 2 Const REG_LINK = 6 Const REG_MULTI_SZ = 7 Const REG_NONE = 0 Const REG_RESOURCE_LIST = 8 Const REG_SZ = 1
' This code is licensed according to the terms and conditions listed here. ' Create a key called HKEY_CURRENT_USER\Software\MyCorp\MyProgram\Config. ' Then create a "username" value under that key and set its value to "Rimmer". Dim hregkey As Long ' receives handle to the newly created or opened registry key Dim secattr As SECURITY_ATTRIBUTES ' security settings of the key Dim subkey As String ' name of the subkey to create Dim neworused As Long ' receives 1 if new key was created or 2 if an existing key was opened Dim stringbuffer As String ' the string to put into the registry Dim retval As Long ' return value ' Set the name of the new key and the default security settings subkey = "Software\MyCorp\MyProgram\Config" secattr.nLength = Len(secattr) ' size of the structure secattr.lpSecurityDescriptor = 0 ' default security level secattr.bInheritHandle = True ' the default value for this setting ' Create or open the registry key retval = RegCreateKeyEx(HKEY_CURRENT_USER, subkey, 0, "", 0, KEY_WRITE, secattr, hregkey, neworused) If retval <> 0 Then ' error during open Debug.Print "Error opening or creating registry key -- aborting." End ' terminate the program End If ' Write the string to the registry. Note that because Visual Basic is being used, the string ' passed to the function must explicitly be passed ByVal. stringbuffer = "Rimmer" & vbNullChar ' note how a null character must be appended to the string retval = RegSetValueEx(hregkey, "username", 0, REG_SZ, ByVal stringbuffer, Len(stringbuffer)) ' write the string ' Close the registry key retval = RegCloseKey(hregkey)
RegDeleteValue, RegQueryValueEx
Registry
Go back to the alphabetical Function listing.
Go back to the Reference section index.
Last Modified: September 11, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/r/regsetvalueex.html