玩酷之家

 找回密码
 给爷来个号
查看: 463|回复: 0

VB API 操作注册表实例(添加、修改、删除键值)

[复制链接]

98

主题

103

帖子

430

积分

中级会员

Rank: 3Rank: 3

积分
430
发表于 2020-2-26 14:59:50 | 显示全部楼层 |阅读模式
因为VB6生成的程序为32位,此代码在64位系统中,
操作HKEY_LOCAL_MACHINE\SOFTWARE下的注册表将自动定位到HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node下。
如需操作HKEY_LOCAL_MACHINE\SOFTWARE下的注册表,则需要添加KEY_WOW64_64KEY参数。
操作例:RegOpenKeyEx(lKey, sKeyName, 0, KEY_ALL_ACCESS Or KEY_WOW64_64KEY, hKey)

  1. 'Module
  2. Option Explicit
  3. Global Const REG_SZ As Long = 1
  4. Global Const REG_DWORD As Long = 4
  5. Global Const HKEY_CLASSES_ROOT = &H80000000
  6. Global Const HKEY_CURRENT_USER = &H80000001
  7. Global Const HKEY_LOCAL_MACHINE = &H80000002
  8. Global Const HKEY_USERS = &H80000003

  9. Global Const ERROR_NONE = 0
  10. Global Const ERROR_BADDB = 1
  11. Global Const ERROR_BADKEY = 2
  12. Global Const ERROR_CANTOPEN = 3
  13. Global Const ERROR_CANTREAD = 4
  14. Global Const ERROR_CANTWRITE = 5
  15. Global Const ERROR_OUTOFMEMORY = 6
  16. Global Const ERROR_INVALID_PARAMETER = 7
  17. Global Const ERROR_ACCESS_DENIED = 8
  18. Global Const ERROR_INVALID_PARAMETERS = 87
  19. Global Const ERROR_NO_MORE_ITEMS = 259

  20. Global Const KEY_ALL_ACCESS = &H3F
  21. Global Const REG_OPTION_NON_VOLATILE = 0

  22. Global Const KEY_WOW64_64KEY = &H100&
  23. Global Const KEY_WOW64_32KEY = &H200&

  24. Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  25. Declare Function RegCreateKeyEx Lib "advapi32.dll" 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, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
  26. Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
  27. Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
  28. Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
  29. Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
  30. Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
  31. Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
  32. Private Declare Function RegDeleteKey& Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String)
  33. Private Declare Function RegDeleteValue& Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String)

  34. Public Function DelKey(lKey As Long, sKeyName As String)
  35.     Dim lVal As Long
  36.     Dim hKey As Long
  37.     lVal = RegOpenKeyEx(lKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
  38.     lVal = RegDeleteKey(lKey, sKeyName)
  39.     RegCloseKey (hKey)
  40. End Function

  41. Public Function DelVal(lKey As Long, sKeyName As String, sValueName As String)
  42.     Dim lVal As Long
  43.     Dim hKey As Long
  44.     lVal = RegOpenKeyEx(lKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
  45.     lVal = RegDeleteValue(hKey, sValueName)
  46.     RegCloseKey (hKey)
  47. End Function

  48. Public Function SetVal(ByVal hKey As Long, sValueName As String, lType As Long, vValue As Variant) As Long
  49.     Dim lValue As Long
  50.     Dim sValue As String
  51.     Select Case lType
  52.     Case REG_SZ
  53.         sValue = vValue
  54.         SetVal = RegSetValueExString(hKey, sValueName, 0&, lType, sValue, LenB(StrConv(sValue, vbFromUnicode)))
  55.     Case REG_DWORD
  56.         lValue = vValue
  57.         SetVal = RegSetValueExLong(hKey, sValueName, 0&, lType, lValue, 4)
  58.     End Select
  59. End Function

  60. Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As String, vValue As Variant) As Long
  61.     Dim cch As Long
  62.     Dim lrc As Long
  63.     Dim lType As Long
  64.     Dim lValue As Long
  65.     Dim sValue As String
  66.     On Error Resume Next
  67.     lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
  68.     If lrc <> ERROR_NONE Then Error 5
  69.     Select Case lType
  70.     Case REG_SZ
  71.         sValue = String(cch, 0)
  72.         lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
  73.         If lrc = ERROR_NONE Then
  74.             vValue = Left$(sValue, cch)
  75.         Else
  76.             vValue = Empty
  77.         End If
  78.     Case REG_DWORD
  79.         lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
  80.         If lrc = ERROR_NONE Then vValue = lValue
  81.     Case Else
  82.         lrc = -1
  83.     End Select
  84.     QueryValueEx = lrc
  85. End Function

  86. Public Function CreateNewKey(lKey As Long, sNewKeyName As String)
  87.     Dim hNewKey As Long
  88.     Dim lVal As Long
  89.     lVal = RegCreateKeyEx(lKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lVal)
  90.     RegCloseKey (hNewKey)
  91. End Function

  92. Public Function SetKeyValue(lKey As Long, sKeyName As String, sValueName As String, vValueSetting As Variant, lValueType As Long)
  93.     Dim lVal As Long
  94.     Dim hKey As Long
  95.     lVal = RegOpenKeyEx(lKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
  96.     lVal = SetVal(hKey, sValueName, lValueType, vValueSetting)
  97.     RegCloseKey (hKey)
  98. End Function

  99. Public Function QueryValue(lKey As Long, sKeyName As String, sValueName As String)
  100.     Dim lVal As Long
  101.     Dim hKey As Long
  102.     Dim vValue As Variant
  103.     lVal = RegOpenKeyEx(lKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
  104.     lVal = QueryValueEx(hKey, sValueName, vValue)
  105.     QueryValue = vValue
  106.     RegCloseKey (hKey)
  107. End Function

复制代码


使用方法:

  1.     '新建项
  2.     CreateNewKey HKEY_CURRENT_USER, "Software\SubKey1\SubKey2"
  3.     '新建、修改键值
  4.     SetKeyValue HKEY_CURRENT_USER, "Software\SubKey1\SubKey2", "Test", "这是测试注册表的程序", REG_SZ
  5.     '查询键值。
  6.     MsgBox QueryValue(HKEY_CURRENT_USER, "Software\SubKey1\SubKey2", "Test")
  7.     '删除键值
  8.     DelVal HKEY_CURRENT_USER, "Software\SubKey1\SubKey2", "Test"
  9.     '删除项
  10.     DelKey HKEY_CURRENT_USER, "Software\SubKey1\SubKey2"
复制代码







最多选择要压缩的20张 PNG图像
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 给爷来个号

本版积分规则

QQ|Archiver|手机版|小黑屋|玩酷之家 ( 鄂ICP备14012049号-1 )|

GMT+8, 2020-8-6 05:52 , Processed in 1.076402 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表