乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
楼主: Jeff_M

用VBA写入注册表???

[复制链接]

71

主题

928

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1230
发表于 2007-6-13 23:09:15 | 显示全部楼层

嘿,杰夫,是的,代码运行得很好,但我还需要进一步扩展
我想写一个完整的VBSCript来写注册表的路径,这样我就不会'甚至不需要打开ACAD
话虽如此,我试着在VBA中运行它,但它仍然返回空变量
我去了参考并检查了类型库Windows脚本主机对象模型,但仍然是pt主机对象模型,但代码仍然失败,我必须检查另一个类型库;你能告诉我这是什么或者我做错了什么吗
现在,我想我看到了在VBA中运行这段代码的情况,至少是现在的情况。代码直接写入注册表,但是ACAD直到您之后才更新注册表;“关闭”;ACAD
你可以很容易地测试出来。手动更改注册表中的路径,关闭ACAD,注意ACAD将覆盖您的手动更改。所以,我要说的是,在关闭ACAD后,它将覆盖注册表
因此,根据这一逻辑,它将C3D2008视为;最后一个“;ACAD的访问版本;不是“;2002年。您在2002年运行了代码,但在关闭2002年之后,2002年用2002年存储的当前路径重写了注册表。因此,记住这个逻辑,我们不能在VBA中使用此代码。注册表直到ACAD关闭后才更新,因此ACAD;必须“;关闭以使此代码有效工作
这让我们回到剧本。我提供的代码很好,但在VBA内部并不好,至少在我看来是这样
它确实是为了在ACAD之外工作而写的
希望这有意义
那么你使用的是Civil Design 2008?我正在慢慢学习2006年的土木设计;我对民用设计一无所知
我熟悉原生AutoCAD,但LDD和Civil Design是一个全新的怪物
你有没有检查过土木设计中的skdm文件
标记
回复

使用道具 举报

71

主题

928

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1230
发表于 2007-6-13 23:10:20 | 显示全部楼层

Jeff尝试将代码放入vbs文件;关闭ACAD,然后运行它
您将看到它按计划工作
回复

使用道具 举报

71

主题

928

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1230
发表于 2007-6-14 21:32:54 | 显示全部楼层
这段代码运行得很好。花时间阅读不同的函数及其示例
我相信只要它在模块中就可以工作
  1. '
  2. ' Created by E.Spencer (elliot@spnc.demon.co.uk) - This code is public domain.
  3. '
  4. Option Explicit
  5. Global sValue As String
  6. 'Security Mask constants
  7. Public Const READ_CONTROL = &H20000
  8. Public Const SYNCHRONIZE = &H100000
  9. Public Const STANDARD_RIGHTS_ALL = &H1F0000
  10. Public Const STANDARD_RIGHTS_READ = READ_CONTROL
  11. Public Const STANDARD_RIGHTS_WRITE = READ_CONTROL
  12. Public Const KEY_QUERY_VALUE = &H1
  13. Public Const KEY_SET_VALUE = &H2
  14. Public Const KEY_CREATE_SUB_KEY = &H4
  15. Public Const KEY_ENUMERATE_SUB_KEYS = &H8
  16. Public Const KEY_NOTIFY = &H10
  17. Public Const KEY_CREATE_LINK = &H20
  18. Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or _
  19.    KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or _
  20.    KEY_CREATE_LINK) And (Not SYNCHRONIZE))
  21. Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _
  22.    KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
  23. Public Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
  24. Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE _
  25.    Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
  26. ' Possible registry data types
  27. Public Enum InTypes
  28.    ValNull = 0
  29.    ValString = 1
  30.    ValXString = 2
  31.    ValBinary = 3
  32.    ValDWord = 4
  33.    ValLink = 6
  34.    ValMultiString = 7
  35.    ValResList = 8
  36. End Enum
  37. Public Type typSrings
  38.   Val1 As String
  39.   Val2 As String
  40. End Type
  41. ' Registry value type definitions
  42. Public Const REG_NONE As Long = 0
  43. Public Const REG_SZ As Long = 1
  44. Public Const REG_EXPAND_SZ As Long = 2
  45. Public Const REG_BINARY As Long = 3
  46. Public Const REG_DWORD As Long = 4
  47. Public Const REG_LINK As Long = 6
  48. Public Const REG_MULTI_SZ As Long = 7
  49. Public Const REG_RESOURCE_LIST As Long = 8
  50. ' Registry section definitions
  51. Public Const HKEY_CLASSES_ROOT = &H80000000
  52. Public Const HKEY_CURRENT_USER = &H80000001
  53. Public Const HKEY_LOCAL_MACHINE = &H80000002
  54. Public Const HKEY_USERS = &H80000003
  55. Public Const HKEY_PERFORMANCE_DATA = &H80000004
  56. Public Const HKEY_CURRENT_CONFIG = &H80000005
  57. Public Const HKEY_DYN_DATA = &H80000006
  58. ' Codes returned by Reg API calls
  59. Private Const ERROR_NONE = 0
  60. Private Const ERROR_BADDB = 1
  61. Private Const ERROR_BADKEY = 2
  62. Private Const ERROR_CANTOPEN = 3
  63. Private Const ERROR_CANTREAD = 4
  64. Private Const ERROR_CANTWRITE = 5
  65. Private Const ERROR_OUTOFMEMORY = 6
  66. Private Const ERROR_INVALID_PARAMETER = 7
  67. Private Const ERROR_ACCESS_DENIED = 8
  68. Private Const ERROR_INVALID_PARAMETERS = 87
  69. Private Const ERROR_NO_MORE_ITEMS = 259
  70. ' Registry API functions used in this module (there are more of them)
  71. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  72. Private 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
  73. Private Declare Function RegQueryValueEx 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
  74. Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
  75. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  76. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  77. Private 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
  78. Private 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
  79. Private Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  80. Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
  81. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  82. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
  83. ' This routine allows you to get values from anywhere in the Registry, it currently
  84. ' only handles string, double word and binary values. Binary values are returned as
  85. ' hex strings.
  86. '
  87. ' Example
  88. ' Text1.Text = ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", "DefaultUserName")
  89. '
  90. Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String) As String
  91. Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long, sValue As String, td As Double
  92. Dim TStr1 As String, TStr2 As String
  93. Dim i As Integer
  94. On Error Resume Next
  95. lResult = RegOpenKey(Group, Section, lKeyValue)
  96. sValue = Space$(2048)
  97. lValueLength = Len(sValue)
  98. lResult = RegQueryValueEx(lKeyValue, Key, 0&, lDataTypeValue, sValue, lValueLength)
  99. If (lResult = 0) And (Err.Number = 0) Then
  100.    If lDataTypeValue = REG_DWORD Then
  101.       td = Asc(Mid$(sValue, 1, 1)) + &H100& * Asc(Mid$(sValue, 2, 1)) + &H10000 * Asc(Mid$(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(sValue, 4, 1)))
  102.       sValue = Format$(td, "000")
  103.    End If
  104.    If lDataTypeValue = REG_BINARY Then
  105.        ' Return a binary field as a hex string (2 chars per byte)
  106.        TStr2 = ""
  107.        For i = 1 To lValueLength
  108.           TStr1 = Hex(Asc(Mid(sValue, i, 1)))
  109.           If Len(TStr1) = 1 Then TStr1 = "0" & TStr1
  110.           TStr2 = TStr2 + TStr1
  111.        Next
  112.        sValue = TStr2
  113.    Else
  114.       sValue = Left$(sValue, lValueLength - 1)
  115.    End If
  116. Else
  117.    sValue = "Not Found"
  118. End If
  119. lResult = RegCloseKey(lKeyValue)
  120. ReadRegistry = sValue
  121. End Function
  122. ' This routine allows you to write values into the entire Registry, it currently
  123. ' only handles string and double word values.
  124. '
  125. ' Example
  126. ' WriteRegistry HKEY_CURRENT_USER, "SOFTWARE\My Name\My App", "NewSubKey", ValString, "NewValueHere"
  127. ' WriteRegistry HKEY_CURRENT_USER, "SOFTWARE\My Name\My App", "NewSubKey", ValDWord, "31"
  128. '
  129. Public Sub WriteRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String, ByVal ValType As InTypes, ByVal Value As Variant)
  130. Dim lResult As Long
  131. Dim lKeyValue As Long
  132. Dim InLen As Long
  133. Dim lNewVal As Long
  134. Dim sNewVal As String
  135. On Error Resume Next
  136. lResult = RegCreateKey(Group, Section, lKeyValue)
  137. If ValType = ValDWord Then
  138.    lNewVal = CLng(Value)
  139.    InLen = 4
  140.    lResult = RegSetValueExLong(lKeyValue, Key, 0&, ValType, lNewVal, InLen)
  141. Else
  142.    ' Fixes empty string bug - spotted by Marcus Jansson
  143.    If ValType = ValString Then Value = Value + Chr(0)
  144.    sNewVal = Value
  145.    InLen = Len(sNewVal)
  146.    lResult = RegSetValueExString(lKeyValue, Key, 0&, 1&, sNewVal, InLen)
  147. End If
  148. lResult = RegFlushKey(lKeyValue)
  149. lResult = RegCloseKey(lKeyValue)
  150. End Sub
  151. ' This routine enumerates the subkeys under any given key
  152. ' Call repeatedly until "Not Found" is returned - store values in array or something
  153. '
  154. ' Example - this example just adds all the subkeys to a string - you will probably want to
  155. ' save then into an array or something.
  156. '
  157. ' Dim Res, NewLine As String
  158. ' Dim i As Long
  159. ' Res = ReadRegistryGetSubkey(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", i)
  160. ' NewLine = ""
  161. ' Do Until Res = "Not Found"
  162. '   Text1.Text = Text1.Text & NewLine & Res
  163. '   i = i + 1
  164. '   Res = ReadRegistryGetSubkey(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", i)
  165. '   NewLine = Chr(13) & Chr(10)
  166. ' Loop
  167. Public Function ReadRegistryGetSubkey(ByVal Group As Long, ByVal Section As String, Idx As Long) As String
  168. Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long, sValue As String, td As Double
  169. On Error Resume Next
  170. lResult = RegOpenKey(Group, Section, lKeyValue)
  171. sValue = Space$(2048)
  172. lValueLength = Len(sValue)
  173. lResult = RegEnumKey(lKeyValue, Idx, sValue, lValueLength)
  174. If (lResult = 0) And (Err.Number = 0) Then
  175.    sValue = Left$(sValue, InStr(sValue, Chr(0)) - 1)
  176. Else
  177.    sValue = "Not Found"
  178. End If
  179. lResult = RegCloseKey(lKeyValue)
  180. ReadRegistryGetSubkey = sValue
  181. End Function
  182. ' This routine allows you to get all the values from anywhere in the Registry under any
  183. ' given subkey, it currently only returns string and double word values.
  184. '
  185. ' Example - returns list of names/values to multiline text box
  186. ' Dim Res As Variant
  187. ' Dim i As Long
  188. ' Res = ReadRegistryGetAll(HKEY_CURRENT_USER, "Software\Microsoft\Notepad", i)
  189. ' Do Until Res(2) = "Not Found"
  190. '    Text1.Text = Text1.Text & Chr(13) & Chr(10) & Res(1) & " " & Res(2)
  191. '    i = i + 1
  192. '    Res = ReadRegistryGetAll(HKEY_CURRENT_USER, "Software\Microsoft\Notepad", i)
  193. ' Loop
  194. '
  195. Public Function ReadRegistryGetAll(ByVal Group As Long, ByVal Section As String, Idx As Long) As Variant
  196. Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long
  197. Dim lValueLength As Long, lValueNameLength As Long
  198. Dim sValueName As String ', sValue As String
  199. Dim td As Double
  200. On Error Resume Next
  201. lResult = RegOpenKey(Group, Section, lKeyValue)
  202. sValue = Space$(2048)
  203. sValueName = Space$(2048)
  204. lValueLength = Len(sValue)
  205. lValueNameLength = Len(sValueName)
  206. lResult = RegEnumValue(lKeyValue, Idx, sValueName, lValueNameLength, 0&, lDataTypeValue, sValue, lValueLength)
  207. If (lResult = 0) And (Err.Number = 0) Then
  208.    If lDataTypeValue = REG_DWORD Then
  209.       td = Asc(Mid$(sValue, 1, 1)) + &H100& * Asc(Mid$(sValue, 2, 1)) + &H10000 * Asc(Mid$(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(sValue, 4, 1)))
  210.       sValue = Format$(td, "000")
  211.    End If
  212.    sValue = Left$(sValue, lValueLength - 1)
  213.    sValueName = Left$(sValueName, lValueNameLength)
  214. Else
  215.    sValue = "Not Found"
  216. End If
  217. lResult = RegCloseKey(lKeyValue)
  218. ' Return the datatype, value name and value as an array
  219. ReadRegistryGetAll = Array(lDataTypeValue, sValueName, sValue)
  220. End Function
  221. ' This routine deletes a specified key (and all its subkeys and values if on Win95) from the registry.
  222. ' Be very careful using this function.
  223. '
  224. ' Example
  225. ' DeleteSubkey HKEY_CURRENT_USER, "Software\My Name\My App"
  226. '
  227. Public Function DeleteSubkey(ByVal Group As Long, ByVal Section As String) As String
  228. Dim lResult As Long, lKeyValue As Long
  229. On Error Resume Next
  230. lResult = RegOpenKeyEx(Group, vbNullChar, 0&, KEY_ALL_ACCESS, lKeyValue)
  231. lResult = RegDeleteKey(lKeyValue, Section)
  232. lResult = RegCloseKey(lKeyValue)
  233. End Function
  234. ' This routine deletes a specified value from below a specified subkey.
  235. ' Be very careful using this function.
  236. '
  237. ' Example
  238. ' DeleteValue HKEY_CURRENT_USER, "Software\My Name\My App", "NewSubKey"
  239. '
  240. Public Function DeleteValue(ByVal Group As Long, ByVal Section As String, ByVal Key As String) As String
  241. Dim lResult As Long, lKeyValue As Long
  242. On Error Resume Next
  243. lResult = RegOpenKey(Group, Section, lKeyValue)
  244. lResult = RegDeleteValue(lKeyValue, Key)
  245. lResult = RegCloseKey(lKeyValue)
  246. End Function
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-7-6 19:43 , Processed in 1.319853 second(s), 56 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表