Private Sub Command1_Click()
Dim hKey As Long ' receives handle to the registry key
Dim secattr As SECURITY_ATTRIBUTES ' security settings for the key
Dim subkey As String ' name of the subkey to create or open
Dim neworused As Long ' receives flag for if the key was created or 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)
secattr.lpSecurityDescriptor = 0
secattr.bInheritHandle = 1
' Create (or open) the registry key.
retval = RegCreateKeyEx(HKEY_CURRENT_USER, subkey, 0, "", 0, KEY_WRITE, _
secattr, hKey, neworused)
If retval <> 0 Then
Debug.Print "Error opening or creating registry key -- aborting."
Exit Sub
End If
' Write the string to the registry. Note the use of ByVal in the second-to-last
' parameter because we are passing a string.
stringbuffer = "Rimmer" & vbNullChar ' the terminating null is necessary
retval = RegSetValueEx(hKey, "username", 0, REG_SZ, ByVal stringbuffer, _
Len(stringbuffer))
' Close the registry key.
retval = RegCloseKey(hKey)
End Sub