Dear Reader
I am using the API function RegSetValueEx to update some values in the windows registry. But each time I update the string value it writes is garbage into that particular field in the registry. I opened the Registry using RegOpenEx.
All I want to write is the value '0' to the string value. I have heard that the registry's values are formatted to UNICODE format when writting to the registry. Is there some thing to do with the format while writting. The dwtype = 1 or REG_SZ. Please help me out with this. I know that there are many OCX'S for registry editing but I want to do this with an API so that I don't have to create a distribution kit.
Amendra
Arjun M
September 23rd, 1999, 02:26 AM
Hi,
Try the code yaar. U need to create a button Button1. I think this will help you.
Bye
[/vbcode]
'**************PASTE THIS CODE IN THE MODULE
Option Explicit
Public Const HKEY_CURRENT_USER = &H80000001 ' KEY
Public Const REG_SZ = 1 ' Unicode nul terminated string
Public Const READ_CONTROL = &H20000
Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Public Const KEY_SET_VALUE = &H2
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
(ByVal hKey As Long, _
ByVal lpSubKey As String, _
ByVal Reserved As Long, _
ByVal lpClass As String, _
ByVal dwOptions As Long, _
ByVal samDesired As Long, _
lpSecurityAttributes As SECURITY_ATTRIBUTES, _
phkResult As Long, _
lpdwDisposition As Long) As Long
Declare Function RegSetValueEx 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 ' Note that if you declare the lpData parameter as String, you must pass it By Value.
Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
'**************PASTE THIS CODE IN THE FORM*******
'**************CREATE A COMMAND BUTTON***********
Option Explicit
Private Sub Command1_Click()
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
Dim strMsg As String
' Set the name of the new key and the default security settings
subkey = "Software\ITS\Arjun\Test"
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
MsgBox "Error opening or creating registry key -- aborting."
End
End If
' Write the string to the registry.
stringbuffer = "This is simply great" & vbNullChar
' Close the registry key
retval = RegCloseKey(hregkey)
strMsg = IIf(hregkey = 1, " New Key was created", " Existing key was opened")
strMsg = strMsg & " and the write " & IIf(retval = 0, "Operation Succeeded.", "Operation Failed.")
MsgBox strMsg
End Sub
Private Sub Form_Activate()
MsgBox "This program creates a key called HKEY_CURRENT_USER\Software\ITS\Arjun\Test." & Chr(13) _
& "Then create a username 'Arjun' value under that key and set its value to 'This is simply great'"
Private Const REG_SZ = 1 ' Unicode nul terminated string
'
' Private member variables
'
Private m_Submain As String
Private m_AppName As String
Private m_Main_key As String
'
' Private class constants
'
Private Const defsubmain As String = "OfficeBlox"
Private Const defMain_key As String = "software"
' ********************************************
' Initialize and Terminate
' ********************************************
Private Sub Class_Initialize()
m_Submain = defsubmain
m_Main_key = defMain_key
m_AppName = App.ProductName
End Sub
' ********************************************
' Public Properties
' ********************************************
Public Property Let Submain(ByVal NewVal As String)
If Len(NewVal) Then
m_Submain = Trim(NewVal)
Else
m_Submain = defsubmain
End If
End Property
Public Property Let Main_key(ByVal NewVal As String)
If Len(NewVal) Then
m_Main_key = Trim(NewVal)
Else
m_Main_key = defMain_key
End If
End Property
Public Property Get Submain() As String
Submain = m_Submain
End Property
Public Property Get Main_key() As String
Main_key = m_Main_key
End Property
Public Property Let AppName(ByVal NewVal As String)
If Len(NewVal) Then
m_AppName = Trim(NewVal)
Else
m_AppName = App.ProductName
End If
End Property
Public Property Get AppName() As String
AppName = m_AppName
End Property
' ********************************************
' Public Methods
' ********************************************
Public Function DeleteSetting(ByVal Section As String, Optional ByVal Key As String = "") As Boolean
' Section Required. String expression containing the name of the section where the key setting
' is being deleted. If only section is provided, the specified section is deleted along
' with all related key settings.
' Key Optional. String expression containing the name of the key setting being deleted.
Dim nRet As Long
Dim hKey As Long
If Len(Key) Then
' Open key
nRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SubKey(Section), 0&, KEY_ALL_ACCESS, hKey)
If nRet = ERROR_SUCCESS Then
' Set appropriate value for default query
If Key = "*" Then Key = vbNullString
' Delete the requested value
nRet = RegDeleteValue(hKey, Key)
End If
Else
' Open parent key
nRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SubKey(), 0&, KEY_ALL_ACCESS, hKey)
If nRet = ERROR_SUCCESS Then
' Attempt to delete whole section
nRet = RegDeleteKey(hKey, Section)
End If
End If
DeleteSetting = (nRet = ERROR_SUCCESS)
End Function
Public Function GetSetting(ByVal Section As String, ByVal Key As String, Optional ByVal Default As String = "") As String
' Section Required. String expression containing the name of the section where the key setting is found.
' If omitted, key setting is assumed to be in default subkey.
' Key Required. String expression containing the name of the key setting to return.
' Default Optional. Expression containing the value to return if no value is set in the key setting.
' If omitted, default is assumed to be a zero-length string ("").
Dim nRet As Long
Dim hKey As Long
Dim nType As Long
Dim nBytes As Long
Dim Buffer As String
' Assume failure and set return to Default
GetSetting = Default
' Open key
nRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SubKey(Section), 0&, KEY_ALL_ACCESS, hKey)
If nRet = ERROR_SUCCESS Then
' Set appropriate value for default query
If Key = "*" Then Key = vbNullString
' Determine how large the buffer needs to be
nRet = RegQueryValueEx(hKey, Key, 0&, nType, ByVal Buffer, nBytes)
If nRet = ERROR_SUCCESS Then
' Build buffer and get data
If nBytes > 0 Then
Buffer = Space(nBytes)
nRet = RegQueryValueEx(hKey, Key, 0&, nType, ByVal Buffer, Len(Buffer))
If nRet = ERROR_SUCCESS Then
' Trim NULL and return successful query!
GetSetting = Left(Buffer, nBytes - 1)
End If
End If
End If
End If
End Function
Public Function SaveSetting(ByVal Section As String, ByVal Key As String, ByVal Setting As String) As Boolean
' Section Required. String expression containing the name of the section where the key setting is being saved.
' Key Required. String expression containing the name of the key setting being saved.
' Setting Required. Expression containing the value that key is being set to.
Dim nRet As Long
Dim hKey As Long
Dim nResult As Long
' Open (or create and open) key
nRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, SubKey(Section), 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, ByVal 0&, hKey, nResult)
If nRet = ERROR_SUCCESS Then
' Set appropriate value for default query
If Key = "*" Then Key = vbNullString
' Write new value to registry
nRet = RegSetValueEx(hKey, Key, 0&, REG_SZ, ByVal Setting, Len(Setting))
Call RegCloseKey(hKey)
End If
SaveSetting = (nRet = ERROR_SUCCESS)
End Function
' ********************************************
' Private Methods
' ********************************************
Private Function SubKey(Optional ByVal Section As String = "") As String
' Build SubKey from known values
SubKey = m_Main_key & "\" & m_Submain & "\" & m_AppName
If Len(Section) Then
SubKey = SubKey & "\" & Section
End If
End Function
[/vbcode
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.