Difference between revisions of "Windows Registry VB6"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
m
(Advanced Windows 32 Base API DLL (advapi32.dll))
Line 47: Line 47:
  
 
== Advanced Windows 32 Base API DLL (advapi32.dll) ==
 
== Advanced Windows 32 Base API DLL (advapi32.dll) ==
 +
 +
RegQueryValueEx<BR>
 +
* hKey identifies a currently opened key
 +
* lpValueName contains the name of the value to be queried
 +
* lpReserved must be null
 +
* lpType will receive key's value type
 +
* lpData points to a buffer that receives the value’s data
 +
* lpcbData specifies the size, in bytes, of the buffer pointed to by the lpData parameter
 +
 +
Step 1:<BR>
 +
Create a MODULE called "modREgKeYmAker" (or whatever you like) added to your VB6 Project
 +
 +
Step 2:<BR>
 +
Cut and paste the following code:
 +
 +
  <nowiki>Option Explicit</nowiki>
 +
  <nowiki></nowiki>
 +
  <nowiki>Declare Function RegCloseKey Lib "advapi32.dll" (ByVal HKEY As Long) As Long</nowiki>
 +
  <nowiki>Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, phkResult As Long) As Long</nowiki>
 +
  <nowiki>Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String) As Long</nowiki>
 +
  <nowiki>Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal HKEY As Long, ByVal lpValueName As String) As Long</nowiki>
 +
  <nowiki>Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, phkResult As Long) As Long</nowiki>
 +
  <nowiki>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</nowiki>
 +
  <nowiki>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</nowiki>
 +
  <nowiki></nowiki>
 +
  <nowiki>Public Const HKEY_CLASSES_ROOT = &H80000000</nowiki>
 +
  <nowiki>Public Const HKEY_CURRENT_USER = &H80000001</nowiki>
 +
  <nowiki>Public Const HKEY_LOCAL_MACHINE = &H80000002</nowiki>
 +
  <nowiki>Public Const HKEY_USERS = &H80000003</nowiki>
 +
  <nowiki>Public Const HKEY_PERFORMANCE_DATA = &H80000004</nowiki>
 +
  <nowiki>Public Const REG_SZ = 1</nowiki>
 +
  <nowiki></nowiki>
 +
  <nowiki>Private Const ERROR_SUCCESS As Long = 0</nowiki>
 +
  <nowiki></nowiki>
 +
  <nowiki>Function RegQueryStringValue(ByVal HKEY As Long, ByVal strValueName As String)</nowiki>
 +
  <nowiki>    Dim frmMain As Form</nowiki>
 +
  <nowiki>    Dim lResult As Long</nowiki>
 +
  <nowiki>    Dim lValueType As Long</nowiki>
 +
  <nowiki>    Dim strBuf As String</nowiki>
 +
  <nowiki>    Dim lDataBufSize As Long</nowiki>
 +
  <nowiki>        On Error GoTo 0</nowiki>
 +
  <nowiki>    lResult = RegQueryValueEx(HKEY, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize)</nowiki>
 +
  <nowiki>    If lResult = ERROR_SUCCESS Then</nowiki>
 +
  <nowiki>        If lValueType = REG_SZ Then</nowiki>
 +
  <nowiki>            strBuf = String(lDataBufSize, " ")</nowiki>
 +
  <nowiki>            lResult = RegQueryValueEx(HKEY, strValueName, 0&, 0&, ByVal strBuf, lDataBufSize)</nowiki>
 +
  <nowiki>            If lResult = ERROR_SUCCESS Then</nowiki>
 +
  <nowiki>                RegQueryStringValue = StripTerminator(strBuf)</nowiki>
 +
  <nowiki>            End If</nowiki>
 +
  <nowiki>        End If</nowiki>
 +
  <nowiki>    End If</nowiki>
 +
  <nowiki>End Function</nowiki>
 +
  <nowiki></nowiki>
 +
  <nowiki>Public Function GetSettingEx(HKEY As Long, sPath As String, sValue As String)</nowiki>
 +
  <nowiki>    Dim KeyHand&</nowiki>
 +
  <nowiki>    Dim datatype&</nowiki>
 +
  <nowiki>    Call RegOpenKey(HKEY, sPath, KeyHand&)</nowiki>
 +
  <nowiki>    GetSettingEx = RegQueryStringValue(KeyHand&, sValue)</nowiki>
 +
  <nowiki>    Call RegCloseKey(KeyHand&)</nowiki>
 +
  <nowiki>End Function</nowiki>
 +
  <nowiki></nowiki>
 +
  <nowiki>Function StripTerminator(ByVal strString As String) As String</nowiki>
 +
  <nowiki>    Dim intZeroPos As Integer</nowiki>
 +
  <nowiki></nowiki>
 +
  <nowiki>    intZeroPos = InStr(strString, Chr$(0))</nowiki>
 +
  <nowiki>    If intZeroPos > 0 Then</nowiki>
 +
  <nowiki>        StripTerminator = Left$(strString, intZeroPos - 1)</nowiki>
 +
  <nowiki>    Else</nowiki>
 +
  <nowiki>        StripTerminator = strString</nowiki>
 +
  <nowiki>    End If</nowiki>
 +
  <nowiki>End Function</nowiki>
 +
  <nowiki></nowiki>
 +
  <nowiki>Public Sub SaveSettingEx(HKEY As Long, sPath As String, sValue As String, sData As String)</nowiki>
 +
  <nowiki>    Dim KeyHand As Long</nowiki>
 +
  <nowiki>    Call RegCreateKey(HKEY, sPath, KeyHand)</nowiki>
 +
  <nowiki>    Call RegSetValueEx(KeyHand&, sValue, 0, REG_SZ, ByVal sData, Len(sData))</nowiki>
 +
  <nowiki>    Call RegCloseKey(KeyHand&)</nowiki>
 +
  <nowiki>End Sub</nowiki>
 +
  <nowiki></nowiki>
 +
  
 
