Click to See Complete Forum and Search --> : Registry writing


Cakkie
February 2nd, 2000, 12:35 PM
Is there a way to write/read to/from the registry NOT using the standard registry commands supplied in vb

I want to write to the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run to make this program run whenever windows starts.

Aaron Young
February 2nd, 2000, 01:52 PM
Here are a couple of Sub/Functions I wrote to store Strings/Longs anywhere in the Registry:
'In a Module..
private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (byval hKey as Long, byval lpSubKey as string, phkResult as Long) as Long
private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (byval hKey as Long, byval lpSubKey as string, phkResult as Long) as Long
private 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
private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (byval hKey as Long, byval lpValueName as string, byval lpReserved as Long, lpType as Long, lpData as Any, lpcbData as Long) as Long
private Declare Function RegCloseKey Lib "advapi32.dll" (byval hKey as Long) as Long

public Const HKEY_LOCAL_MACHINE = &H80000002
public Const HKEY_CURRENT_USER = &H80000001
public Const HKEY_CURRENT_CONFIG = &H80000005
public Const HKEY_CLASSES_ROOT = &H80000000
public Const HKEY_DYN_DATA = &H80000006
public Const HKEY_PERFORMANCE_DATA = &H80000004
public Const HKEY_USERS = &H80000003

public Sub SaveRegSetting(byval lKey as Long, byval sSubKey as string, byval sValue as string, byval vData as Variant)
'Save a string/Long Value to the Registry
Dim lRegKey as Long
Dim lType as Long
Dim lData as Long
Dim sData as string

'Determine the type of Data being Saved
lType = IIf(VarType(vData) = vbString, 1, 4)
If lType = 4 then
'Long Data Type
lData = vData
else
'string Data Type
sData = vData
End If
If RegCreateKey(lKey, sSubKey, lRegKey) = 0 then
'Open/Create the Registry Key
If lType = 1 then
'Save the string Value to the Registry
Call RegSetValueEx(lRegKey, sValue, 0&, lType, byval sData, len(sData))
else
'Save the Long Value to the Registry
Call RegSetValueEx(lRegKey, sValue, 0&, lType, lData, 4)
End If
'Close the Registry Key
Call RegCloseKey(lRegKey)
End If
End Sub

public Function GetRegSetting(byval lKey as Long, byval sSubKey as string, byval sValue as string) as Variant
'Retrieve a string/Long Value from the Registry
Dim lRegKey as Long
Dim lData as Long
Dim sData as string
Dim lType as Long

'Open the Registry Key
If RegOpenKey(lKey, sSubKey, lRegKey) = 0 then
'Find out the Data Type of the Value to Retrieve
Call RegQueryValueEx(lRegKey, sValue, 0&, lType, byval 0&, 0&)
sData = Space(255)
If lType = 1 then
'Return a string Value
Call RegQueryValueEx(lRegKey, sValue, 0&, lType, byval sData, len(sData))
If InStr(sData, Chr(0)) then
sData = Left$(sData, InStr(sData, Chr(0)) - 1)
End If
else
'Return a Long Value
Call RegQueryValueEx(lRegKey, sValue, 0&, lType, lData, 4)
End If
GetRegSetting = IIf(lType = 1, sData, lData)
'Close the Registry Key
Call RegCloseKey(lRegKey)
End If
End Function


Usage: Call SaveRegSetting(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "My Value", "SomeApp.exe")

sMyAppName = GetRegSetting(HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "My Value")

Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com