home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / enumer1a / enumval.bas < prev    next >
Encoding:
BASIC Source File  |  1999-10-18  |  4.4 KB  |  130 lines

  1. Attribute VB_Name = "EnumerateValues"
  2. 'Subroutine EnumerateRegValues by Jason Merlo 10/18/99
  3. 'This Sub is an example of how to read registry values without knowing
  4. 'their data type.  It simply takes a predefined registry key and dumps
  5. 'all of its values to a text file in the form:  "Value name", "Value".
  6.  
  7. 'NOTE:  This was tested on Windows NT 4.0 SP4.  Other operating systems
  8. 'May behave differently.
  9.  
  10. Option Explicit
  11.  
  12. Dim Index As Long
  13. Dim BufferLength As Long
  14. Dim BufferLength2 As Long
  15. Dim Resp As Long
  16. Dim KeyHandle As Long
  17. Dim DataType As Long
  18. Dim DummyString As String * 100
  19. Dim DummyString2 As String
  20. Dim i As Integer
  21.  
  22. Dim OutFileNum As Long
  23. Const FILE_PATH = "C:\TEMP\keydump.txt"
  24. Const REG_PATH = "SOFTWARE\Microsoft\Windows\CurrentVersion"
  25.  
  26. 'Here's the trick:  Since the RegEnumValue API function requires a variable
  27. 'of type BYTE as its return (lpData), we need to do something sneaky.
  28. 'Since we don't have pointers in VB, we define a Type (or structure)
  29. 'where the first item is a single byte, and the rest is a byte array.
  30. 'This way, we can pass the function the byte it needs and our array will
  31. 'get populated with the return, whatever its data type.  This is because
  32. 'The variables defined within a Type are always stored consecutively
  33. 'in memory.  Now all we need to know is how to parse the returned bytes
  34. 'to get the value we want.
  35.  
  36. Private Type ByteArray
  37.   FirstByte As Byte
  38.   ByteBuffer(100) As Byte
  39. End Type
  40.  
  41. 'We want a variable of the above type to play with.
  42. Dim RegValue As ByteArray
  43.  
  44. Public Sub EnumerateRegValues()
  45.  
  46.   'Open the output file.
  47.   OutFileNum = FreeFile
  48.   Open FILE_PATH For Output As OutFileNum
  49.  
  50.   Index = 0
  51.   'Open the key and get its handle.
  52.   Resp = RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_PATH, 0, KEY_READ, KeyHandle)
  53.   Do While Resp = 0
  54.     'These initializations are *very* important.  Without them, the API call will
  55.     'think you are trying to pass it something and return bad data.
  56.     DataType = 0
  57.     BufferLength = 100
  58.     BufferLength2 = 100
  59.     DummyString = ""
  60.     RegValue.FirstByte = 0
  61.     For i = 0 To 100
  62.       RegValue.ByteBuffer(i) = 0
  63.     Next
  64.     
  65.     'Grab the value at this Index.
  66.     Resp = RegEnumValue(KeyHandle, Index, DummyString, BufferLength, 0, DataType, RegValue.FirstByte, BufferLength2)
  67.     'See if we're out of values.  Return code 259 means no more data.
  68.     If Resp = 259 Then Exit Do
  69.     
  70.     'Now we need to parse the ByteArray.
  71.     'for strings, we grab the chr$ values.
  72.     Select Case DataType
  73.     
  74.       'String data.
  75.       Case REG_SZ, REG_EXPAND_SZ:
  76.         DummyString2 = Chr$(RegValue.FirstByte)
  77.         'After the first byte, the rest are in the ByteBuffer.
  78.         'Subtract one for null termination, one for FirstByte,
  79.         'and one for the zero index.
  80.         For i = 0 To BufferLength2 - 3
  81.           DummyString2 = DummyString2 & Chr$(RegValue.ByteBuffer(i))
  82.         Next i
  83.         
  84.       'DWORD data.
  85.       Case REG_DWORD:
  86.         'All dwords are four bytes long, so we will just convert to decimal:
  87.         DummyString2 = Trim(Str(RegValue.FirstByte _
  88.                        + (256 * RegValue.ByteBuffer(0)) _
  89.                        + (65536 * RegValue.ByteBuffer(1)) _
  90.                        + (16777216 * RegValue.ByteBuffer(2))))
  91.         
  92.       'Binary data.
  93.       Case REG_BINARY:
  94.         'Just line up the bytes.  No null termination here.
  95.         DummyString2 = Trim(Hex(RegValue.FirstByte))
  96.         For i = 0 To BufferLength2 - 2
  97.           DummyString2 = DummyString2 & " " & Trim(Hex(RegValue.ByteBuffer(i)))
  98.         Next i
  99.       
  100.       'We won't worry about other data types here for this example.
  101.       'But you can add code to parse the byte array for any other
  102.       'needed data types.  Just check the text file for the DataType
  103.       'after running the function and compare it to the API DIMs!
  104.       Case Else:
  105.         DummyString2 = "Unsupported data type: " & DataType
  106.  
  107.     End Select
  108.  
  109.     'Now the Value name is in DummyString -- up to the first Chr$(0),
  110.     'and the value itself resides in DummyString2.
  111.     Write #OutFileNum, Left(DummyString, (InStr(DummyString, Chr$(0))) - 1), DummyString2
  112.     
  113.     Index = Index + 1
  114.     
  115.   Loop
  116.   'Let's be tidy and clean up after ourselves.
  117.   Resp = RegCloseKey(KeyHandle)
  118.   
  119.   'Close the file.
  120.   Close #OutFileNum
  121.   
  122. End Sub
  123.  
  124. Public Sub main()
  125.  
  126. 'This could be modified to pass the function a path...
  127. Call EnumerateRegValues
  128.  
  129. End Sub
  130.