home *** CD-ROM | disk | FTP | other *** search
/ distrib.akp.su/Programming/Vb-6+Rus/ / distrib.akp.su.tar / distrib.akp.su / Programming / Vb-6+Rus / VB98 / TEMPLATE / CODE / REGKEYS.BAS < prev   
BASIC Source File  |  1998-06-18  |  11KB  |  238 lines

  1. Attribute VB_Name = "RegKeys"
  2. ' This module reads and writes registry keys.  Unlike the
  3. ' internal registry access methods of VB, it can read and
  4. ' write any registry keys with string values.
  5.  
  6. Option Explicit
  7. '---------------------------------------------------------------
  8. '-Registry API Declarations...
  9. '---------------------------------------------------------------
  10. Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
  11. Private Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long
  12. Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
  13. Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
  14. Private Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
  15.  
  16. '---------------------------------------------------------------
  17. '- Registry Api Constants...
  18. '---------------------------------------------------------------
  19. ' Reg Data Types...
  20. Const REG_SZ = 1                         ' Unicode nul terminated string
  21. Const REG_EXPAND_SZ = 2                  ' Unicode nul terminated string
  22. Const REG_DWORD = 4                      ' 32-bit number
  23.  
  24. ' Reg Create Type Values...
  25. Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted
  26.  
  27. ' Reg Key Security Options...
  28. Const READ_CONTROL = &H20000
  29. Const KEY_QUERY_VALUE = &H1
  30. Const KEY_SET_VALUE = &H2
  31. Const KEY_CREATE_SUB_KEY = &H4
  32. Const KEY_ENUMERATE_SUB_KEYS = &H8
  33. Const KEY_NOTIFY = &H10
  34. Const KEY_CREATE_LINK = &H20
  35. Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
  36. Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
  37. Const KEY_EXECUTE = KEY_READ
  38. Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + _
  39.                        KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
  40.                        KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
  41.                      
  42. ' Reg Key ROOT Types...
  43. Const HKEY_CLASSES_ROOT = &H80000000
  44. Const HKEY_CURRENT_USER = &H80000001
  45. Const HKEY_LOCAL_MACHINE = &H80000002
  46. Const HKEY_USERS = &H80000003
  47. Const HKEY_PERFORMANCE_DATA = &H80000004
  48.  
  49. ' Return Value...
  50. Const ERROR_NONE = 0
  51. Const ERROR_BADKEY = 2
  52. Const ERROR_ACCESS_DENIED = 8
  53. Const ERROR_SUCCESS = 0
  54.  
  55. '---------------------------------------------------------------
  56. '- Registry Security Attributes TYPE...
  57. '---------------------------------------------------------------
  58. Private Type SECURITY_ATTRIBUTES
  59.     nLength As Long
  60.     lpSecurityDescriptor As Long
  61.     bInheritHandle As Boolean
  62. End Type
  63.  
  64. ' The resource string will be loaded into a control's property as follows:
  65. ' Object      Property
  66. ' Form        Caption
  67. ' Menu        Caption
  68. ' TabStrip    Caption, ToolTipText
  69. ' Toolbar     ToolTipText
  70. ' ListView    ColumnHeader.Text
  71.  
  72. Sub LoadResStrings(frm As Form)
  73.   On Error Resume Next
  74.   
  75.   Dim ctl As Control
  76.   Dim obj As Object
  77.   
  78.   'set the form's caption
  79.   If IsNumeric(frm.Tag) Then
  80.     frm.Caption = LoadResString(CInt(frm.Tag))
  81.   End If
  82.   
  83.   'set the controls' captions using the caption
  84.   'property for menu items and the Tag property
  85.   'for all other controls
  86.   For Each ctl In frm.Controls
  87.     Err.Clear
  88.     If TypeName(ctl) = "Menu" Then
  89.       If IsNumeric(ctl.Caption) Then
  90.         If Err = 0 Then
  91.           ctl.Caption = LoadResString(CInt(ctl.Caption))
  92.         End If
  93.       End If
  94.     ElseIf TypeName(ctl) = "TabStrip" Then
  95.       For Each obj In ctl.Tabs
  96.         Err.Clear
  97.         If IsNumeric(obj.Tag) Then
  98.           obj.Caption = LoadResString(CInt(obj.Tag))
  99.         End If
  100.         'check for a tooltip
  101.         If IsNumeric(obj.ToolTipText) Then
  102.           If Err = 0 Then
  103.             obj.ToolTipText = LoadResString(CInt(obj.ToolTipText))
  104.           End If
  105.         End If
  106.       Next
  107.     ElseIf TypeName(ctl) = "Toolbar" Then
  108.       For Each obj In ctl.Buttons
  109.         Err.Clear
  110.         If IsNumeric(obj.Tag) Then
  111.           obj.ToolTipText = LoadResString(CInt(obj.Tag))
  112.         End If
  113.       Next
  114.     ElseIf TypeName(ctl) = "ListView" Then
  115.       For Each obj In ctl.ColumnHeaders
  116.         Err.Clear
  117.         If IsNumeric(obj.Tag) Then
  118.           obj.Text = LoadResString(CInt(obj.Tag))
  119.         End If
  120.       Next
  121.     Else
  122.       If IsNumeric(ctl.Tag) Then
  123.         If Err = 0 Then
  124.           ctl.Caption = LoadResString(CInt(ctl.Tag))
  125.         End If
  126.       End If
  127.       'check for a tooltip
  128.       If IsNumeric(ctl.ToolTipText) Then
  129.         If Err = 0 Then
  130.           ctl.ToolTipText = LoadResString(CInt(ctl.ToolTipText))
  131.         End If
  132.       End If
  133.     End If
  134.   Next
  135.  
  136. End Sub
  137.  
  138. '-------------------------------------------------------------------------------------------------
  139. 'sample usage - Debug.Print UpodateKey(HKEY_CLASSES_ROOT, "keyname", "newvalue")
  140. '-------------------------------------------------------------------------------------------------
  141. Public Function UpdateKey(KeyRoot As Long, KeyName As String, SubKeyName As String, SubKeyValue As String) As Boolean
  142.     Dim rc As Long                                      ' Return Code
  143.     Dim hKey As Long                                    ' Handle To A Registry Key
  144.     Dim hDepth As Long                                  '
  145.     Dim lpAttr As SECURITY_ATTRIBUTES                   ' Registry Security Type
  146.     
  147.     lpAttr.nLength = 50                                 ' Set Security Attributes To Defaults...
  148.     lpAttr.lpSecurityDescriptor = 0                     ' ...
  149.     lpAttr.bInheritHandle = True                        ' ...
  150.  
  151.     '------------------------------------------------------------
  152.     '- Create/Open Registry Key...
  153.     '------------------------------------------------------------
  154.     rc = RegCreateKeyEx(KeyRoot, KeyName, _
  155.                         0, REG_SZ, _
  156.                         REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, lpAttr, _
  157.                         hKey, hDepth)                   ' Create/Open //KeyRoot//KeyName
  158.     
  159.     If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError   ' Handle Errors...
  160.     
  161.     '------------------------------------------------------------
  162.     '- Create/Modify Key Value...
  163.     '------------------------------------------------------------
  164.     If (SubKeyValue = "") Then SubKeyValue = " "        ' A Space Is Needed For RegSetValueEx() To Work...
  165.     
  166.     ' Create/Modify Key Value
  167.     rc = RegSetValueEx(hKey, SubKeyName, _
  168.                        0, REG_SZ, _
  169.                        SubKeyValue, LenB(StrConv(SubKeyValue, vbFromUnicode)))
  170.                        
  171.     If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError   ' Handle Error
  172.     '------------------------------------------------------------
  173.     '- Close Registry Key...
  174.     '------------------------------------------------------------
  175.     rc = RegCloseKey(hKey)                              ' Close Key
  176.     
  177.     UpdateKey = True                                    ' Return Success
  178.     Exit Function                                       ' Exit
  179. CreateKeyError:
  180.     UpdateKey = False                                   ' Set Error Return Code
  181.     rc = RegCloseKey(hKey)                              ' Attempt To Close Key
  182. End Function
  183.  
  184. '-------------------------------------------------------------------------------------------------
  185. 'sample usage - Debug.Print GetKeyValue(HKEY_CLASSES_ROOT, "COMCTL.ListviewCtrl.1\CLSID", "")
  186. '-------------------------------------------------------------------------------------------------
  187. Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String) As String
  188.     Dim i As Long                                           ' Loop Counter
  189.     Dim rc As Long                                          ' Return Code
  190.     Dim hKey As Long                                        ' Handle To An Open Registry Key
  191.     Dim hDepth As Long                                      '
  192.     Dim sKeyVal As String
  193.     Dim lKeyValType As Long                                 ' Data Type Of A Registry Key
  194.     Dim tmpVal As String                                    ' Tempory Storage For A Registry Key Value
  195.     Dim KeyValSize As Long                                  ' Size Of Registry Key Variable
  196.     
  197.     ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
  198.     '------------------------------------------------------------
  199.     rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' Open Registry Key
  200.     
  201.     If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Error...
  202.     
  203.     tmpVal = String$(1024, 0)                             ' Allocate Variable Space
  204.     KeyValSize = 1024                                       ' Mark Variable Size
  205.     
  206.     '------------------------------------------------------------
  207.     ' Retrieve Registry Key Value...
  208.     '------------------------------------------------------------
  209.     rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
  210.                          lKeyValType, tmpVal, KeyValSize)    ' Get/Create Key Value
  211.                         
  212.     If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Errors
  213.       
  214.     tmpVal = Left$(tmpVal, InStr(tmpVal, Chr(0)) - 1)
  215.  
  216.     '------------------------------------------------------------
  217.     ' Determine Key Value Type For Conversion...
  218.     '------------------------------------------------------------
  219.     Select Case lKeyValType                                  ' Search Data Types...
  220.     Case REG_SZ, REG_EXPAND_SZ                              ' String Registry Key Data Type
  221.         sKeyVal = tmpVal                                     ' Copy String Value
  222.     Case REG_DWORD                                          ' Double Word Registry Key Data Type
  223.         For i = Len(tmpVal) To 1 Step -1                    ' Convert Each Bit
  224.             sKeyVal = sKeyVal + Hex(Asc(Mid(tmpVal, i, 1)))   ' Build Value Char. By Char.
  225.         Next
  226.         sKeyVal = Format$("&h" + sKeyVal)                     ' Convert Double Word To String
  227.     End Select
  228.     
  229.     GetKeyValue = sKeyVal                                   ' Return Value
  230.     rc = RegCloseKey(hKey)                                  ' Close Registry Key
  231.     Exit Function                                           ' Exit
  232.     
  233. GetKeyError:    ' Cleanup After An Error Has Occured...
  234.     GetKeyValue = vbNullString                              ' Set Return Val To Empty String
  235.     rc = RegCloseKey(hKey)                                  ' Close Registry Key
  236. End Function
  237.  
  238.