Results 1 to 5 of 5

Thread: Add String Value and Key to HKEY_LOCAL_MACHINE

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    Question

    Please Help! I need to add a Key and value to HKEY_LOCAL_MACHINE Software\Microsoft\windows\run. So that when the system boots up the program will load. I got a great module here (thanks guys) but it adds the vakue in Binary which will not work for thsi purpose. The system will not resolve the binary to a string value. What I need it to be is a string value not a binary value
    HKEY_LOCAL_MACHINE Software\Microsoft\windows\run\
    Key(EzTelnet) String ("C:\Program Files\EzTelnet\EzTelnet.exe")

    Help!!!!!
    TMacPherson
    MIS Systems Engineer
    [email protected]


  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    In a Module:
    Code:
    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
    Example:
    Code:
    SaveRegSetting HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", "EzTelnet", "C:\Program Files\EzTelnet\EzTelnet.exe"

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463
    I get and error Sub or Function not Defined for the Sub: RegCreateKey gets highlighted
    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
    TMacPherson
    MIS Systems Engineer
    [email protected]


  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Did you copy the Registry API functions into the Module too?
    It works fine here.

  5. #5
    Member
    Join Date
    Jul 2000
    Location
    Belgium/Kortrijk
    Posts
    34

    Wink

    Hello,

    try this

    put Call before Savesetting string and put the rest between brackets

    Ctrack ....
    Vb-Addicted
    Diederik Van Durme
    18/m/Belgium:Kortrijk

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