Results 1 to 4 of 4

Thread: How to

  1. #1
    Guest

    Question

    Dear Friends
    i wish to put my program name and directory path into the registry and later on retreive it back from the registry? how can i do this

    thanks to all

    Merry X'mas and Happy New Year for all

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    You don't need to save the path & title, it's always accessable via the
    Code:
    App.Path 'path to exe
    App.Title 'name of exe
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    Hyperactive Member
    Join Date
    Nov 2000
    Location
    Mexico City
    Posts
    306
    If you really want to do that, check getsetting and savesetting.
    If things were easy, users might be programmers.

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Thumbs up

    Hey! Outruner2000, here is the sample code to put the App.Path and App.Title in to the registry path:

    \\HKEY_CURRENT_USER\Software\Outruner2000\Path
    and
    \\HKEY_CURRENT_USER\Software\Outruner2000\Title

    Code:
    'Put this code into a module file
    Option Explicit
    
    'WIN32API Costant
    Public Const REG_SZ = 1                         ' Unicode nul terminated string
    Public Const REG_BINARY = 3                     ' Free form binary
    Public Const REG_DWORD = 4                      ' 32-bit number
    
    Public Const HKEY_CLASSES_ROOT = &H80000000
    Public Const HKEY_CURRENT_CONFIG = &H80000005
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_DYN_DATA = &H80000006
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const HKEY_PERFORMANCE_DATA = &H80000004
    Public Const HKEY_USERS = &H80000003
    
    Public Const ERROR_SUCCESS = 0&
    
    'WIN32 API declaration
    Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
    
    Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    
    Public 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         ' Note that if you declare the lpData parameter as String, you must pass it By Value.
    Public 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         ' Note that if you declare the lpData parameter as String, you must pass it By Value.
    
    Public hCurKey As Long
    
    Public Function SET_STRING_VALUE(ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpString As String, ByVal dwType As Long, ByVal lpData As String) As Long
    On Error GoTo ErrHandle
        'Data validation
        If hkey = 0 Or lpSubKey = "" Or lpString = "" Then GoTo ErrHandle
        
        'Open the given hkey + subkey
        If RegOpenKey(hkey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
            'Create the given string + value
            If RegSetValueEx(hCurKey, lpString, 0, dwType, ByVal lpData, Len(lpData)) = ERROR_SUCCESS Then
                SET_STRING_VALUE = 1
            Else
                SET_STRING_VALUE = 0
            End If
        Else
            'Create the given hkey + subkey
            If RegCreateKey(hkey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
                'Create the given string + value
                If RegSetValueEx(hCurKey, lpString, 0, dwType, ByVal lpData, Len(lpData)) = ERROR_SUCCESS Then
                    SET_STRING_VALUE = 1
                Else
                    SET_STRING_VALUE = 0
                End If
            Else
                SET_STRING_VALUE = 0
            End If
        End If
        RegCloseKey hCurKey
        
    Exit Function
    ErrHandle:
        SET_STRING_VALUE = 0
    End Function
    
    Public Function GET_STRING_VALUE(ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpString As String, ByVal dwType As Long) As Variant
    On Error GoTo ErrHandle
        'Data validation
        If hkey = 0 Or lpSubKey = "" Or lpString = "" Then GoTo ErrHandle
        
        'Open the given hkey + subkey
        If RegOpenKey(hkey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
            'Retrieve the given string value
            Select Case dwType
            Case REG_SZ
                Dim strBuff As String
                strBuff = String(255, Chr(0))
                
                If RegQueryValueEx(hCurKey, lpString, 0, dwType, ByVal strBuff, Len(strBuff)) = ERROR_SUCCESS Then
                    GET_STRING_VALUE = Left(strBuff, InStr(1, strBuff, Chr(0), vbTextCompare))
                Else
                    GET_STRING_VALUE = ""
                End If
            Case REG_BINARY
                Dim strData As Integer
                
                If RegQueryValueEx(hCurKey, lpString, 0, dwType, strData, Len(strData)) = ERROR_SUCCESS Then
                    GET_STRING_VALUE = strData
                Else
                    GET_STRING_VALUE = ""
                End If
            Case REG_DWORD
                Dim strDWORD As String
                
                If RegQueryValueEx(hCurKey, lpString, 0, dwType, ByVal strDWORD, Len(strDWORD)) = ERROR_SUCCESS Then
                    GET_STRING_VALUE = Left(strDWORD, InStr(1, strDWORD, Chr(0), vbTextCompare))
                Else
                    GET_STRING_VALUE = ""
                End If
            End Select
        Else
            GET_STRING_VALUE = ""
        End If
        RegCloseKey hCurKey
        
        Exit Function
    ErrHandle:
        GET_STRING_VALUE = ""
    End Function
    'Here show you how to call this 2 function
    Code:
    Private Sub Command1_Click()
        SET_STRING_VALUE HKEY_CURRENT_USER, "Software\Outruner2000", "Path", REG_SZ, ByVal CStr(App.Path)
        SET_STRING_VALUE HKEY_CURRENT_USER, "Software\Outruner2000", "Title", REG_SZ, ByVal CStr(App.Title)
    End Sub
    
    Private Sub Command2_Click()
        MsgBox GET_STRING_VALUE(HKEY_CURRENT_USER, "Software\Outruner2000", "Path", REG_SZ)
        MsgBox GET_STRING_VALUE(HKEY_CURRENT_USER, "Software\Outruner2000", "Title", REG_SZ)
    End Sub
    regards,
    Chris

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