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
Printable View
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
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
If you really want to do that, check getsetting and savesetting.
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
'Here show you how to call this 2 functionCode:'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
regards,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
Chris