&nbsp;
 
&nbsp;

Revision as of 07:25, 2 February 2008

You can read from and write to the Windows Registry with your Visual BASIC program. There are basically two main approaches to accomplishing this in your Visual BASIC program.

  • Using Visual BASIC Built in Registry Functions
  • Advanced Windows 32 Base API DLL (advapi32.dll)

What's the difference?

Using built in registry functions limits you a great deal. Using the API allows you complete access to the registry from an application level. Using the built in functions is quick and easy. Using the API requires a bit more work and external function calls. Using the built in functions is safer.

Visual BASIC Built In Registry Functions

The functions are :

  • GetSetting
  • SaveSetting

SaveSetting stores the data in the Registry (in HKEY_CURRENT_USER|Software|VB and VBA Program Settings|YourAppName). The four parts of the function are the name under which it is stored. That is the main limitation, being where the data is stored. All of your registry data for your application has to be under "VB and VBA Program Settings" under the HKEY_CURRENT_USER hive.

Parameters

  • AppName: Required. String expression containing the name of the application or project to which the setting applies.
  • Section: Required. String expression containing the name of the section in which the key setting is being saved.
  • Key: Required. String expression containing the name of the key setting being saved.
  • Setting: Required. Expression containing the value to which Key is being set.

The SaveSetting function adds the key to HKEY_CURRENT_USER\Software\VB and VBA Program Settings. SaveSetting requires that a user be logged on since it operates under the HKEY_LOCAL_USER registry key, which is not active until a user logs on interactively.

HKEY_CURRENT_USER\Software\VB and VBA

Code Example:

Private Sub Form_Load()
  Me.Left = GetSetting(App.Title, "Settings", "MainLeft", 1000)
  Me.Top = GetSetting(App.Title, "Settings", "MainTop", 1000)
  Me.Width = GetSetting(App.Title, "Settings", "MainWidth", 6500)
  Me.Height = GetSetting(App.Title, "Settings", "MainHeight", 6500)
End Sub
Private Sub Form_Unload(Cancel As Integer)
  If Me.WindowState <> vbMinimized Then
    SaveSetting App.Title, "Settings", "MainLeft", Me.Left
    SaveSetting App.Title, "Settings", "MainTop", Me.Top
    SaveSetting App.Title, "Settings", "MainWidth", Me.Width
    SaveSetting App.Title, "Settings", "MainHeight", Me.Height
  End If
End Sub

 

Advanced Windows 32 Base API DLL (advapi32.dll)

RegQueryValueEx

  • hKey identifies a currently opened key
  • lpValueName contains the name of the value to be queried
  • lpReserved must be null
  • lpType will receive key's value type
  • lpData points to a buffer that receives the value’s data
  • lpcbData specifies the size, in bytes, of the buffer pointed to by the lpData parameter

Step 1:
Create a MODULE called "modREgKeYmAker" (or whatever you like) added to your VB6 Project

Step 2:
Cut and paste the following code:

 Option Explicit
 
 Declare Function RegCloseKey Lib "advapi32.dll" (ByVal HKEY As Long) As Long
 Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, phkResult As Long) As Long
 Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String) As Long
 Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal HKEY As Long, ByVal lpValueName As String) As Long
 Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal HKEY As Long, ByVal lpSubKey As String, phkResult As Long) As Long
 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
 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
 
 Public Const HKEY_CLASSES_ROOT = &H80000000
 Public Const HKEY_CURRENT_USER = &H80000001
 Public Const HKEY_LOCAL_MACHINE = &H80000002
 Public Const HKEY_USERS = &H80000003
 Public Const HKEY_PERFORMANCE_DATA = &H80000004
 Public Const REG_SZ = 1
 
 Private Const ERROR_SUCCESS As Long = 0
 
 Function RegQueryStringValue(ByVal HKEY As Long, ByVal strValueName As String)
     Dim frmMain As Form
     Dim lResult As Long
     Dim lValueType As Long
     Dim strBuf As String
     Dim lDataBufSize As Long
         On Error GoTo 0
     lResult = RegQueryValueEx(HKEY, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize)
     If lResult = ERROR_SUCCESS Then
         If lValueType = REG_SZ Then
             strBuf = String(lDataBufSize, " ")
             lResult = RegQueryValueEx(HKEY, strValueName, 0&, 0&, ByVal strBuf, lDataBufSize)
             If lResult = ERROR_SUCCESS Then
                 RegQueryStringValue = StripTerminator(strBuf)
             End If
         End If
     End If
 End Function
 
 Public Function GetSettingEx(HKEY As Long, sPath As String, sValue As String)
     Dim KeyHand&
     Dim datatype&
     Call RegOpenKey(HKEY, sPath, KeyHand&)
     GetSettingEx = RegQueryStringValue(KeyHand&, sValue)
     Call RegCloseKey(KeyHand&)
 End Function
 
 Function StripTerminator(ByVal strString As String) As String
     Dim intZeroPos As Integer
 
     intZeroPos = InStr(strString, Chr$(0))
     If intZeroPos > 0 Then
         StripTerminator = Left$(strString, intZeroPos - 1)
     Else
         StripTerminator = strString
     End If
 End Function
 
 Public Sub SaveSettingEx(HKEY As Long, sPath As String, sValue As String, sData As String)
     Dim KeyHand As Long
     Call RegCreateKey(HKEY, sPath, KeyHand)
     Call RegSetValueEx(KeyHand&, sValue, 0, REG_SZ, ByVal sData, Len(sData))
     Call RegCloseKey(KeyHand&)
 End Sub