CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Registry writing

    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.

    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Registry writing

    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
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured