Advertisement
Guest User

Untitled

a guest
May 5th, 2017
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.76 KB | None | 0 0
  1. Option Explicit
  2.  
  3. ' --------------------------------------------------------------
  4. ' Update the Windows registry.
  5. ' Written by Kenneth Ives kenaso@home.com
  6. ' NT tested by Brett Gerhardi Brett.Gerhardi@trinite.co.uk
  7. '
  8. ' Perform the four basic functions on the Windows registry.
  9. ' Add
  10. ' Change
  11. ' Delete
  12. ' Query
  13. '
  14. ' Important: If you treat all key data strings as being
  15. ' case sensitive, you should never have a problem.
  16. ' Always backup your registry files (System.dat
  17. ' and User.dat) before performing any type of
  18. ' modifications
  19. '
  20. ' Software developers vary on where they want to update the
  21. ' registry with their particular information. The most common
  22. ' are in HKEY_lOCAL_MACHINE or HKEY_CURRENT_USER.
  23. '
  24. ' This BAS module handles all of my needs for string and
  25. ' basic numeric updates in the Windows registry.
  26. '
  27. ' Brett found that NT users must delete each major key
  28. ' separately. See bottom of TEST routine for an example.
  29. ' --------------------------------------------------------------
  30.  
  31. ' --------------------------------------------------------------
  32. ' Private variables
  33. ' --------------------------------------------------------------
  34. Private m_lngRetVal As Long
  35.  
  36. ' --------------------------------------------------------------
  37. ' Constants required for values in the keys
  38. ' --------------------------------------------------------------
  39. Private Const REG_NONE As Long = 0 ' No value type
  40. Private Const REG_SZ As Long = 1 ' nul terminated string
  41. Private Const REG_EXPAND_SZ As Long = 2 ' nul terminated string w/enviornment var
  42. Private Const REG_BINARY As Long = 3 ' Free form binary
  43. Private Const REG_DWORD As Long = 4 ' 32-bit number
  44. Private Const REG_DWORD_LITTLE_ENDIAN As Long = 4 ' 32-bit number (same as REG_DWORD)
  45. Private Const REG_DWORD_BIG_ENDIAN As Long = 5 ' 32-bit number
  46. Private Const REG_LINK As Long = 6 ' Symbolic Link (unicode)
  47. Private Const REG_MULTI_SZ As Long = 7 ' Multiple Unicode strings
  48. Private Const REG_RESOURCE_LIST As Long = 8 ' Resource list in the resource map
  49. Private Const REG_FULL_RESOURCE_DESCRIPTOR As Long = 9 ' Resource list in the hardware description
  50. Private Const REG_RESOURCE_REQUIREMENTS_LIST As Long = 10
  51.  
  52. ' --------------------------------------------------------------
  53. ' Registry Specific Access Rights
  54. ' --------------------------------------------------------------
  55. Private Const KEY_QUERY_VALUE As Long = &H1
  56. Private Const KEY_SET_VALUE As Long = &H2
  57. Private Const KEY_CREATE_SUB_KEY As Long = &H4
  58. Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
  59. Private Const KEY_NOTIFY As Long = &H10
  60. Private Const KEY_CREATE_LINK As Long = &H20
  61. Private Const KEY_ALL_ACCESS As Long = &H3F
  62.  
  63. ' --------------------------------------------------------------
  64. ' Constants required for key locations in the registry
  65. ' --------------------------------------------------------------
  66. Public Const HKEY_CLASSES_ROOT As Long = &H80000000
  67. Public Const HKEY_CURRENT_USER As Long = &H80000001
  68. Public Const HKEY_LOCAL_MACHINE As Long = &H80000002
  69. Public Const HKEY_USERS As Long = &H80000003
  70. Public Const HKEY_PERFORMANCE_DATA As Long = &H80000004
  71. Public Const HKEY_CURRENT_CONFIG As Long = &H80000005
  72. Public Const HKEY_DYN_DATA As Long = &H80000006
  73.  
  74. ' --------------------------------------------------------------
  75. ' Constants required for return values (Error code checking)
  76. ' --------------------------------------------------------------
  77. Private Const ERROR_SUCCESS As Long = 0
  78. Private Const ERROR_ACCESS_DENIED As Long = 5
  79. Private Const ERROR_NO_MORE_ITEMS As Long = 259
  80.  
  81. ' --------------------------------------------------------------
  82. ' Open/Create constants
  83. ' --------------------------------------------------------------
  84. Private Const REG_OPTION_NON_VOLATILE As Long = 0
  85. Private Const REG_OPTION_VOLATILE As Long = &H1
  86.  
  87. ' --------------------------------------------------------------
  88. ' Declarations required to access the Windows registry
  89. ' --------------------------------------------------------------
  90. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lngRootkey As Long) As Long
  91.  
  92. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
  93. (ByVal lngRootkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  94.  
  95. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
  96. (ByVal lngRootkey As Long, ByVal lpSubKey As String) As Long
  97.  
  98. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
  99. (ByVal lngRootkey As Long, ByVal lpValueName As String) As Long
  100.  
  101. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
  102. (ByVal lngRootkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  103.  
  104. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
  105. (ByVal lngRootkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
  106. lpType As Long, lpData As Any, lpcbData As Long) As Long
  107.  
  108. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
  109. (ByVal lngRootkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
  110. ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
  111.  
  112. Public Function regDelete_Sub_Key(ByVal lngRootkey As Long, _
  113. ByVal strRegKeyPath As String, _
  114. ByVal strRegSubKey As String)
  115.  
  116. ' --------------------------------------------------------------
  117. ' Written by Kenneth Ives kenaso@home.com
  118. '
  119. ' Important: If you treat all key data strings as being
  120. ' case sensitive, you should never have a problem.
  121. ' Always backup your registry files (System.dat
  122. ' and User.dat) before performing any type of
  123. ' modifications
  124. '
  125. ' Description: Function for removing a sub key.
  126. '
  127. ' Parameters:
  128. ' lngRootKey - HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
  129. ' HKEY_lOCAL_MACHINE, HKEY_USERS, etc
  130. ' strRegKeyPath - is name of the key path you wish to traverse.
  131. ' strRegSubKey - is the name of the key which will be removed.
  132. '
  133. ' Syntax:
  134. ' regDelete_Sub_Key HKEY_CURRENT_USER, _
  135. "Software\AAA-Registry Test\Products", "StringTestData"
  136. '
  137. ' Removes the sub key "StringTestData"
  138. ' --------------------------------------------------------------
  139.  
  140. ' --------------------------------------------------------------
  141. ' Define variables
  142. ' --------------------------------------------------------------
  143. Dim lngKeyHandle As Long
  144.  
  145. ' --------------------------------------------------------------
  146. ' Make sure the key exist before trying to delete it
  147. ' --------------------------------------------------------------
  148. If regDoes_Key_Exist(lngRootkey, strRegKeyPath) Then
  149.  
  150. ' Get the key handle
  151. m_lngRetVal = RegOpenKey(lngRootkey, strRegKeyPath, lngKeyHandle)
  152.  
  153. ' Delete the sub key. If it does not exist, then ignore it.
  154. m_lngRetVal = RegDeleteValue(lngKeyHandle, strRegSubKey)
  155.  
  156. ' Always close the handle in the registry. We do not want to
  157. ' corrupt the registry.
  158. m_lngRetVal = RegCloseKey(lngKeyHandle)
  159. End If
  160.  
  161. End Function
  162.  
  163. Public Function regDoes_Key_Exist(ByVal lngRootkey As Long, _
  164. ByVal strRegKeyPath As String) As Boolean
  165.  
  166. ' --------------------------------------------------------------
  167. ' Written by Kenneth Ives kenaso@home.com
  168. '
  169. ' Important: If you treat all key data strings as being
  170. ' case sensitive, you should never have a problem.
  171. ' Always backup your registry files (System.dat
  172. ' and User.dat) before performing any type of
  173. ' modifications
  174. '
  175. ' Description: Function to see if a key does exist
  176. '
  177. ' Parameters:
  178. ' lngRootKey - HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
  179. ' HKEY_lOCAL_MACHINE, HKEY_USERS, etc
  180. ' strRegKeyPath - is name of the key path you want to test
  181. '
  182. ' Syntax:
  183. ' strKeyQuery = regQuery_A_Key(HKEY_CURRENT_USER, _
  184. ' "Software\AAA-Registry Test\Products")
  185. '
  186. ' Returns the value of TRUE or FALSE
  187. ' --------------------------------------------------------------
  188.  
  189. ' --------------------------------------------------------------
  190. ' Define variables
  191. ' --------------------------------------------------------------
  192. Dim lngKeyHandle As Long
  193.  
  194. ' --------------------------------------------------------------
  195. ' Initialize variables
  196. ' --------------------------------------------------------------
  197. lngKeyHandle = 0
  198.  
  199. ' --------------------------------------------------------------
  200. ' Query the key path
  201. ' --------------------------------------------------------------
  202. m_lngRetVal = RegOpenKey(lngRootkey, strRegKeyPath, lngKeyHandle)
  203.  
  204. ' --------------------------------------------------------------
  205. ' If no key handle was found then there is no key. Leave here.
  206. ' --------------------------------------------------------------
  207. If lngKeyHandle = 0 Then
  208. regDoes_Key_Exist = False
  209. Else
  210. regDoes_Key_Exist = True
  211. End If
  212.  
  213. ' --------------------------------------------------------------
  214. ' Always close the handle in the registry. We do not want to
  215. ' corrupt these files.
  216. ' --------------------------------------------------------------
  217. m_lngRetVal = RegCloseKey(lngKeyHandle)
  218.  
  219. End Function
  220.  
  221. Public Function regQuery_A_Key(ByVal lngRootkey As Long, _
  222. ByVal strRegKeyPath As String, _
  223. ByVal strRegSubKey As String) As Variant
  224.  
  225. ' --------------------------------------------------------------
  226. ' Written by Kenneth Ives kenaso@home.com
  227. '
  228. ' Important: If you treat all key data strings as being
  229. ' case sensitive, you should never have a problem.
  230. ' Always backup your registry files (System.dat
  231. ' and User.dat) before performing any type of
  232. ' modifications
  233. '
  234. ' Description: Function for querying a sub key value.
  235. '
  236. ' Parameters:
  237. ' lngRootKey - HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
  238. ' HKEY_lOCAL_MACHINE, HKEY_USERS, etc
  239. ' strRegKeyPath - is name of the key path you wish to traverse.
  240. ' strRegSubKey - is the name of the key which will be queryed.
  241. '
  242. ' Syntax:
  243. ' strKeyQuery = regQuery_A_Key(HKEY_CURRENT_USER, _
  244. ' "Software\AAA-Registry Test\Products", _
  245. "StringTestData")
  246. '
  247. ' Returns the key value of "StringTestData"
  248. ' --------------------------------------------------------------
  249.  
  250. ' --------------------------------------------------------------
  251. ' Define variables
  252. ' --------------------------------------------------------------
  253. Dim intPosition As Integer
  254. Dim lngKeyHandle As Long
  255. Dim lngDataType As Long
  256. Dim lngBufferSize As Long
  257. Dim lngBuffer As Long
  258. Dim strBuffer As String
  259.  
  260. ' --------------------------------------------------------------
  261. ' Initialize variables
  262. ' --------------------------------------------------------------
  263. lngKeyHandle = 0
  264. lngBufferSize = 0
  265.  
  266. ' --------------------------------------------------------------
  267. ' Query the key path
  268. ' --------------------------------------------------------------
  269. m_lngRetVal = RegOpenKey(lngRootkey, strRegKeyPath, lngKeyHandle)
  270.  
  271. ' --------------------------------------------------------------
  272. ' If no key handle was found then there is no key. Leave here.
  273. ' --------------------------------------------------------------
  274. If lngKeyHandle = 0 Then
  275. regQuery_A_Key = ""
  276. m_lngRetVal = RegCloseKey(lngKeyHandle) ' always close the handle
  277. Exit Function
  278. End If
  279.  
  280. ' --------------------------------------------------------------
  281. ' Query the registry and determine the data type.
  282. ' --------------------------------------------------------------
  283. m_lngRetVal = RegQueryValueEx(lngKeyHandle, strRegSubKey, 0&, _
  284. lngDataType, ByVal 0&, lngBufferSize)
  285.  
  286. ' --------------------------------------------------------------
  287. ' If no key handle was found then there is no key. Leave.
  288. ' --------------------------------------------------------------
  289. If lngKeyHandle = 0 Then
  290. regQuery_A_Key = ""
  291. m_lngRetVal = RegCloseKey(lngKeyHandle) ' always close the handle
  292. Exit Function
  293. End If
  294.  
  295. ' --------------------------------------------------------------
  296. ' Make the API call to query the registry based on the type
  297. ' of data.
  298. ' --------------------------------------------------------------
  299. Select Case lngDataType
  300. Case REG_SZ: ' String data (most common)
  301. ' Preload the receiving buffer area
  302. strBuffer = Space(lngBufferSize)
  303.  
  304. m_lngRetVal = RegQueryValueEx(lngKeyHandle, strRegSubKey, 0&, 0&, _
  305. ByVal strBuffer, lngBufferSize)
  306.  
  307. ' If NOT a successful call then leave
  308. If m_lngRetVal <> ERROR_SUCCESS Then
  309. regQuery_A_Key = ""
  310. Else
  311. ' Strip out the string data
  312. intPosition = InStr(1, strBuffer, Chr(0)) ' look for the first null char
  313. If intPosition > 0 Then
  314. ' if we found one, then save everything up to that point
  315. regQuery_A_Key = Left(strBuffer, intPosition - 1)
  316. Else
  317. ' did not find one. Save everything.
  318. regQuery_A_Key = strBuffer
  319. End If
  320. End If
  321.  
  322. Case REG_DWORD: ' Numeric data (Integer)
  323. m_lngRetVal = RegQueryValueEx(lngKeyHandle, strRegSubKey, 0&, lngDataType, _
  324. lngBuffer, 4&) ' 4& = 4-byte word (long integer)
  325.  
  326. ' If NOT a successful call then leave
  327. If m_lngRetVal <> ERROR_SUCCESS Then
  328. regQuery_A_Key = ""
  329. Else
  330. ' Save the captured data
  331. regQuery_A_Key = lngBuffer
  332. End If
  333.  
  334. Case Else: ' unknown
  335. regQuery_A_Key = ""
  336. End Select
  337.  
  338. ' --------------------------------------------------------------
  339. ' Always close the handle in the registry. We do not want to
  340. ' corrupt these files.
  341. ' --------------------------------------------------------------
  342. m_lngRetVal = RegCloseKey(lngKeyHandle)
  343.  
  344. End Function
  345. Public Sub regCreate_Key_Value(ByVal lngRootkey As Long, ByVal strRegKeyPath As String, _
  346. ByVal strRegSubKey As String, varRegData As Variant)
  347.  
  348. ' --------------------------------------------------------------
  349. ' Written by Kenneth Ives kenaso@home.com
  350. '
  351. ' Important: If you treat all key data strings as being
  352. ' case sensitive, you should never have a problem.
  353. ' Always backup your registry files (System.dat
  354. ' and User.dat) before performing any type of
  355. ' modifications
  356. '
  357. ' Description: Function for saving string data.
  358. '
  359. ' Parameters:
  360. ' lngRootKey - HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
  361. ' HKEY_lOCAL_MACHINE, HKEY_USERS, etc
  362. ' strRegKeyPath - is name of the key path you wish to traverse.
  363. ' strRegSubKey - is the name of the key which will be updated.
  364. ' varRegData - Update data.
  365. '
  366. ' Syntax:
  367. ' regCreate_Key_Value HKEY_CURRENT_USER, _
  368. ' "Software\AAA-Registry Test\Products", _
  369. ' "StringTestData", "22 Jun 1999"
  370. '
  371. ' Saves the key value of "22 Jun 1999" to sub key "StringTestData"
  372. ' --------------------------------------------------------------
  373.  
  374. ' --------------------------------------------------------------
  375. ' Define variables
  376. ' --------------------------------------------------------------
  377. Dim lngKeyHandle As Long
  378. Dim lngDataType As Long
  379. Dim lngKeyValue As Long
  380. Dim strKeyValue As String
  381.  
  382. ' --------------------------------------------------------------
  383. ' Determine the type of data to be updated
  384. ' --------------------------------------------------------------
  385. If IsNumeric(varRegData) Then
  386. lngDataType = REG_DWORD
  387. Else
  388. lngDataType = REG_SZ
  389. End If
  390.  
  391. ' --------------------------------------------------------------
  392. ' Query the key path
  393. ' --------------------------------------------------------------
  394. m_lngRetVal = RegCreateKey(lngRootkey, strRegKeyPath, lngKeyHandle)
  395.  
  396. ' --------------------------------------------------------------
  397. ' Update the sub key based on the data type
  398. ' --------------------------------------------------------------
  399. Select Case lngDataType
  400. Case REG_SZ: ' String data
  401. strKeyValue = Trim(varRegData) & Chr(0) ' null terminated
  402. m_lngRetVal = RegSetValueEx(lngKeyHandle, strRegSubKey, 0&, lngDataType, _
  403. ByVal strKeyValue, Len(strKeyValue))
  404.  
  405. Case REG_DWORD: ' numeric data
  406. lngKeyValue = CLng(varRegData)
  407. m_lngRetVal = RegSetValueEx(lngKeyHandle, strRegSubKey, 0&, lngDataType, _
  408. lngKeyValue, 4&) ' 4& = 4-byte word (long integer)
  409.  
  410. End Select
  411.  
  412. ' --------------------------------------------------------------
  413. ' Always close the handle in the registry. We do not want to
  414. ' corrupt these files.
  415. ' --------------------------------------------------------------
  416. m_lngRetVal = RegCloseKey(lngKeyHandle)
  417.  
  418. End Sub
  419. Public Function regCreate_A_Key(ByVal lngRootkey As Long, ByVal strRegKeyPath As String)
  420.  
  421. ' --------------------------------------------------------------
  422. ' Written by Kenneth Ives kenaso@home.com
  423. '
  424. ' Important: If you treat all key data strings as being
  425. ' case sensitive, you should never have a problem.
  426. ' Always backup your registry files (System.dat
  427. ' and User.dat) before performing any type of
  428. ' modifications
  429. '
  430. ' Description: This function will create a new key
  431. '
  432. ' Parameters:
  433. ' lngRootKey - HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
  434. ' HKEY_lOCAL_MACHINE, HKEY_USERS, etc
  435. ' strRegKeyPath - is name of the key you wish to create.
  436. ' to make sub keys, continue to make this
  437. ' call with each new level. MS says you
  438. ' can do this in one call; however, the
  439. ' best laid plans of mice and men ...
  440. '
  441. ' Syntax:
  442. ' regCreate_A_Key HKEY_CURRENT_USER, "Software\AAA-Registry Test"
  443. ' regCreate_A_Key HKEY_CURRENT_USER, "Software\AAA-Registry Test\Products"
  444. ' --------------------------------------------------------------
  445.  
  446. ' --------------------------------------------------------------
  447. ' Define variables
  448. ' --------------------------------------------------------------
  449. Dim lngKeyHandle As Long
  450.  
  451. ' --------------------------------------------------------------
  452. ' Create the key. If it already exist, ignore it.
  453. ' --------------------------------------------------------------
  454. m_lngRetVal = RegCreateKey(lngRootkey, strRegKeyPath, lngKeyHandle)
  455.  
  456. ' --------------------------------------------------------------
  457. ' Always close the handle in the registry. We do not want to
  458. ' corrupt these files.
  459. ' --------------------------------------------------------------
  460. m_lngRetVal = RegCloseKey(lngKeyHandle)
  461.  
  462. End Function
  463. Public Function regDelete_A_Key(ByVal lngRootkey As Long, _
  464. ByVal strRegKeyPath As String, _
  465. ByVal strRegKeyName As String) As Boolean
  466.  
  467. ' --------------------------------------------------------------
  468. ' Written by Kenneth Ives kenaso@home.com
  469. '
  470. ' Important: If you treat all key data strings as being
  471. ' case sensitive, you should never have a problem.
  472. ' Always backup your registry files (System.dat
  473. ' and User.dat) before performing any type of
  474. ' modifications
  475. '
  476. ' Description: Function for removing a complete key.
  477. '
  478. ' Parameters:
  479. ' lngRootKey - HKEY_CLASSES_ROOT, HKEY_CURRENT_USER,
  480. ' HKEY_lOCAL_MACHINE, HKEY_USERS, etc
  481. ' strRegKeyPath - is name of the key path you wish to traverse.
  482. ' strRegKeyValue - is the name of the key which will be removed.
  483. '
  484. ' Returns a True or False on completion.
  485. '
  486. ' Syntax:
  487. ' regDelete_A_Key HKEY_CURRENT_USER, "Software", "AAA-Registry Test"
  488. '
  489. ' Removes the key "AAA-Registry Test" and all of its sub keys.
  490. ' --------------------------------------------------------------
  491.  
  492. ' --------------------------------------------------------------
  493. ' Define variables
  494. ' --------------------------------------------------------------
  495. Dim lngKeyHandle As Long
  496.  
  497. ' --------------------------------------------------------------
  498. ' Preset to a failed delete
  499. ' --------------------------------------------------------------
  500. regDelete_A_Key = False
  501.  
  502. ' --------------------------------------------------------------
  503. ' Make sure the key exist before trying to delete it
  504. ' --------------------------------------------------------------
  505. If regDoes_Key_Exist(lngRootkey, strRegKeyPath) Then
  506.  
  507. ' Get the key handle
  508. m_lngRetVal = RegOpenKey(lngRootkey, strRegKeyPath, lngKeyHandle)
  509.  
  510. ' Delete the key
  511. m_lngRetVal = RegDeleteKey(lngKeyHandle, strRegKeyName)
  512.  
  513. ' If the value returned is equal zero then we have succeeded
  514. If m_lngRetVal = 0 Then regDelete_A_Key = True
  515.  
  516. ' Always close the handle in the registry. We do not want to
  517. ' corrupt the registry.
  518. m_lngRetVal = RegCloseKey(lngKeyHandle)
  519. End If
  520.  
  521. End Function
  522.  
  523. Sub Test()
  524.  
  525. ' --------------------------------------------------------------
  526. ' Test Windows registry basic functions.
  527. ' Written by Kenneth Ives kenaso@home.com
  528. '
  529. ' Rename this to "Main". Press F8 to step thru the code. You
  530. ' will be able to stop at will and execute Regedit.exe to see
  531. ' the results. Or, you can press F5 and this test procedure
  532. ' has its own stops built in.
  533. '
  534. ' Perform the four basic functions on the Windows registry.
  535. ' Add
  536. ' Change
  537. ' Delete
  538. ' Query
  539. '
  540. ' Important: If you treat all key data strings as being
  541. ' case sensitive, you should never have a problem.
  542. ' Always backup your registry (System.dat and
  543. ' User.dat) before performing any type of updates.
  544. '
  545. ' Rename this procedure back to TEST so as not to intefere if
  546. ' this BAS module is used in another application.
  547. ' --------------------------------------------------------------
  548.  
  549. ' --------------------------------------------------------------
  550. ' Define variables
  551. ' --------------------------------------------------------------
  552. Dim lngRootkey As Long
  553. Dim strKeyQuery As Variant ' we are not sure what type of
  554. ' data will be returned
  555.  
  556. ' --------------------------------------------------------------
  557. ' Initialize variables
  558. ' --------------------------------------------------------------
  559. strKeyQuery = vbNullString
  560. lngRootkey = HKEY_CURRENT_USER
  561.  
  562. ' --------------------------------------------------------------
  563. ' See if the key already exist. If the key does not exist, we
  564. ' will create one. Some people want to automatically create a
  565. ' key if it does not exist. This philosophy can be dangerous.
  566. ' Querying the registry is one function and updating is another.
  567. ' --------------------------------------------------------------
  568. If Not regDoes_Key_Exist(lngRootkey, "Software\AAA-Registry Test") Then
  569. ' create the main key and the first sub key
  570. regCreate_A_Key lngRootkey, "Software\AAA-Registry Test"
  571. regCreate_A_Key lngRootkey, "Software\AAA-Registry Test\Products"
  572. End If
  573.  
  574. ' --------------------------------------------------------------
  575. ' see if the next sub key exist.
  576. ' --------------------------------------------------------------
  577. If Not regDoes_Key_Exist(lngRootkey, "Software\AAA-Registry Test\Products") Then
  578. ' create the first sub key
  579. regCreate_A_Key lngRootkey, "Software\AAA-Registry Test\Products"
  580. End If
  581.  
  582. ' --------------------------------------------------------------
  583. ' Create a string type sub key
  584. ' --------------------------------------------------------------
  585. regCreate_Key_Value lngRootkey, "Software\AAA-Registry Test\Products", _
  586. "StringTestData", "22 SEP 1999"
  587.  
  588. ' --------------------------------------------------------------
  589. ' Create a numeric type sub key
  590. ' --------------------------------------------------------------
  591. regCreate_Key_Value lngRootkey, "Software\AAA-Registry Test\Products", _
  592. "NumericTestData", 1234567890
  593.  
  594. ' --------------------------------------------------------------
  595. ' See if we have successfully created the key. The value of
  596. ' of the sub key will be returned. strKeyQuery is a variant
  597. ' because we do not know if the data being returned is string
  598. ' or numeric. Once it is returned then we can manipulate it.
  599. ' --------------------------------------------------------------
  600. strKeyQuery = regQuery_A_Key(lngRootkey, "Software\AAA-Registry Test\Products", "StringTestData")
  601. strKeyQuery = regQuery_A_Key(lngRootkey, "Software\AAA-Registry Test\Products", "NumericTestData")
  602.  
  603. ' --------------------------------------------------------------
  604. ' Stop processing here.
  605. ' Execute Regedit.exe and verify that all the keys have
  606. ' been added to the registry.
  607. ' Press F5 or F8 to continue.
  608. ' --------------------------------------------------------------
  609. Stop
  610.  
  611. ' --------------------------------------------------------------
  612. ' Change the value of the sub key, "StringTestData", from
  613. ' "22 SEP 1999" to "September 22, 1999"
  614. ' --------------------------------------------------------------
  615. regCreate_Key_Value lngRootkey, "Software\AAA-Registry Test\Products", _
  616. "StringTestData", "September 22, 1999"
  617.  
  618. ' --------------------------------------------------------------
  619. ' See if the sub key has been updated
  620. ' --------------------------------------------------------------
  621. strKeyQuery = regQuery_A_Key(lngRootkey, "Software\AAA-Registry Test\Products", "StringTestData")
  622.  
  623. ' --------------------------------------------------------------
  624. ' Stop processing here.
  625. ' Execute Regedit.exe and verify that the sub key has
  626. ' been updated in the registry.
  627. ' Press F5 or F8 to continue.
  628. ' --------------------------------------------------------------
  629. Stop
  630.  
  631. ' --------------------------------------------------------------
  632. ' Delete the sub key, "NumericTestData", only.
  633. ' --------------------------------------------------------------
  634. regDelete_Sub_Key lngRootkey, "Software\AAA-Registry Test\Products", "NumericTestData"
  635.  
  636. ' --------------------------------------------------------------
  637. ' Stop processing here.
  638. ' Execute Regedit.exe and verify the sub key ("NumericTestData")
  639. ' has been removed from the registry.
  640. ' Press F5 or F8 to continue.
  641. ' --------------------------------------------------------------
  642. Stop
  643.  
  644. ' --------------------------------------------------------------
  645. ' Remove the complete key from the registry. You do not want
  646. ' to remove the "Software" key. NT users must remove each
  647. ' major key component separately as shown below. Windows 95/98
  648. ' users can do this in one step by using the second line only.
  649. ' --------------------------------------------------------------
  650. If Not regDelete_A_Key(lngRootkey, "Software\AAA-Registry Test", "Products") Then
  651. MsgBox "Failed to delete requested subkey! ", vbOKOnly + vbExclamation, "Registry Key Delete"
  652. GoTo Normal_Exit:
  653. End If
  654.  
  655. If Not regDelete_A_Key(lngRootkey, "Software", "AAA-Registry Test") Then
  656. MsgBox "Failed to delete requested main key! ", vbOKOnly + vbExclamation, "Registry Key Delete"
  657. GoTo Normal_Exit:
  658. End If
  659.  
  660.  
  661. Normal_Exit:
  662. ' --------------------------------------------------------------
  663. ' Terminate program.
  664. ' Execute Regedit.exe and verify that the key
  665. ' ("AAA-Registry Test") and all of its sub keys have been
  666. ' removed from the registry.
  667. ' --------------------------------------------------------------
  668. End
  669.  
  670. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement